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

aznyouth's avatar

Dynamic prefix routing

Hello Guys !

Just to make clear, I'm a noob in Laravel, my question may sound stupid ahah

I'm currently facing an issue with the prefix routing. I'm not sure if I'm doing it right for the routing, can someone give me some advice ?

Here is the context: My website has 2 front and 1 back. All views will kind of be similar, just some section will disappear. Depending on which urls for the homepage we will have, if the user is not authenticated, it should redirect to the login page.

Urls should be like this:

  • localhost/admin -> localhost/admin/login
  • localhost/front1 -> localhost/front1/login
  • localhost/front2 -> localhost/front2/login

web.php

Route::group([
    'prefix' => 'front1'
], function () {
    AuthRoutes();
    Route::get('/', 'Front1Controller@index')->name('f1.home');
});

Route::group([
    'prefix' => 'front2'
], function () {
    AuthRoutes();
    Route::get('/', 'Front2Controller@index')->name('f2.home');

});

Route::group([
    'prefix' => 'admin',
], function () {
    AuthRoutes();
    Route::get('/', 'AdminController@index')->name('admin.home');

});

//Auth::routes();

function AuthRoutes()
{
    Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
    Route::post('login', 'Auth\LoginController@login');
    Route::post('logout', 'Auth\LoginController@logout')->name('logout');
    Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
    Route::post('register', 'Auth\RegisterController@register');
    Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
    Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
    Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
    Route::post('password/reset', 'Auth\ResetPasswordController@reset')->name('password.update');
    Route::get('email/verify', 'Auth\VerificationController@show')->name('verification.notice');
    Route::get('email/verify/{id}', 'Auth\VerificationController@verify')->name('verification.verify');
    Route::get('email/resend', 'Auth\VerificationController@resend')->name('verification.resend');
}

How do I redirect to the right view ? Here is what happened :

  • localhost/admin -> localhost/admin/login
  • localhost/front1 -> localhost/admin/login
  • localhost/front2 -> localhost/admin/login

No matter which prefix I am at, it will redirect me to the admin/login

How do I fix this ?

Thanks everyone

0 likes
7 replies
munazzil's avatar

Check your this path app\Http\Controllers\Auth\LoginController.php change the redirect route in Logincontroller,

    protected $redirectTo = '/home';
JohnBraun's avatar

edit: This is not a solution, only an explanation of the observed behaviour.

The problem is caused by the fact that you have multiple routes defined called "login". If I may simplify your example, it comes down to this routes file:

Route::group(['prefix' => 'front'], function() {
    authRoutes(); 
    // will evaluate to: 
    // Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
    
    Route::get('/', 'FrontController@index');
});

Route::group(['prefix' => 'admin'], function() {
    authRoutes(); 
    // will evaluate to: 
    // Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
    
    Route::get('/', 'AdminController@index');
});

function authRoutes()
{
    Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
}

Since you have the route called "login" multiple times defined, Laravel will always pick the last defined one.

In the app/Http/Middleware/Authenticate class, this route is returned when not authenticated.

// app/Http/Middleware/Authenticate.php

    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('login');
        }
    }

You can see this in action if you change the order of the Route::group functions. If you put "admin" before "front", now all logins will be redirected to "/front/login".

aznyouth's avatar

This doesn't work for me.

I found a hint to do it cleanly but It doesn't work, I might miss something.

app\Providers\RouteServiceProvider.php

    public function boot()
    {

        parent::boot();

        Route::bind('brand', function ($value) {
            return Brand::where('slug', $value)->first() ?? abort(404);
        });
    }


routes/web.php

use Illuminate\Support\Facades\Auth;
use \App\Brand;

Route::group([
    'prefix' => '{brand}'
], function () {
    AuthRoutes();
    Route::get('/home', function() {
        return view('welcome');
    });
});

In localhost/{brand}/ which should redirect to localhost/{brand}/login

I have this error :

  • Missing required parameters for [Route: login] [URI: {brand}/login]

My database is populated so {brand} should be replaced by for example nike ?

JohnBraun's avatar

@aznyouth "This doesn't work for me."

Sorry, my reply was meant to explain where the problem originates from, not a solution.

To make this system work the way you want, you should return a route based on the user's current location, instead of just returning the route called "login" in the Authenticate middleware.

// app/Http/Middleware/Authenticate.php

    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            // return route('login');
            return $request->url() . '/login';
        }
    }
aznyouth's avatar

I still have the issue with your method.

How do I determine which route it will be on my template ?

<li class="nav-item">
       <a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
</li>
AddWebContribution's avatar

You should try this:

Route::group(['prefix' => 'front'], function() {
    authRoutes(); 
    Route::get('/', 'FrontController@index');
});

Route::group(['prefix' => 'front1'], function() {
    authRoutes(); 
    Route::get('/', 'FrontOneController@index');
});

Route::group(['prefix' => 'admin'], function() {
    authRoutes(); 
    Route::get('/', 'AdminController@index');
});

function authRoutes()
{
    Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
}
aznyouth's avatar

That's what I did before but I thought it was not very clean to do it that way...

I will have like thrice the same route just because of the prefix :(

Please or to participate in this conversation.