aznyouth
3 months ago
166
7
Laravel

Dynamic prefix routing

Posted 3 months ago by aznyouth

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('/', '[email protected]')->name('f1.home');
});

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

});

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

});

//Auth::routes();

function AuthRoutes()
{
    Route::get('login', 'Auth\[email protected]')->name('login');
    Route::post('login', 'Auth\[email protected]');
    Route::post('logout', 'Auth\[email protected]')->name('logout');
    Route::get('register', 'Auth\[email protected]')->name('register');
    Route::post('register', 'Auth\[email protected]');
    Route::get('password/reset', 'Auth\[email protected]')->name('password.request');
    Route::post('password/email', 'Auth\[email protected]')->name('password.email');
    Route::get('password/reset/{token}', 'Auth\[email protected]')->name('password.reset');
    Route::post('password/reset', 'Auth\[email protected]')->name('password.update');
    Route::get('email/verify', 'Auth\[email protected]')->name('verification.notice');
    Route::get('email/verify/{id}', 'Auth\[email protected]')->name('verification.verify');
    Route::get('email/resend', 'Auth\[email protected]')->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

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