前言


本篇是对个人PHP, Laravel系列博文的总结与思考。

目的在于理清并熟练如下过程:

"需求 --> Usercase --> UI --> 框架 --> 开发"

需求分析


一、页面初步设计

Ref: [Laravel] 06 - Project: from Usercase to View

1. Pure html 写法

2. 找到其中的公共部分 --> blade模板

3. 布局设计css,bootstrap and jquery

重难点也就是blade,以及bootstra p& jquery。

二、Common页面模板设计

views -- common -- layouts.blade.php
-- message.blade.php
-- validator.blade.php
  • Layouts 核心模板

<head>

<title>轻松学会Laravel - @yield('title')</title>

</head>

<body>

Ref: laravel 基础教程 —— Blade 模板引擎【写的不错】

Ref: [Laravel] 04 - Blade templates【关键字的理解】

</body>

  • 布局设计

这部分内容请见:[Full-stack] 网页布局艺术 - Less

三、路由与MVC框架

  • 有效路由 与 无效路由

无效路由:没有实际页面的路由,且request后,服务器会调用redirect重定向到一个有效路由对应的页面。

Route::group(['middleware' => ['web']], function () {

    Route::get('student/index',       ['uses' => 'StudentController@index' ]);
Route::any('student/create', ['uses' => 'StudentController@create']);
Route::any('student/save', ['uses' => 'StudentController@save' ]);
Route::any('student/update/{id}', ['uses' => 'StudentController@update']);
Route::any('student/detail/{id}', ['uses' => 'StudentController@detail']);
Route::any('student/delete/{id}', ['uses' => 'StudentController@delete']);
});

Ref: 路由相关概念:[Laravel] 02 - Route and MVC

  • 模型 与 数据库

[1] 首先,pure php 是如何链接数据库的呢?

Goto: [PHP] 07 - Json, XML and MySQL

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB"; // 1.创建连接
$conn = new mysqli($servername, $username, $password, $dbname); // 2.检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}

// 3.SQL操作
$sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

// 4.关闭连接
$conn->close();
?>

[2] Laravel 又是如何通过框架实现的呢?

Goto: [Laravel] 03 - DB facade, Query builder & Eloquent ORM

1. 原始方式

第一步,设置配置文件:[config/database.php]

第二步,执行sql语句。

use Illuminate\Support\Facades\DB
$students = DB::select('select * from student');

2. 查询构造器

3. Eloquent ORM - 对象关系映射(Object Relational Mapping,简称ORM)

  • 路由 --> 控制器 (模型) --> 视图

[1] 路由 到 控制器

Route::any('student/create', ['uses' => 'StudentController@create']);

URL中参数解析:

  (1) router 中获得已定义好的参数。[Laravel] 02 - Route and MVC

  (2) controller 中通过request函数获得URL中key对应的value。[Laravel] 05 - Controller

[2] 控制器 with 模型 到 视图

    // 添加页面
public function create(Request $request)
{
# 获得模型载体
$student = new Student(); if ($request->isMethod('POST')) { // 控制器验证 or Validator类验证
# 获取数据
$data = $request->input('Student');

# 载体加载了数据,数据库与载体数据同步
if (Student::create($data) ) {
return redirect('student/index')->with('success', '添加成功!');
} else {
return redirect()->back();
}
}

# 视图展示载体
return view('student.create', [
'student' => $student
]);
}

PHP API 设计


一、Pure PHP 封装接口

  • 什么叫封装?

封装一个类,服务器端采用 response 处理以下内容。

code 返回的id,统一编号
message 具体解释,detail
data 具体内容,参数

参数要数组化,构成 $result

$result --> XML or JSON 格式

XML组装时需要一个递归的解析过程:

public static function xmlToEncode($data) {

  $xml = $attr = "";
  foreach( $data as $key => $value) {     if (is_numberic($key)) {
      $attr = "id='{$key}'";
      $key = "item";
     # <item id=[key]>
    }     $xml .= "<{$key}><{$attr}>";
    $xml .= is_array($value) ? self::xmlToEncode($value) : $value;
    $xml .= "</{$key}>";  
  }   return $xml;
}
  • Response 封装

