Sign Up Form

Sign Up

How to get a link with subdomain either using url() helper or route() helper in Laravel?

310 163 point-admin
  • 0

I have set up my subdomain domain routes like this:-

Route::group(['domain' => '{subdomain}.' . env('APP_URL'), 'prefix' => 'console', 'namespace' => 'admin', 'middleware' => 'subdomain'], function () {
   Route::get('/login', 'LoginController@index')->name('admin.login');
});

And main domain routes like this-

Route::group(['prefix' => 'console', 'namespace' => 'admin', 'middleware' => 'maindomain'], function () {
    Route::get('/login', 'LoginController@index')->name('admin.login');
});

Now in an email, I want to send the login url of the subdomain.

  1. If I simply write route('admin.login') it will give me the login url of the main domain.
  2. url() . 'console/login' will also give me the login url of the main domain.
  • For Subdomain Routes:
Route::group([
    'domain' => '{subdomain}.' . env('APP_URL'),
    'prefix' => 'console',
    'namespace' => 'admin',
    'middleware' => 'subdomain'
], function () {
    Route::get('/login', 'LoginController@index')->name('admin.login');
})->name('subdomain.');
  • For Main Domain Routes:
Route::group([
    'prefix' => 'console',
    'namespace' => 'admin',
    'middleware' => 'maindomain'
], function () {
    Route::get('/login', 'LoginController@index')->name('admin.login');
})->name('domain.');

Resulting Route Names

With these definitions, your routes will be:

  • Subdomain Login Route:
route('subdomain.admin.login')
  • Main Domain Login Route:
route('domain.admin.login')

By naming the route groups, you ensure that both login routes have unique names, preventing any naming conflicts.

Discover more questions here LARAVEL

Leave a Reply

Your email address will not be published.