前言
本篇是对我的PHP, Laravel系列博文的总结与思考。php
目的在于理清并熟练以下过程:css
"需求 --> Usercase --> UI --> 框架 --> 开发"html
需求分析
1、页面初步设计
Ref: [Laravel] 06 - Project: from Usercase to Viewmysql
1. Pure html 写法jquery
2. 找到其中的公共部分 --> blade模板laravel
3. 布局设计css,bootstrap and jqueryweb
重难点也就是blade,以及bootstra p& jquery。sql
2、Common页面模板设计
views -- common -- layouts.blade.php -- message.blade.php -- validator.blade.php
- Layouts 核心模板
<head>数据库
<title>轻松学会Laravel - @yield('title')</title>
</head>json
<body>
Ref: laravel 基础教程 —— Blade 模板引擎【写的不错】 Ref: [Laravel] 04 - Blade templates【关键字的理解】
</body>
- 布局设计
这部份内容请见:[Full-stack] 网页布局艺术 - Less
3、路由与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']);
(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 设计
1、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 } }
2、缓存策略
-
静态缓存 与 mem缓存
Ref: [Laravel] 11 - WEB API : cache & timer
此连接内容为下面的缓存三种策略作铺垫。
- 缓冲方案
这里以静态缓冲做为例子,仅考虑后两种方案便可。
Goto: [Laravel] 12 - WEB API : cache implement
3、数据库连接
须要复习一遍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);
4、版本检查
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();
5、错误日志
携带设备信息 device id 的 request 发送到服务器端,
而后,服务器当作 error log 保存起来。
Maravel API 设计
1、前言
- REST API
Goto: [Node.js] 08 - Web Server and REST API
- Laravel API
Ref: https://laravel.com/docs/5.6
2、功能实现
- 用户注册模块
Goto: [Laravel] 14 - REST API: Laravel from scratch
3、单元测试
Goto: [Laravel] 15 - REST API: sidebar with unit test
附录
1、PHP 参考手册
2、其余经常使用函数
- ucwords() 函数
把每一个单词的首字符转换为大写。
$resultClass = ucwords($type);
- 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关键字的用法及注意点