简体   繁体   中英

Laravel 5 generate subdomain aware URL

I am trying to create an application that will have about 4 sub domains and one main domain ie www domain.
Now let's assume i am in http://subdomain1.example.com and i want to generate a URL to a product like this http://subdomain2.example.com/product/id
{{ url('product/id') }} produces

http://subdomain1.example.com/product/id

I want it to be

http://subdomain2.example.com/product/id

This is what i have in the routes.php

Route::group(['domain' => '{subdomain}.{domain}.{tld}'], function () {
        // routes
});

I guess you will have to do this using named routes. something like this -

routes.php -

Route::group(['domain' => 'subdomain2.example.com'], function () {
        Route::get('product/{id}', ['as' => 'product', function ($id) {
            //probably would want to put this under controller 
        }]);
});

and to get the url you would do - route('product', ['id' => 1]);

Hy, you can use this one:

routes.php

Route::group(['domain' => '{subdomain}.domain.com'], function () {
    Route::get('/', function(){
        ...
    })->name('somename'); 
});

In your view:

{{ route('somename', 'subdomain-as-param') }}

It will make an url:

http://subdomain-as-param.domain.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM