前言
本篇是对个人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关键字的用法及注意点