php的laravel,PHP Laravel框架分享

Why Laravel

github 50k stars

bb3fb9ba0ecd3091bfc00aaf63017d8b.png

google trend

f1beb001a1fd464e56ca9766e2ff7b59.png

目录结构

.

├── app //应用的核心代码

│   ├── Broadcasting //广播频道类

│   ├── Console //自定义artisan命令

│   ├── Events //事件类(观察者)

│   ├── Exceptions //异常类

│   ├── Http //所有http相关的 控制器/中间件等

│   ├── Jobs //队列任务

│   ├── Listeners //处理事件监听的类(观察者)

│   ├── Mail //邮件相关类

│   ├── Models //与数据库交互的模型类

│   ├── Notifications //应用发送的所有通知

│   ├── Policies //应用所有的授权策略类

│   ├── Providers //服务提供者

│   └── Rules //自定义验证规则对象

├── bootstrap //框架的启动和自动载入配置

│   ├── app.php

│   └── cache //提升性能缓存文件夹

├── config //应用所有的配置文件

│   ├── app.php

│   ├── auth.php

│   ├── broadcasting.php

│   ├── cache.php

│   ├── database.php

│   ├── filesystems.php

│   ├── hashing.php

│   ├── logging.php

│   ├── mail.php

│   ├── queue.php

│   ├── services.php

│   ├── session.php

│   └── view.php

├── database //数据库迁移文件及填充文件

├── public //服务器所指向的应用根目录

│   └── index.php

├── resources //应用视图文件和未编译的原生前端资源文件(LESS、SASS、JavaScript)

├── routes //应用定义的所有路由

│   ├── api.php //纯api,需要通过token认证

│   ├── channels.php //事件广播路由

│   ├── console.php //控制台命令

│   └── web.php //view Session/CSRF/Cookie

├── storage

│   ├── app //应用生成的文件

│   ├── framework //框架生成或缓存的文件

│   └── logs //应用日志

├── tests

│   ├── Feature

│   └── Unit

└── vendor

创建个新项目

composer create-project laravel/laravel test --prefer-dist

docker-compose down

docker-compose up -d nginx mysql phpmyadmin redis workspace

1) web hello-world

./artisan make:controller HelloWorldController

2) api hello-world

Route::middleware('auth:api', 'throttle:60,1')->group(function () {

Route::get('/user', function () {

//

});

});

3) console hello-world

./artisan make:command HelloWorld

4) channels hello-world

请求生命周期

bdc8b016a6c46b70951a224dd78c09af.png

核心概念

服务容器

ef834618ca56575876ee57c75badd668.png

绑定闭包

$this->app->bind('HelpSpot\API', function ($app) {

return new HelpSpot\API();

});//闭包直接提供实现方式

绑定接口

$this->app->bind(

'App\Contracts\EventPusher',

'App\Services\RedisEventPusher'

);

singleton绑定

$this->app->singleton('HelpSpot\API', function ($app) {

return new HelpSpot\API($app->make('HttpClient'));

});

instance绑定

$api = new HelpSpot\API(new HttpClient);

$this->app->instance('HelpSpot\Api', $api);

Context绑定

$this->app->when(DumpServer::class)->needs('$host')->give($host);

$this->app->when(StorageController::class)

->needs(Filesystem::class)

->give(function () {

Storage::class

});//提供类名

$this->app->when(PhotoController::class)

->needs(Filesystem::class)

->give(function () {

return new Storage();

});//提供实现方式

服务别名

$this->app->make('auth')

又可以写成:

$this->app->make('\Illuminate\Auth\AuthManager::class')

还可以写成:

$this->app->make('\Illuminate\Contracts\Auth\Factory::class')

具体实现类

make解析

$fooBar = $this->app->make('HelpSpot\API');

//or

$api = resolve('HelpSpot\API');

例子

// 创建一个容器(后面称作超级工厂)

$container = new Container;

// 向该 超级工厂添加超人的生产脚本

$container->bind('superman', function($container, $moduleName) {

return new Superman($container->make($moduleName));

});

// 向该 超级工厂添加超能力模组的生产脚本

$container->bind('xpower', function($container) {

return new XPower;

});

// 同上

$container->bind('ultrabomb', function($container) {

return new UltraBomb;

});

// ****************** 华丽丽的分割线 **********************

// 开始启动生产

$superman_1 = $container->make('superman', 'xpower');

$superman_2 = $container->make('superman', 'ultrabomb');

$superman_3 = $container->make('superman', 'xpower');

// ...随意添加

服务提供者

应用启动的中心,应用以及所有 的核心服务都是通过服务提供者启动。

就是提供了把类注册到容器的功能,以便后面使用。

* config/app.php

* register注册

* boot 引导、初始化

门面

门面为应用服务容器中的绑定类提供了一个「静态」接口。

Route::get('/', function() {

// bla bla bla...

});

c70b1ce92bda2874b99fbe7cca02c695.png

契约

指框架提供的一系列定义核心服务的接口,如:

Illuminate\Contracts\Queue\Queue

Illuminate\Contracts\Mail\Mailer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值