Laravel / Host Setup for Multi-TLD

天大地大妈咪最大 提交于 2019-12-02 10:20:34

Thanks for the replies guys, this is what i ended up doing... just putting the routes into variable and passing it to route groups, seems to work for me

$frontendRoutes = function()
{
    Route::get('/', function(){
        return 'Index Page';
    })
};

Route::group(['domain' => 'example.net'], $frontendRoutes);
Route::group(['domain' => 'example.info'], $frontendRoutes);

This seems to work but the php artisan routes looks a bit wrong.

http://ryantablada.com/post/multi-tld-routing-in-laravel

I was having a problem having the code pick up the last tld in the list. This, although not perfect, seems to be the best answer I have found so far.

This might be a crazy hack, but as I ended up with the same issue and I wanted to have two tld's pointing to my site and then serve a different language depending on what tld they were coming from I made this solution.

I created a folder called Routes in the app folder.

I then added two folders in the the Routes folder, one named "com" and one named "se".

And in each folder I added the files I wanted to use depending on what language. in the "com" folder I made a file called pages.php with the code:

Route::get('/', 'PagesController@index');

Route::get('about', [
    'as' => 'about',
    'uses' => 'PagesController@about'
]);

And then I added a file in the "se" folder also named pages.php with the code:

Route::get('/', 'PagesController@index');

Route::get('om-oss', [
    'as' => 'about',
    'uses' => 'PagesController@about'
]);

Then back at the routes.php I added this code:

$tld = strrchr ( $_SERVER['SERVER_NAME'], "." );
$tld = substr ( $tld, 1 );

foreach (File::allFiles(__DIR__.'\Routes\/'.$tld) as $partial) 
{
require_once $partial->getPathname();
}

So now I have one folder for each tld, not the best solution but it works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!