我想检查身份验证以访问名为FoodController的控制器,并且此控制器访问将仅允许一些用户,因此我创建了一个中间件并将其分配给route.php文件上的隐式控制器路由。但它显示错误 -
ErrorException in Router.php line 612:
strpos() expects parameter 1 to be string, array given
代码如下:
Route.php
Route::group(['middleware' => 'auth'],function(){
Route::controller('home','HomeController');
Route::controller('food',['middleware' => 'fm', 'uses' => 'FoodController']);
});
kernel.php
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'fm' => \App\Http\Middleware\FoodAuthentication::class,
];
FoodAuthentication.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class FoodAuthentication
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (Auth::user()->role!=2) {
return redirect('home');
}
return $next($request);
}
}
我该如何解决?
答案 0 :(得分:1)
您无法将数组放到Route :: controller中。只需添加
public function __construct() {
$this->middleware('fm');
}
到你的FoodController类
答案 1 :(得分:0)
解决! 您无法直接在隐式控制器中添加中间件。 请点击以下链接: http://arunkp.in/adding-middleware-in-implicit-controllers/