Laravel 路由到多域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22827226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:18:00  来源:igfitidea点击:

Laravel Route to multi domain

phplaravellaravel-routing

提问by Wagner Mariotto Bonfiglio

I'm creating a plataform to support multiple websites under the plataform, and some websites needs to have a different domain.

我正在创建一个平台来支持平台下的多个网站,有些网站需要有不同的域。

The most commom is to use the same domain:

最常见的是使用相同的域:

myrootdomain.com/first_product/profile

myrootdomain.com/first_product/profile

myrootdomain.com/second_product/profile

myrootdomain.com/second_product/profile

In these cases, the {first_product} and {second_product} will be passed as argument to almost all functions. /first_product and /second_product are total different websites running under the same plataform.

在这些情况下,{first_product} 和 {second_product} 将作为参数传递给几乎所有函数。/first_product 和 /second_product 是在同一平台下运行的完全不同的网站。

But I need to create another product where I can change the TLD and still be able to identify {anotherrootdomain} as the first parameter to my functions. Something like this:

但是我需要创建另一个产品,我可以在其中更改 TLD 并且仍然能够将 {anotherrootdomain} 标识为我的函数的第一个参数。像这样的东西:

anotherrootdomain.com/profile

anotherrootdomain.com/profile

I'm already handling the first parameter using Route::bind.

我已经在使用 Route::bind 处理第一个参数。

<?php

Route::bind('short_url', function($value, $route)
{
    $product = Product::where('short_url', $value)->first();

    if(is_null($product))
        return false;
    return $product->id;
});

Route::get('/{short_url}/login', 'HomeController@login');
Route::get('/{short_url}/profile', 'UserController@profile');

Now I don't know how to:

现在我不知道如何:

1) look for domain

1)寻找域名

2) use the domain as the first parameter

2)使用域作为第一个参数

I know my english is terrible, but I hope you can help me!

我知道我的英语很糟糕,但我希望你能帮助我!



EDIT:

编辑:

I can do something like this on routes.php:

我可以在 routes.php 上做这样的事情:

if($_SERVER['HTTP_HOST'] == "anotherrootdomain.com")
    $tld = "anotherrootdomain";

Then I need to chance from this:

然后我需要从这个机会:

Route::get('/{short_url}/login', 'HomeController@login');
Route::get('/{short_url}/profile', 'UserController@profile');

To this:

对此:

Route::get('/login', 'HomeController@login');
Route::get('/profile', 'UserController@profile');

And I can do it using a simple if, but now I can't pass the parameter on my routes! HomeController@login expects a parameter, and I need to pass $tld variable, but I don't know how!

我可以使用一个简单的 if 来做到这一点,但现在我无法在我的路线上传递参数!HomeController@login 需要一个参数,我需要传递 $tld 变量,但我不知道怎么做!

采纳答案by Wagner Mariotto Bonfiglio

I realized a way to do it!

我想到了一个办法!

Let's say that my /second_product on the example needs to be accesible using another TLD, like "awesomeproduct.com". Some URLs need to chance:

假设示例中的 /second_product 需要使用另一个 TLD 进行访问,例如“awesomeproduct.com”。一些 URL 需要偶然:

myrootdomain.com/second_product/login => awesomeproduct.com/login

myrootdomain.com/second_product/login => awesomeproduct.com/login

myrootdomain.com/second_product/profile => awesomeproduct.com/profile

myrootdomain.com/second_product/profile => awesomeproduct.com/profile

While the first_product still just working the first way:

虽然 first_product 仍然只是以第一种方式工作:

myrootdomain.com/first_product/login

myrootdomain.com/first_product/login

myrootdomain.com/first_product/profile

myrootdomain.com/first_product/profile

To make it possible, I created a route group with sub-domain (I didn't know that I could also use wildcard on TLD):

为了使它成为可能,我创建了一个带有子域的路由组(我不知道我也可以在 TLD 上使用通配符):

Route::group(array('domain' => '{short_url}.com'), function($short_url)

And I changed a little bit my Route::bind to change the {short_url} parameter:

我稍微改变了我的 Route::bind 来改变 {short_url} 参数:

Route::bind('short_url', function($value, $route)
{
    if($value == "awesomeproduct")
        $value = "second_product";

    $product = Product::where('short_url', $value)->first();

    if(is_null($product))
        return false;
    return $product->id;
});

Finally, I need to change my routes to use or not the {short_url}:

最后,我需要更改我的路线以使用或不使用 {short_url}:

$tld = "";
if($_SERVER['HTTP_HOST'] != "testefacop.com")
    $tld = "{short_url}/";

Route::get('/'.$tld.'login', 'HomeController@login');
Route::get('/'.$tld.'profile', 'UserController@profile');

And here is my complete routes.php:

这是我完整的routes.php:

<?php

Route::bind('short_url', function($value, $route)
{
    if($value == "awesomeproduct")
        $value = "second_product";

    $product = Product::where('short_url', $value)->first();

    if(is_null($product))
        return false;
    return $product->id;
});

Route::group(array('domain' => '{short_url}.com'), function($short_url)
{
    $tld = "";
    if($_SERVER['HTTP_HOST'] != "awesomeproduct.com")
        $tld = "{short_url}/";

    Route::get('/'.$tld.'login', 'HomeController@login');
    Route::get('/'.$tld.'profile', 'UserController@profile');

}

It isn't a great and beatifull solution, but it's working as expected!

这不是一个伟大而美丽的解决方案,但它按预期工作!