Remove subdomain from urls with Laravel 4

I am using subdomain routing with laravel 4.2 app. Here's my routes file:

Route::group(array('domain' => '{keyword}.example.com'), function() {
    Route::get('/', 'FrontendHomeController@index');
});

Route::get('/', ['uses' => 'FrontendHomeController@index', 'as' => 'home']);
Route::get('hotels', ['uses' => 'FrontendHotelController@index', 'as' => 'hotelsearch']);
Route::get('hotel/{slug}/{id}', ['uses' => 'FrontendHotelController@detail', 'as' => 'hoteldetail']);
[...]

      

So, I have a few pages that use subdomains such as keyword.example.com

, another.example.com

. But most of the other pages are regular example.com/page

URLs. So far, that's fine. I am creating links to navigate using Laravel / route url helpers, eg. {{ url('hotels') }}

or {{ route('hotelsearch') }}

.

Now, on the subdomain pages, the generated url contains the subdomain. For example. at keyword.example.com {{ url('hotels') }}

generates keyword.example.com/hotels

instead of example.com/hotels

.

I would like to remove subdomains for all links generated with helpers url()

or route()

, these helpers should always point to the root domain.

Is there a parameter or do I need to rewrite the helper methods in some way?

+3


source to share


1 answer


The function url

in templates wraps Illuminate\Routing\UrlGenerator::to

, which prevents you from specifying the domain, but you can use the same technique Route::group

to encapsulate all of the main domain routes. (This does not require a template as shown in the documentation.)



Route::group(array('domain' => 'www.example.com'), function() {
    Route::get('hotels', ['uses' => 'FrontendHotelController@index', 'as' => 'hotelsearch']);
    // ...
});

      

0


source







All Articles