Support the ongoing development of Laravel.io →
Configuration Requests

Hi everyone,

I ran into an issue with subdomain routing configuration. Here is what I am trying to accomplish:

  • ALL controllers MUST GET subdomain as a parameter

  • ALL subdomains MUST SHARE the same controllers

  • example of my domain & subdomains: xyz.com, a.xyz.com, b.xyz.com, c.xyz.com...

My routes seem to be working fine.

##PROBLEM: when I try to generate links in my templates/blades, the subdomain is not picked up by helpers, etc.

##EXAMPLE: if I go to a.xyz.com, the routing seem to work, but the code below (in my blade file) fails to generate URL with proper subdomain:

<a href="{{URL::route('account-signin')}}">Sign-in</a>

The URL::route('account-signin') outputs: %7Bsubdom%7D.xyz.com INSTEAD OF: a.xyz.com.

##QUESTION: how can I make URL::route('some-route-name') to generate a.xyz.com instead of %7Bsubdom%7D.xyz.com?

Any help is appreciated.

I include my routes.php code below:

<?php ~~~ /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the Closure to execute when that URI is requested. | */ Route::group(array('domain' => '{subdom}.xyz.com'), function($subdom) { Route::get('/', array( 'as' => 'home', /*name route. we will use it later to link to homepage*/ 'uses' => 'HomeController@home' /*direct it to controller*/ )); /* * This profile can be accessed publicly (move to Authenticated group to make it private */ Route::get('/user/{username}', array( 'as' => 'profile-user', 'uses' => 'ProfileController@user' )); /* * Authenticated group */ Route::group(array('before' => 'auth'), function(){ /* * Cross site request forgerty protection */ Route::group(array('before' => 'csrf'), function(){ /* * Change password (post) */ Route::post('/account/change-password', array( 'as' => 'account-change-password-post', 'uses' => 'AccountController@postChangePassword' )); }); /* * Change password (get) */ Route::get('/account/change-password', array( 'as' => 'account-change-password', 'uses' => 'AccountController@getChangePassword' )); /* * signout (get) */ Route::get('/account/signout', array( 'as' => 'account-signout', 'uses' => 'AccountController@getSignOut' )); }); /* * Unauthenticated group */ Route::group(array('before' => 'guest'), function(){ /* * Cross site request forgerty protection */ Route::group(array('before' => 'csrf'), function(){ /* * Obtain Signin form data (post) */ Route::post('/account/signin', array( 'as' => 'account-signin-post', 'uses' => 'AccountController@postSignIn' )); /* * Create account (post) */ Route::post('/account/create', array( 'as' => 'account-create-post', 'uses' => 'AccountController@postCreate' )); /* * Forgot password (post) */ Route::post('/account/forgot-password', array( 'as' => 'account-forgot-password-post', 'uses' => 'AccountController@postForgotPassword' )); }); /* * Forgot password (get) */ Route::get('/account/forgot-password', array( 'as' => 'account-forgot-password', 'uses' => 'AccountController@getForgotPassword' )); Route::get('/account/recover/{code}', array( 'as' => 'account-recover', 'uses' => 'AccountController@getRecover' )); /* * Display signin to account page */ Route::get('/account/signin', array( 'as' => 'account-signin', 'uses' => 'AccountController@getSignIn' )); /* * Create account (get) */ Route::get('/account/create', array( 'as' => 'account-create', 'uses' => 'AccountController@getCreate' )); /* * Activate account (get) */ Route::get('/account/activate/{Code}', array( 'as' => 'account-activate', 'uses' => 'AccountController@getActivate' )); }); /* Route::get('/', function() { var_dump(App::environment()); return View::make('hello'); }); */ Route::get('users', function() { return 'Users!'; }); }); ~~~
Last updated 2 years ago.
0

Hi, when a route has parameters, you can enter them like below, each key is the parameter name of your route parameter. http://laravel.com/docs/helpers#urls

URL::route('routename', array('subdom' => 'your-subdomain'))

Last updated 2 years ago.
0

Edwin-Luijten said:

Hi, when a route has parameters, you can enter them like below, each key is the parameter name of your route parameter. http://laravel.com/docs/helpers#urls

URL::route('routename', array('subdom' => 'your-subdomain'))

Hi Edwin, thank you for your reply. Your solution WORKS, but...

Question 1:

Is there a way to make URL::route('routename') to include subdomain automatically?

If I use your approach, then I have to include the 2nd parameter in all of my blades/templates for all links

(Example: <li><a href="{{ URL::route('routename', $subdom) }}">some link</a></li>

In addition, I have to pass $subdom to all Controller functions in my project such as:

class HomeController extends BaseController {

	public function home($subdom){
            
            return View::make('home')
                    ->with('subdom', $subdom);
	}
}

Question 2

Is there any way to avoid the passing of $subdom parameter to all Controller functions?

My apologies for asking dumb questions, I just started with Laravel...

Last updated 2 years ago.
0

I would suggest using a intermediary route function where the subdomain will be set by default. This way you won't have to give the subdomain parameter for each route. The downside is you will have to find/replace the current used route() calls to this custom one. Something like this should do the trick:


function subRoute($route, $params = array())
{
     // retrieve current subdomain if none passed
     if(!isset($params['subdomain'])
     { 
          $params['subdomain'] = array_shift(explode(".",Request::server('HTTP_HOST'))); 
     }

     return route($route,$params);
}

Last updated 2 years ago.
0

BenCavens said:

I would suggest using a intermediary route function where the subdomain will be set by default. This way you won't have to give the subdomain parameter for each route. The downside is you will have to find/replace the current used route() calls to this custom one. Something like this should do the trick:


function subRoute($route, $params = array())
{
    // retrieve current subdomain if none passed
    if(!isset($params['subdomain'])
    { 
         $params['subdomain'] = array_shift(explode(".",Request::server('HTTP_HOST'))); 
    }

    return route($route,$params);
}

Ben, thank you for the reply. This function will still add an extra layer of maintenance because we would not be calling builtin URL::route(...) method in our templates/blades.

Is it possible to override the URL::route()?

Thanks again to everyone for trying to help the Laravel beginner.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.