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

RinoDrummer's avatar

Base route (/) under prefix

Hi all!

I'm having problems when I try to reach the base route of the admin panel I'm building.

Suppose that I have it on: http://localhost/admin/

When I type that URL, I get a Forbidden error page. That doesn't happen when I visit the Auth routes, the dashboard route and general one for controller/action.

I use Laravel 5.5.

This is the interesting code fragment of my web.php:

Route::prefix('admin')->group(function () {
    Auth::routes();

    Route::middleware(['auth'])->group(function () {
        Route::namespace('Admin')->group(function () {
            Route::get('/', 'PageController@dashboard')->name('panel'); // Added name
            Route::get('/dashboard', 'PageController@dashboard')->name('dashboard');

            Route::get('/{controller}/{action?}', 'PageController@action')->where([
                'controller' => '[a-z]+(?:-[a-z]+)*',
                'action' => '[a-z]+(?:-[a-z]+)*'
            ]);
        });
    });
});

This is a snippet of Admin\PageController.php:

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use Auth;
use \App\Data;

class PageController extends Controller
{
    public function dashboard() {
        if (Auth::check()) {
            $data = Data::all();

            return view('admin/dashboard')->with('data', $data);
        }

        return redirect()->route('login');
    }

    public function action($controller, $action = 'index') {

        if (Auth::check()) {
            return studly_case('controller') . 'Controller' . '@' . $action;
        }

        return redirect()->route('login');
    }
}

And this is the output of php artisan route:list: Laravel routes list

Thanks in advance!

0 likes
17 replies
arukomp's avatar

type php artisan route:list in the console and see if that gives you any indication about what's wrong

RinoDrummer's avatar

@arukomp, I have updated the question with more details, can you please give a better look?

Snapey's avatar

If you are not logged in then middleware will redirect you to the login page. I wonder if that is redirecting you to /login instead if /admin/login?

You can check this by opening the browser network tools and seeing if any 302 redirect occurs

RinoDrummer's avatar

@Snapey the login URL works perfectly.

The login route is defined only for the admin prefix.

But the route named "panel" doesn't work.

Snapey's avatar

not sure what you mean? what works perfectly? Does that mean you have no problem any more

Snapey's avatar

your auth middleware prevents you from getting to the admin route, so, are you logging in ok?

rbur0425's avatar

what if you put the routes outside of the middleware -

Route::prefix('admin')->group(function () {
    Auth::routes();

    Route::get('/', 'PageController@dashboard')->name('panel'); // Added name
    Route::get('/dashboard', 'PageController@dashboard')->name('dashboard');

    Route::middleware(['auth'])->group(function () {
        Route::namespace('Admin')->group(function () {
            

            Route::get('/{controller}/{action?}', 'PageController@action')->where([
                'controller' => '[a-z]+(?:-[a-z]+)*',
                'action' => '[a-z]+(?:-[a-z]+)*'
            ]);
        });
    });
});'

see if the middleware is blocking it

Snapey's avatar

Bear with me.. how do you know you are logged in?

RinoDrummer's avatar

Supposing that I'm not logged in: If I visit http://localhost/admin/dashboard it redirects me to http://localhost/admin/login.

Continuing to suppose that I'm not logged in, if I visit http://localhost/admin/login it properly shows the login form and once I compile and send it, I get redirected to http://localhost/admin/dashboard. From that I understand that login works.

The problem is when I try to reach the panel from the URL: http://localhost/admin. It should take me to http://localhost/admin/login if I'm not logged in, and to http://localhost/admin/dashboard if I'm logged.

Snapey's avatar
Snapey
Best Answer
Level 122

just check that you dont have a folder called admin in your public folder

RinoDrummer's avatar

@Snapey, that was the problem.

Is there a way to not rename the 'admin' folder in public without changing the admin route?

Snapey's avatar

i dont think so, because the webserver itself sees the folder and therefore does not rewrite the url to index.php.

You would somehow need to change the .htaccess but this would probably then block use of /admin routes in laravel

Please or to participate in this conversation.