这里可以考虑使用工厂方法。

public static function show($code, $message='', $data=array(), $type=self::JSON) {
if(!is_numeric($code)) {
return '';
} $type = isset($_GET['format']) ? $_GET['format'] : self::JSON; $result = array(
'code' => $code,
'message' => $message,
'data' => $data,
);
/**
* 以下switch的写法,也可以写成工厂方法的形式,
*/
if($type == 'json') {
self::json($code, $message, $data);
exit; } elseif($type == 'array') {
var_dump($result); } elseif($type == 'xml') {
self::xmlEncode($code, $message, $data);
exit; } else {
// TODO
}
}

二、缓存策略

  • 静态缓存 与 mem缓存

Ref: [Laravel] 11 - WEB API : cache & timer

此链接内容为下面的缓存三种策略做铺垫。

  • 缓冲方案

这里以静态缓冲作为例子,仅考虑后两种方案即可。

Goto: [Laravel] 12 - WEB API : cache implement

三、数据库链接

需要复习一遍sql语句:[Laravel] 16 - DB: Eloquent

# step 1, 语句编写
$sql = "select *
    from `version_upgrade`
    where app_id = " . $appId ."
    and status = 1
    limit 1";

# step 2,连接
$connect = Db::getInstance()->connect();

#step 3,执行语句
$result = mysql_query($sql, $connect);

#step 4,转化结果格式
mysql_fetch_assoc($result);

四、版本检查

Ref: [Laravel] 13 - WEB API : update & error tracking

<?php
require_once('./common.php'); class Init extends Common {
  public function index() {
    $this->check();  # check的实现,判断app的版本是否有问题
/**
* 1.获得手机信息,确认没有问题
* 2.该手机是否需要升级
* implement here.
*/
Response::show(number, '返回给app的一些信息');
  }
}
-------------------------------------------
$init = new Init();
$init->index();

五、错误日志

携带设备信息 device id 的 request 发送到服务器端,

然后,服务器当做 error log 保存起来。

Maravel API 设计


一、前言

  • REST API

Goto: [Node.js] 08 - Web Server and REST API

  • Laravel API

Ref: https://laravel.com/docs/5.6

二、功能实现

  • 用户注册模块

Goto: [Laravel] 14 - REST API: Laravel from scratch

三、单元测试

Goto: [Laravel] 15 - REST API: sidebar with unit test

附录


一、PHP 参考手册

二、其他常用函数

  • ucwords() 函数

把每个单词的首字符转换为大写。

$resultClass = ucwords($type);
毕竟url中的参数不能使用大写。
 
  • dirname(__FILE__) 
当前所在目录,也可以是目录所在的目录。
dirname(dirname(__FILE__)); 
假设__FILE__为 /home/web/config/config.php
上面的方法输出为 /home/web
  • is_dir()
是目录么 
  • file_put_contents()
字符串写入文件
<?php
echo file_put_contents("test.txt","Hello World. Testing!");
?>
  • @unlink()
当删除一个不存在或只读的文件时,是要报错的
@ 作用就是:错了也不告诉你!骗你没商量
if(is_null($value)) {
  return @unlink($filename);
}
  • is_numeric($var)

常用于检查参数。

if(!is_numeric($appId) || !is_numeric($versionId)) {
  return Response::show(401, '参数不合法');
}
  • static:: 

Ref: PHP static关键字的用法及注意点

详见: [PHP] 02 - Namespace & Class

[Full-stack] 世上最好语言 - PHP的更多相关文章

  1. DSP中CMD文件

    DSP中CMD文件 (2012-12-26 20:54:17) 转载▼ 标签: 杂谈 分类: DSP FPGA DSP的存储器的地址范围,CMD是主要是根据那个来编的.CMD 它是用来分配rom和ra ...

  2. [Code::Blocks] Install wxWidgets & openCV

    The open source, cross platform, free C++ IDE. Code::Blocks is a free C++ IDE built to meet the most ...

  3. 第21月第4天 leetcode codinginterview c++

    1.leetcode Implement strStr(). Returns the index of the first occurrence of needle in haystack, or - ...

  4. 本人SW知识体系导航 - Programming menu

    将感悟心得记于此,重启程序员模式. js, py, c++, java, php 融汇之全栈系列 [Full-stack] 快速上手开发 - React [Full-stack] 状态管理技巧 - R ...

  5. Java Run-Time Data Areas

    前言 本文主要介绍JVM的运行时数据区 来自Oracle文档 Java Virtual Machine Specification -- Chapter 2. The Structure of the ...

  6. java Vamei快速教程22 内存管理和垃圾回收

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 整个教程中已经不时的出现一些内存管理和垃圾回收的相关知识.这里进行一个小小的总结. ...

  7. 【Java】JMM内存模型和JVM内存结构

    JMM内存模型和JVM内存结构 JAVA内存模型(Java Memory Model) Java内存模型,一般指的是JDK 5 开始使用的新的内存模型,主要由JSR-133: JavaTM Memor ...

  8. 【DSP开发】CMD文件

    DSP的存储器的地址范围,CMD是主要是根据那个来编的. CMD 它是用来分配rom和ram空间用的,告诉链接程序怎样计算地址和分配空间. 所以不同的芯片就有不同大小的rom和ram.放用户程序的地方 ...

  9. 再谈js对象数据结构底层实现原理-object array map set

    如果有java基础的同学,可以回顾下<再谈Java数据结构—分析底层实现与应用注意事项>:java把内存分两种:一种是栈内存,另一种是堆内存.基本类型(即int,short,long,by ...

随机推荐

  1. no console to display at this time

    no console to display at this time我把控制台关掉,重新run as 还是同样问题,于是运行其他项目,但是其他项目能正常运行,说明项目写的有问题,而不是控制台的问题

  2. spring boot + embed tomcat + standalone jar的内存泄露问题

    前一阵遇到了一个很坑的内存泄露问题,记录于此: 有个项目采用spring cloud重构后,部署到线上(其中有一个接口,大概每天调用量在1千万次左右),发现zabbix监控里,linux的可用内存一直 ...

  3. IDA Pro Disassembler 6.8.15.413 (Windows, Linux, Mac)

    IDA: What's new in 6.8 Highlights This is mainly a maintenance release, so our focus was on fixing b ...

  4. C#编程(八十一)---------- 捕获异常

    捕获异常 前面主要说了关于异常的一些基础和理论知识,没有进入到正真的异常案例,这一讲通过几个案例来描述一下异常的捕获和处理. 案例代码: using System; using System.Coll ...

  5. C# SpinWait 实现

    其实SpinWait的code 非常简单,以前看过很多遍,但是从来都没有整理过,整理也是再次学习吧. 我们先看看SpinWait的一些评论或者注意点吧:如果等待某个条件满足需要的时间很短,而且不希望发 ...

  6. centos7下安装gcc7

    之前写过在linux下升级gcc 4.8至gcc 4.9的过程,现在gcc最新的版本是8,有些软件必须是gcc 7或者以上的版本才可以编译,比如clickhouse,gcc 7的安装过程和之前基本上一 ...

  7. 《A.I.爱》王力宏与人工智能谈恋爱 邀李开复来客串

    2017年9月19日下午,王力宏首张数字专辑<A.I.爱>亚洲发布会在北京举行,力宏在新歌MV中化身技术男,网红机器人Sophia扮新娘!和Robo Alpha机器人天团大跳舞蹈,与超跑酷 ...

  8. [Python设计模式] 第6章 衣服搭配系统——装饰模式

    github地址:https://github.com/cheesezh/python_design_patterns 题目 设计一个控制台程序,可以给人搭配嘻哈风格(T恤,垮裤,运动鞋)或白领风格( ...

  9. fiddler展示serverIP方法

    转载自:http://www.xuanfengge.com/fiddler-displays-the-set-ip-method.html 前言 由于web前端在多个环境中开发,需要经常更换host, ...

  10. 廉价的SUP掌机拆解

    最近经常出现的一款山寨sup掌机, 75元包邮入手, 全套配件如下. 看看正面和背面的实拍图, 比较明显的廉价玩具塑料感. 手柄和充电线共用下方的microUSB口, 所以在双打时是不能用电源供电的. ...