简体   繁体   中英

Laravel 5.1 adding middleware to resource route

so I have been trying to use middleware with my route resource and having trouble making it work.

Here is my routes setup:

Route::group(['prefix' => 'api','middleware' => 'locationRouteValidator'], function()
{
    Route::resource('location', 'LocationController');
});

and route seems to be setup properly:

 php artisan route:list +--------+----------+------------------------------+----------------------+-------------------------------------------------+------------------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+----------+------------------------------+----------------------+-------------------------------------------------+------------------------+ | | GET|HEAD | / | | Closure | | | | GET|HEAD | api/location | api.location.index | App\\Http\\Controllers\\LocationController@index | locationRouteValidator | | | POST | api/location | api.location.store | App\\Http\\Controllers\\LocationController@store | locationRouteValidator | | | GET|HEAD | api/location/create | api.location.create | App\\Http\\Controllers\\LocationController@create | locationRouteValidator | | | DELETE | api/location/{location} | api.location.destroy | App\\Http\\Controllers\\LocationController@destroy | locationRouteValidator | | | PATCH | api/location/{location} | | App\\Http\\Controllers\\LocationController@update | locationRouteValidator | | | GET|HEAD | api/location/{location} | api.location.show | App\\Http\\Controllers\\LocationController@show | locationRouteValidator | | | PUT | api/location/{location} | api.location.update | App\\Http\\Controllers\\LocationController@update | locationRouteValidator | | | GET|HEAD | api/location/{location}/edit | api.location.edit | App\\Http\\Controllers\\LocationController@edit | locationRouteValidator | +--------+----------+------------------------------+----------------------+-------------------------------------------------+------------------------+ 

so now I create the middleware :

php artisan make:middleware locationRouteValidator

and leave the default code, which is :

public function handle($request, Closure $next)
{
    return $next($request);
}

and just for testing, in my controllers show method, I echo out the passed id like so:

public function show($id)
{
    //
    echo "show ".$id;
}

so now I expect that when I visit /public/api/location/abcd it should display: show abcd or when I visit /public/api/location/1234 it should display show 1234 after which I intended to modify the middleware to allow only numeric values to be passed into {location} .

But If I just run with the default middleware code, the page returns white without displaying anything. I remove the middleware from the route, and it displays the text as expected.

I know I could attach the middleware to the controller instead, but I thought of attaching it in the route instead so that I could write and apply some common middleware by using the route's group feature, which should be possible, right?

Where do you guys think I am going wrong? Thanks in advance for looking!

检查您的\\ app \\ http \\ kernel.php文件,以查看是否已将中间件注册为路由中间件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM