Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ethar's avatar
Level 5

New Route File Laravel 8

I want to create a new route file for admins.

in routeServiceProvider.php file

i create this new route inside boot function

    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));

            Route::middleware('admin')
                ->namespace($this->namespace)
                ->group(base_path('routes/admin.php'));

            Route::prefix('api')
                ->namespace($this->namespace)
                ->middleware('api')
                ->group(base_path('routes/api.php'));
        });
    }

this code i add


            Route::middleware('admin')
                ->namespace($this->namespace)
                ->group(base_path('routes/admin.php'));

in routes folder i create admin.php for routing

but i got this error

Illuminate\Contracts\Container\BindingResolutionException Target class [admin] does not exist.

not see new route file admin.php

0 likes
7 replies
ismaile's avatar

Since you are using a middleware for admin : Route::middleware('admin'), you'd need to add admin to the $middlewareGroups array in app\Http\Kernel.php:

'admin' => []

The error should not appear any more but since you apply the admin middleware to a group of routes, you shouldn't use an empty array and rather put some middlewares in that array.

3 likes
ethar's avatar
Level 5

thanxs @ismaile

But i got this error

InvalidArgumentException Auth driver [eloquent] for guard [admin] is not defined.

ismaile's avatar
ismaile
Best Answer
Level 30

@ethar You're welcome.

How does your auth.php look like ?

ethar's avatar
Level 5

thanks @ismaile im sorry for late


my auth.php // i add admin guards and providers
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],

        'admin' => [
            'driver' => 'session',
            'provider' => 'admin',
        ],

    ],

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],

        'admin' => [
            'driver' => 'eloquent',
            'model' => App\Models\Admins::class,
        ],

in redirectifauthenticated.php


    public function handle($request, Closure $next, ...$guards)
    {
        $guards = empty($guards) ? [null] : $guards;
        if (Auth::guard($guards)->check()) {
            if($guards == 'admin')
                return redirect(RouteServiceProvider::ADMIN);
            else
                return redirect(RouteServiceProvider::HOME);
        }
        return $next($request);
    }

in routeserviceprovider.php


    public const HOME = '/home';
    public const ADMIN = '/admin/dashboard';
    protected $namespace='App\Http\Controllers';

    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));

            Route::middleware('admin')
                ->namespace($this->namespace)
                ->group(base_path('routes/admin.php'));

            Route::prefix('api')
                ->namespace($this->namespace)
                ->middleware('api')
                ->group(base_path('routes/api.php'));
        });
    }

admin.php //new route file


Route::group(['prefix'=>'admin','middleware'=>'guest:admin','namespace'=>'Admin'],function(){
    Route::get('/login','LoginController@getAdminPage')->name('admin.login');
    Route::post('/login','LoginController@login')->name('admin.login');
});

new error ErrorException Illegal offset type

ethar's avatar
Level 5

@ismaile after change


    public function handle($request, Closure $next, ...$guards)
    {
        $guards = empty($guards) ? [null] : $guards;
        if (Auth::guard($guards)->check()) {
            if($guards == 'admin')
                return redirect(RouteServiceProvider::ADMIN);
            else
                return redirect(RouteServiceProvider::HOME);
        }
        return $next($request);
    }

to


    public function handle($request, Closure $next,$guards=null)
    {
        if (Auth::guard($guards)->check()) {
            if($guards == 'admin')
                return redirect(RouteServiceProvider::ADMIN);
            else
                return redirect(RouteServiceProvider::HOME);
        }
        return $next($request);
    }

problem solved

but i dont think this is best idea

ismaile's avatar

@ethar

Before moving forward, if you don't mind, let's step back a little bit.

How are your admins authenticated ? Is it through the usual login form used by other users ? If that's the case, then maybe there is no need for a dedicated guard for admins.

Besides, what are the fields of your admins table ? Usually, the users table could have a is_admin or admin_at column. In this case, the admins table and its App\Models\Admin model are not necessary.

Maybe what you need is just using the web guard but with authorization to limit access to admin only for specific routes on your site.

Please let me know if that's the case.

ethar's avatar
Level 5

@ismaile let me explain

I used the admin route for the control panel /admin/dashboard /admin/login

I create an admin.php file inside the routes folder used for the admin panel route, all admin route(admin panel) not in web.php but in admin.php I created.

I used a dedicated table for admins, not a user's table.

admins have a login page,different from users page users have a specific login page. admins have a specific login page.

now the problem solved after your help and after the change

   public function handle($request, Closure $next,$guards=null)
    {
        if (Auth::guard($guards)->check()) {
            if($guards == 'admin')
                return redirect(RouteServiceProvider::ADMIN);
            else
                return redirect(RouteServiceProvider::HOME);
        }
        return $next($request);
    }

Please or to participate in this conversation.