0%

PHP框架

古之欲明明德于天下者,先治其国;欲治其国者,先齐其家;欲齐其家者,先修其身;欲修其身者,先正其心;欲正其心者,先诚其意;欲诚其意者,先致其知,致知在格物。物格而后知至,知至而后意诚,意诚而后心正,心正而后身修,身修而后家齐,家齐而后国治,国治而后天下平。–《礼记·大学》

Laravel

一、基础

      Laravel是一个免费的开放源代码PHP Web框架,由Taylor Otwell创建,旨在遵循模型-视图-控制器(MVC)架构模式开发Web应用程序。

  1. 特点

    • 强大的rest router:用简单的回调函数就可以调用,快速绑定controller和router
    • artisan:命令行工具,很多手动的工作都自动化
    • 可继承的模板,简化view的开发和管理
    • blade模板:渲染速度更快
    • ORM操作数据库
    • migration:管理数据库和版本控制
    • PHPUnit单元测试功能
    • composer包自动管理依赖
    • 设计模式:laravel框架引入了门面,依赖注入,Ioc 模式,以及各种各样的设计模式等
  2. 涉及的设计模式

    • 工厂模式
    • 单例模式
    • 策略模式
    • 原型模式
    • 代理模式
    • 注册树模式
    • 适配器模式
    • 观察者模式
    • 装饰器模式
    • 迭代器模式
  1. Request Lifecycle:Laravel的生命周期从public\index.php开始,从public\index.php结束,以下是public\index.php全部代码:
1
2
3
4
5
6
7
8
9
10
11
require __DIR__.'/../bootstrap/autoload.php';

$app = require_once __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();

$kernel->terminate($request, $response);
  • 加载项目依赖,注册加载composer自动生成的class loader,也就是加载初始化第三方依赖(包括项目自带和手动require)。
  • 创建应用实例,生成容器 Container,并向容器注册核心组件,是从bootstrap/app.php脚本获取 Laravel 应用实例,并且绑定内核服务容器。
    • 它根据HTTP请求的运行环境的不同,将请求发送至相应的内核:HTTP内核或Console内核。
  • 接收请求并响应,请求被发送到HTTP内核或Console内核,这取决于进入应用的请求类型。
    • HTTP内核继承自Illuminate\Foundation\Http\Kernel类,该类定义了一个bootstrappers数组,这个数组中的类在请求被执行前运行,这些bootstrappers配置了错误处理、日志、检测应用环境以及其它在请求被处理前需要执行的任务。
    • HTTP内核还定义了一系列所有请求在处理前需要经过的HTTP中间件,这些中间件处理HTTP会话的读写、判断应用是否处于维护模式、验证CSRF令牌等等。
  • 发送请求,在Laravel基础的服务启动之后,把请求传递给路由。
    • 路由器将会分发请求到路由或控制器,同时运行所有路由指定的中间件。
    • 传递给路由是通过Pipeline来传递的,在传递给路由之前所有请求都要经过app\Http\Kernel.php中的$middleware数组,也就是中间件。
    • 默认只有一个全局中间件,用来检测你的网站是否暂时关闭。所有请求都要经过,你也可以添加自己的全局中间件。
    • 然后遍历所有注册的路由,找到最先符合的第一个路由,经过它的路由中间件,进入到控制器或者闭包函数,执行你的具体逻辑代码,把那些不符合或恶意的的请求隔离在外。
  1. 操作数据库:Laravel makes interacting with databases extremely simple across a variety of database backends using either raw SQL, the fluent query builder, and the Eloquent ORM.

    • insert:插入数据时,需要维护created_at和updated_at字段
    • save:无论插入或者更新会自动维护,无需手动操作,其接受一个完整的Eloquent模型实例
      • 更新模型时,需要检索到它然后设置模型属性,再调用save方法,updated_at时间戳会自动更新
      • save()方法的调用必须先实例化对象,所以是非静态(static)方法
      • save()方法的功能是插入或更新,每一次调用方法都会查找当前的数据是否包含主键和数据表里是否包含当前主键,如果数据中没主键则获得一个自增的主键给当前对象,如果数据表中已存在该主键则更新该条数据
      • 返回值为整型或false
    • create:自动维护created_at和updated_at两个字段,其接受普通的PHP数组
      • create()方法为静态方法,通过通过类直接调用
      • create()的功能为实例对象和插入(new and create)
      • 返回值为当前模型的实例对象
    • update:更新操作,自动维护updated_at字段
  2. 反射的应用

  3. Contracts:Laravel’s Contracts are a set of interfaces that define the core services provided by the framework.

  4. Facades:Facades provide a “static” interface to classes that are available in the application’s service container. Laravel ships with many facades which provide access to almost all of Laravel’s features.

  5. Service Container:The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection. Dependency injection is a fancy phrase that essentially means this: class dependencies are “injected” into the class via the constructor or, in some cases, “setter” methods.

  6. Service Providers:Service providers are the central place of all Laravel application bootstrapping. Your own application, as well as all of Laravel’s core services are bootstrapped via service providers.

  7. 路由

    • Route::apiResource,资源隐式绑定

二、使用

  1. 项目创建

    • 使用git
    • 使用composer
      • 使用composer直接创建composer create-project laravel/laravel example-app
      • 使用laravel工具
        • composer global require laravel/installer
        • laravel new example-app
    • 使用Docker Desktop(MacOS)
      • curl -s "https://laravel.build/example-app" | bash
      • cd example-app
      • ./vendor/bin/sail up
  2. 数据库

    • 建表:php artisan make:migration create_users_table,最终会生成users表
    • 生成:php artisan migrate,生成已定义好的表
  3. 控制器

    • 创建:php artisan make:controller UserController
  4. 提交表单419错误码

    • 表单加{{csrf_field()}}
  5. Requests对象

  6. 重定向的几种办法

    • 使用Facdes的方式
      • return Redirect::to(‘/home’);
      • return Redirect::route(‘website.home’);
      • return Redirect::action(‘homeController@home’);
    • 使用快速函数
      • redirect()->to(‘/home’);
      • redirect()->route(‘home’);
      • return redirect()->action(‘homeController@home’);
  7. 常用命令

    • php artisan optimize
    • php artisan view:clear
    • php artisan cache:clear
    • php artisan route:clear
    • php artisan route:list
    • php artisan make:request Chat/ChatRequests
    • php artisan make:migration xxx_table
    • php artisan migrate –path=/database/migrations/xxx_table.php –f
    • php artisan migrate
    • php artisan schedule:list
    • php artisan schedule:run
    • php artisan schedule:work
    • php artisan queue:retry +uuid
    • php artisan queue:restart
    • php artisan send:mail

三、参考

  1. 参考一
  2. 参考二
  3. 参考三
  4. 参考四

ThinkPHP


CodeIgniter(CI)


Yii


Symfony


Zend Framework