I'm new to Laravel 5 middleware - and I just recently added a new user type
== Dev
into my application. I need to set a certain route restriction for that user type - in term of what they can/can't access.
So I create
<?php
namespace App\Http\Middleware;
use App\Article;
use Closure, View ;
use Illuminate\Contracts\Auth\Guard;
class DevMiddleware
{
/**
* The Guard implementation.
*
* @var Guard
*/
protected $auth;
/**
* Create a new filter instance.
*
* @param Guard $auth
* @return void
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($this->auth->getUser()->type !== "Dev") {
return View::make('errors.404');
}
return $next($request);
}
}
I registered it in my
'dev' => 'App\Http\Middleware\DevMiddleware',
I use it in my
$router->group(['middleware' => ['auth', 'admin', 'dev' ] ], function() {
//Any routes goes in here
});
When I go those route in there - keep getting 404 error.
I want to be able to go to those routes.
What did I do wrong here ? How do I fix it ?
Are you using the same logic for the other middleware? If so then the logic would act like an OR statement as in if 'auth' fails, or 'admin' fails, or 'dev' fails, then 404.
I am not sure of the "best" way to handle what you want but I have an example from an open source project; laravel-5-boilerplate:
Route::group([
'middleware' => 'access.routeNeedsRole',
'role' => ['admin', 'dev'],
'redirect' => '/',
'with' => ['flash_danger', 'You do not have access to do that.']
], function ()
{
//Any routes goes in here
});
You have 3 middlewares acting there, but you are assuming the DEV one is failing. Paste the content of the other 2. Also, you are using:
$this->auth->getUser()->type
What does that do? If the user is admin it may be failing at the DEV middleware (or the other way around).
P.S: I'd recommend Entrust Package if you need to deal with roles.