Reputation: 93
I want to check an authentication to access a controller named FoodController, and this controller access will allow only some users, therefore I create a middleware and assign it to the implicit controller routing on route.php file. But it shows an error -
ErrorException in Router.php line 612:
strpos() expects parameter 1 to be string, array given
The codes are given below:
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);
}
}
How can I fix it?
Upvotes: 1
Views: 1078
Reputation: 1
Resolved! You cannot add middleware directly in implicit controllers. Follow the below link: http://arunkp.in/adding-middleware-in-implicit-controllers/
Upvotes: 0
Reputation: 1386
You cant put an array to Route::controller. Just add
public function __construct() {
$this->middleware('fm');
}
to your FoodController class
Upvotes: 1