- Laravel环境安装配置
- Laravel应用程序结构
- Laravel配置
- Laravel路由
- Laravel中间件
- Laravel控制器
- Laravel请求
- Laravel Cookie
- Laravel响应
- Laravel视图
- Laravel重定向
- Laravel操作数据库
- Laravel插入数据库表数据
- Laravel检索查询数据
- Laravel更新数据
- Laravel删除数据
- Laravel错误和日志记录
- Laravel表单处理
- Laravel本地化
- Laravel会话(session)
- Laravel验证
- Laravel文件上传
- Laravel发送邮件
- Laravel Ajax
- Laravel错误处理
- Laravel事件处理
- Laravel Facades
- Laravel安全
Laravel中间件
php artisan make:middleware <middleware-name>
使用你的中间件名称替换<middleware-name>。创建可以看到中间件在 app/Http/Middleware 目录。
示例
php artisan make:middleware AgeMiddleware
第3步 − AgeMiddlware 会在 app/Http/Middleware 文件中创建。新创建的文件将自动创建下面代码:
<?php namespace App\Http\Middleware; use Closure; class AgeMiddleware { public function handle($request, Closure $next) { return $next($request); } }
注册中间件
-
全局中间件
-
路由中间件
全局中间件将在应用程序的每个HTTP请求运行,而路由中间件将被分配到一个特定的路由。中间件可在 app/Http/Kernel.php 注册.
该文件包含两个属性: $middleware 和 $routeMiddleware。$middleware 属性用于注册全局中间件,$routeMiddleware属性用于注册路由指定中间件。
protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, ];
protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, ];
示例
我们已经在前面的例子中已创建 AgeMiddleware 中间件。 现在,我们可以在具体的路由中间件属性登记。注册代码如下所示。
<?php namespace App\Http; use Illuminate\Foundation\Http\Kernel as HttpKernel; class Kernel extends HttpKernel { protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, ]; protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'Age' => \App\Http\Middlware\AgeMiddleware::class, ]; }
中间件参数
我们也可以传递中间件参数。例如,如果您的应用程序有不同角色,如用户,管理员,超级管理员等,并且要验证基于角色的动作,这可以通过参数传递中间件来实现。我们创建的中间件包含以下功能,我们可以通过 $next 参数之后,自定义参数。
public function handle($request, Closure $next) { return $next($request); }
示例
php artisan make:middleware RoleMiddleware
第3步 − 添加以下代码到新创建的角色中间件的处理方法 - app/Http/Middleware/RoleMiddleware.php.
<?php namespace App\Http\Middleware; use Closure; class RoleMiddleware { public function handle($request, Closure $next, $role) { echo "Role: ".$role; return $next($request); } }
第4步 − 在 app\Http\Kernel.php 文件中注册角色中间件。 加入灰色突出的线条的是在该文件中注册 RoleMiddleware 中间件。
php artisan make:controller TestController
app/Http/TestController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; class TestController extends Controller { public function index(){ echo "<br>Test Controller."; } }
app/Http/routes.php
Route::get('role',[ 'middleware' => 'Role:editor', 'uses' => 'TestController@index', ]);
可终止的中间件
响应已经发送给浏览器后可终止中间件执行一些任务。这可以通过使用创建中间件“终止”方法的中间件来实现。可终止中间件会注册全局中间件。该终止方法将接受两个参数:$request 和 $response。终止方法可以被创建,如在下面显示的代码。
示例
php artisan make:middleware TerminateMiddleware
第3步 - 复制下面的代码到新创建文件 - TerminateMiddleware 在 app/Http/Middleware/TerminateMiddleware.php 文件中如下:
<?php namespace App\Http\Middleware; use Closure; class TerminateMiddleware { public function handle($request, Closure $next) { echo "Executing statements of handle method of TerminateMiddleware."; return $next($request); } public function terminate($request, $response){ echo "<br>Executing statements of terminate method of TerminateMiddleware."; } }
第4步 − 注册 TerminateMiddleware 到 app\Http\Kernel.php 文件中.
加了灰色突出的线条的是该文件中注册的 TerminateMiddleware 中间件。
php artisan make:controller ABCController
app/Http/ABCController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; class ABCController extends Controller { public function index(){ echo "<br>ABC Controller."; } }
app/Http/routes.php
Route::get('terminate',[ 'middleware' => 'terminate', 'uses' => 'ABCController@index', ]);
http://localhost:8000/terminate
上一篇:Laravel路由
下一篇:Laravel控制器