0

When i install multi-auth in laravel but routing issue occur.

I'm edit in web.php when address bar write in ,

localhost/projectname/admin 

but error occur - page not found but i passed specific address like this

localhost/projectname/admin/login 

after that page open BUT this not good. can you possible i'm only pass

localhost/projectname/admin 

after that page(view) open ?

Middleware

protected $routeMiddleware = [ 'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class, 'admin.guest' => \App\Http\Middleware\RedirectIfAdmin::class, 'auth' => \Illuminate\Auth\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, ]; 

Middleware RedirectifNotAdmin.php controller

<?php namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Auth; class RedirectIfNotAdmin { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param string|null $guard * @return mixed */ public function handle($request, Closure $next, $guard = 'admin') { if (!Auth::guard($guard)->check()) { return redirect('admin/login'); } return $next($request); } } 

my routes

Route::get('/admin', 'AdminController@index'); 

Controller file AdminController

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class HomeController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth:admin'); } /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ public function index() { return view('admin'); } } 
8
  • try Route::get('/admin', 'AdminController@index')->middleware(['auth:admin']) Commented Jul 25, 2018 at 7:42
  • you can see:stackoverflow.com/questions/46270775/… Commented Jul 25, 2018 at 8:12
  • @J.Doe try your code but not working.When i call /admin redirect to /login not /admin/login Commented Jul 25, 2018 at 9:48
  • add your admin middleware code to question Commented Jul 25, 2018 at 9:50
  • 1
    i think that auth middleware run first, try add to RedirectIfNotAdmin class: public function handle($request, Closure $next, $guard = 'admin') { dd('auth middleware') if (!Auth::guard($guard)->check()) { return redirect('admin/login'); } return $next($request); } Commented Jul 25, 2018 at 13:04

1 Answer 1

0

I think here the auth middleware run first and for that reason you can not get your route url.

For solving issue you should add this logic inside RedirectIfNotAdmin::class()

My solution is :

public function handle($request, Closure $next, $guard = 'admin') { if(!Auth::guard($guard)->check()) { return redirect('admin/login'); } return $next($request); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.