简体   繁体   中英

Laravel - All route links are prefixed with sub-domain

I'm using a sub-domain in my application as a dashboard for a user account. In the views of the dashboard, I include a master template which contains links in the navigation that lead to the root of the website. When I visited the sub-domain I've noticed all links in the navigation were changed based on the sub-domain name.

In my master template, links are generated using the helper function route() . When a generated link is viewed on my sub-domain, the link changes from domain.com/about to sub.domain.com/about along with all other links.

Route::group(['domain' => 'dashboard.project.app', 'before' => 'auth'], function() 
{
    Route::controller('/', 'DashboardController');
});

Route::get('/', ['as' => 'home', 'uses' => 'HomeController@index']);

Route::resource('products', 'ProductsController');

Route::resource('support', 'SupportController');

So visiting dashboard.project.app would trigger getIndex() in the DashboardController while visiting project.app would trigger index() in the HomeController.

Great, we've got this far. Now I have a simple view I use as a template that will contain URLs to the named resource routes defined outside of the sub-domain.

Again, URLs are made using the helper function route() . In a template extended by a view called in DashboardController I would have a link in the navigation like:

<a href="{{ route('products.index') }}">Products</a>

which would generate <a href="http://project.app/products">Products</a> as desired however changes to <a href="http://dashboard.project.app/products">Products</a> when shown in a view under the sub-domain.

So I would like the desired output to always be project.app/products and never dashboard.project.app/products as visiting that link will result in a 404 as there is no getProducts() method in my DashboardController. It's the wrong links!

Get what I'm saying?

If the named route is not defined as being specific to a domain, then the url generator will use whatever domain you are currently on. If the route should be specific to a domain, specify it:

Route::group(['domain' => 'project.app'], function() {
    Route::resource('products', 'ProductsController');
    Route::resource('support', 'SupportController');
});

Now, even from your dashboard, route('products.index') should give you project.app/products .

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