带有通配符的多个域的Laravel路由组,如何处理域后缀? [英] Laravel routing group for muliple domains with wildcards, how to handle domain suffixes?

查看:105
本文介绍了带有通配符的多个域的Laravel路由组,如何处理域后缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个域,在我的本地服务器上,采用以下格式:

I have 3 domains which, on my local server, take the format:

  • mydomainfirst.local
  • mydomainsecond.local
  • mydomainthird.local

在routes.php文件中,我具有以下内容:

In my routes.php file, I have the following:

Route::group(array('domain' => '{domain}.{suffix}'), function() {

    Route::get('/', 'Primary@initialize');

});

这个想法是在我的控制器中使用$domain变量,并从中提取第一/第二/第三部分,效果很好.但是,现在我的网站在线,此路由文件不再起作用,并引发Http-notfound异常.一段时间后,我发现问题出在,域现在采用的格式为mydomainfirst.co.uk.因为域后缀现在有2个部分,所以看来我需要这样做:

The idea is to take the $domain variable in my controller and extract the first/second/third part from it, which works fine. However, now my site is online, this routing file no longer works and throws a Http-not-found exception. After a while, I have figured that the problem is that the domains have now taken the format mydomainfirst.co.uk. Because there are now 2 parts to the domain suffix, it seems I need to do this:

Route::group(array('domain' => '{domain}.{a}.{b}'), function() {

    Route::get('/', 'Primary@initialize');

});

要使其正常工作.这是愚蠢的.我怎样才能告诉它只接受任何后缀?我可以使用任何"通配符吗?

To get it to work. Which is stupid. How can I tell it to just accept any suffix? Is there an 'anything' wildcard I can use?

我已经尝试了一些诸如此答案的方法,但是不适用于路由组.

I have tried a few things like this answer but it doesn't work with route groups.

:似乎增强型路由器软件包会至少可以使我在路由组中添加where子句,但这是否解决了如何设置将与不确定的段数匹配的通配符的问题?我需要类似的东西:

It seems the Enhanced Router package would at least enable me to add a where clause to the route group, but does it solve the problem of how to set a wildcard that will match an indeterminate number of segments? I need something like:

{domain}.{anything}

这将同时符合以下条件:

That will match both:

mydomainfirst.本地 AND mydomainfirst.合作.英国

mydomainfirst.local AND mydomainfirst.co.uk

?

推荐答案

好吧,我首先要说该软件包实际上看起来不错,应该可以使用.即使您无法通过安装使其运行,也可以获取文件并与您自己的服务提供商等一起使用该代码.

Ok let me first say that the code of this package actually looks good and should work. Even if you can't get it running by installing you could take the files and use the code with your own service provider etc.

但是,还有一种快速而肮脏的解决方案. (实际上,程序包确实很相似,但是看起来好多了;))

But there's also a kind of quick and dirty solution. (Actually the package does it pretty similar, but it looks a lot nicer ;))

首先,这是您针对一条路线执行的操作:

First, here's how you can do it for one route:

Route::group(array('domain' => '{domain}.{tld}'), function(){
    Route::get('/', 'Primary@initialize')->where('tld', '.*');
});

因此,路由组的where条件实际上是在单个路由上设置的.

So the where condition for the route group actually gets set on the individual route.

当然,您不想为该组内的每条路由执行此操作,因此可以使用简单的foreach循环:

Of course you don't want to do this for every route inside that group so you can use a simple foreach loop:

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

    Route::get('/', 'Primary@initialize');

    foreach($group->getRoutes() as $route){
        $route->where('tld', '.*');
    }
});

注意:循环需要出现在所有路线之后.否则,它们将不会注册,因此不会返回getRoutes()

Note: The loop needs to come after all routes. Otherwise they won't registered and therefore not returned with getRoutes()

这篇关于带有通配符的多个域的Laravel路由组,如何处理域后缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