Goran Siriev
Goran Siriev

Reputation: 703

Laravel routes for admin and users

i have 2 logins pages on my project

1) cms/admin/login

2) cms/users/login

How to redirect if user to user login page cms/users/login

and if admin call cms/admin/ redirect to cms/admin/login page

Upvotes: 2

Views: 3482

Answers (1)

KuKeC
KuKeC

Reputation: 4620

First of all, you don't know if user is user or admin until he logs into your app, so having 2 different routes for same thing is kinda bad. To achieve something similar what you want, you will need to have one cms/login route where user/admin will login and depending on his status (e.g. 1 - user, 2 - admin) you redirect him on cms/user/page or cms/admin/page. To make this you will have to use Middleware which is very good documented in Laravel official documentation.

For example your middleware for all admin pages should look like this

<?php

namespace App\Http\Middleware;

use Closure;

class AdminMiddleware
{
    /**
     * Run the request filter.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->input('status') == 2) {
        //2 means its admin and i let him get that admin page
           return $next($request);
        }
        //he is not admin so i redirect him back
        return Redirect::back();            
    }    
}

In Kernel.php you add middleware alias

protected $routeMiddleware = [        
        'admin' => \App\Http\Middleware\AdminMiddleware::class,
    ];

And in routes.php you assign middleware to that routes

Route::get('/cms/admin/page', ['middleware' => 'admin', 'uses'=>'Controller@method']);

Hope it helps

Upvotes: 3

Related Questions