Home > Backend Development > PHP Tutorial > laravel5 的中间件的使用 (1)

laravel5 的中间件的使用 (1)

WBOY
Release: 2016-06-23 13:13:08
Original
954 people have browsed it

laravel5 的中间件的使用 (1)

1.创建中间件

  php artisan make:middleware _Name_Middleware

 

2.注册中间件

  2.1 中间件注册在 Kennel.php 里面

  2.2 默认的存放在此处,会自动调用

     

/** * The application's global HTTP middleware stack. * * These middleware are run during every request to your application. * * @var array */protected $middleware = [    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,  #可在此处进行注册 如<br />
Copy after login
   'App\Http\Middleware\AfterMiddleware',
Copy after login
<br />];
Copy after login

  2.3 还有一种触发的方式,就是 从Route进行触发的

  2.3.1 首先需要注册他,即

  

    /**     * The application's route middleware.     *     * These middleware may be assigned to groups or used individually.     *     * @var array     */    protected $routeMiddleware = [        'auth' => \App\Http\Middleware\Authenticate::class,        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,        #注册一个 中间件        'demo.before'  => 'App\Http\Middleware\BeforeMiddleware',    ];
Copy after login

他的使用和默认的有所不同,他想要在 router.php 中 使用 Router:: 的时候触发

<?phpRoute::group(['middleware' => ['web']], function () {    Route::get('/', function () {        return view('welcome');    });    Route::group(['prefix' => 'demo'],function(){
Copy after login
          Route::get('after', ['as'=>'home.after.ware' , 'uses' => 'HomeController@after']);
Copy after login
 }); });
Copy after login

此处的代码是这样子的,当我们 访问的时候,他便会 进行自动使用中间件

3. 主要说说第二种方式的触发,可以看到

Kernel.php 处的 middleware 的 demo.before

'demo.before'  => 'App\Http\Middleware\BeforeMiddleware',<br /><br /><br />与 Router.php 处的 路由 demo --> before 对应
Copy after login
Route::group(['prefix' => 'demo'],function(){<br />    Route::get('before', ['as'=>'home.before.ware' , 'uses' => 'HomeController@before']);<br />});
Copy after login

 

OK,写的不好之处,往指正

 

 

 

 

 

     

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template