How does Laravel's Route pass parameters in the url to middleware?
大家讲道理
大家讲道理 2017-05-16 16:55:18
0
2
560

Such as
Route::group(['prefix'=>'code/{code}','middleware'=>'code'],function(){

});

How can $code be transferred to middleware~
Thank you!

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
Ty80
$request->code 
我想大声告诉你

Middleware parameters

Middleware can also receive additional custom parameters. For example, if your app needs to verify that the user has a given "role" before executing an action, you can create a RoleMiddleware middleware that accepts the role name as an extra parameter.

Extra parameters of the middleware will be passed in after the $next parameter:

<?php

namespace App\Http\Middleware;

use Closure;

class RoleMiddleware
{
    /**
     * Run the request filter.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string  $role
     * @return mixed
     */
    public function handle($request, Closure $next, $role)
    {
        if (! $request->user()->hasRole($role)) {
            // Redirect...
        }

        return $next($request);
    }

}

Middleware parameters can be specified by separating the name and parameters of the middleware with : when defining routes. Multiple parameters should be separated by commas:

Route::put('post/{id}', ['middleware' => 'role:editor', function ($id) {
    //
}]);

For details, please refer to: http://www.golaravel.com/laravel/docs/5.1/middleware/#middleware-parameters

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!