任何域上的 Laravel 5 子域路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31376592/
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
Laravel 5 subdomain routing on any domain
提问by YomY
In a laravel 5 application, I'm trying to make a route for subdomain without knowing the domain.
在 laravel 5 应用程序中,我试图在不知道域的情况下为子域创建路由。
Route::group(array('domain' => 'subdomain.domain.tld'), function() {
Route::get('/', 'testController@getTest2');
});
Route::get('/', 'testController@getTest1');
This kind of routing works, and I get getTest2() called for subdomain and getTest1() for calling without subdomain.
这种路由工作,我得到 getTest2() 调用子域和 getTest1() 调用没有子域。
Now, I'd like this to work with wildcard domain, but without sending parameters to controller, so the application in dev enviroinment can be on any domain. (I also considered using .env for storing domain, but that seems too much hassle for just routing)
现在,我希望它与通配符域一起使用,但不向控制器发送参数,因此开发环境中的应用程序可以在任何域上。(我也考虑过使用 .env 来存储域,但这对于路由来说似乎太麻烦了)
I've tried
我试过了
array('domain' => 'subdomain.{domain}.{tld}')
Which requires parameters on controller methods. and I've tried
这需要控制器方法的参数。我试过了
array('domain' => 'subdomain.{domain?}.{tld?}')
Which doesn't require parameters, but sends them anyway, so my actual route parameters get shifted.
这不需要参数,但无论如何都会发送它们,所以我的实际路由参数会发生变化。
I've also seen http://laravel-tricks.com/tricks/dynamic-subdomain-routing, but I don't like the idea of handling my domains in filters.
我也看过http://laravel-tricks.com/tricks/dynamic-subdomain-routing,但我不喜欢在过滤器中处理我的域的想法。
Is there any other way to have a wildcard domain that will be ignored once route group is handled?
有没有其他方法可以让通配符域在处理路由组后被忽略?
回答by jpcamara
The best way i've found to do this is to create a middleware that removes specific parameters from your route. For instance, in your case:
我发现做到这一点的最佳方法是创建一个中间件,从您的路线中删除特定参数。例如,在您的情况下:
class Subdomain {
public function handle($request, Closure $next)
{
$route = $request->route();
$domain = $route->parameter('domain');
$tld = $route->parameter('tld');
//do something with your params
$route->forgetParameter('domain');
$route->forgetParameter('tld');
return $next($request);
}
}
//inside your Kernel.php, make sure to register the middleware
protected $routeMiddleware = [
'subdomain' => \App\Http\Middleware\Subdomain::class,
];
Route::group(['middleware' => 'subdomain', 'domain' => 'subdomain.{domain}.{tld}'], function () {
Route::get('/', function () {
dd(func_get_args()); //no args, because it was removed in the middleware
});
});
回答by hcker2000
Just so you know you can do
只是让你知道你可以做到
'domain' => '{subdomain}.{domain}.{tld}'
and that will route a.domain.com and b.domain.com to all he same routes / same controllers and you can do your logic inside of that controller.
这会将 a.domain.com 和 b.domain.com 路由到所有相同的路由/相同的控制器,您可以在该控制器内部执行逻辑。
I would use a middleware like suggjested above to remove the domain and tld from the request or else every modeling is going to have to look like.
我会使用上面建议的中间件从请求中删除域和 tld,否则每个建模都必须看起来像。
public function myMethod($subdomain, $domain, $tld, $id)
assuming your route needs an $id or any other route parameter.
假设您的路线需要 $id 或任何其他路线参数。
One other thing you might be interested in is Explicit Form Model Binding.
您可能感兴趣的另一件事是显式表单模型绑定。
回答by sabtikw
check this it might help answer your question :
检查这可能有助于回答您的问题:
http://laravel.com/docs/5.1/routing#parameters-regular-expression-constraints
http://laravel.com/docs/5.1/routing#parameters-regular-expression-constraints
http://laravel.com/docs/5.1/routing#route-group-sub-domain-routing
http://laravel.com/docs/5.1/routing#route-group-sub-domain-routing