Base route (/) under prefix

Published 1 month ago by RinoDrummer

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('/', '[email protected]')->name('panel'); // Added name
            Route::get('/dashboard', '[email protected]')->name('dashboard');

            Route::get('/{controller}/{action?}', '[email protected]')->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!

Best Answer (As Selected By RinoDrummer)
Snapey

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

arukomp

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

RinoDrummer

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

Snapey
Snapey
1 month ago (868,375 XP)

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

@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
Snapey
1 month ago (868,375 XP)

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

RinoDrummer

@Snapey, the problem is that if type the URL http://localhost/admin/ I get the Forbidden error page.

If I go to http://localhost/admin/login and I'm not logged in, it returns the login view; if I'm logged in, I get redirected to http://localhost/admin/dashboard and viceversa.

Snapey
Snapey
1 month ago (868,375 XP)

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

rbur0425

what if you put the routes outside of the middleware -

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

    Route::get('/', '[email protected]')->name('panel'); // Added name
    Route::get('/dashboard', '[email protected]')->name('dashboard');

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

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

see if the middleware is blocking it

RinoDrummer

@Snapey, Yes, I can login without problems.

@rbur0425 It didn't work.

Snapey
Snapey
1 month ago (868,375 XP)

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

Cronix
Cronix
1 month ago (398,210 XP)

What if you don't add the trailing slash when you go to the url? http://localhost/admin

RinoDrummer

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.

RinoDrummer

@Cronix I get redirected to http://localhost/admin/

Snapey
Snapey
1 month ago (868,375 XP)

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

RinoDrummer

@Snapey, that was the problem.

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

Please sign in or create an account to participate in this conversation.