[英]Laravel routes with regex portions that don't get passed as parameters
Is there any way I can have a route set up with one regex section that doesn't get passed as a parameter? 有什么办法可以设置一个不带有参数的正则表达式段的路由?
For example: 例如:
Route::get('{string}/method/{id}', function($id)
{
return 'only one parameter passed, ID is ' . $id;
});
Specifically I'm routing to a controller and the methods need to be compatible with routes coming from elsewhere, which don't include this first parameter. 具体来说,我要路由到控制器,并且方法必须与来自其他地方的路由兼容,这些路由不包含第一个参数。
The most important thing is that, routes has to be matched according to it's declaration, for example, if you define a route like your example here 最重要的是,路由必须根据其声明进行匹配,例如,如果您在此处定义了类似示例的路由
Route::get('{string}/method/{id}', function($id)
{
return 'only one parameter passed, ID is ' . $id;
});
Then the requested url
has to be matched with same numbers of parameters including the http
method (GET here) and in this case, the route will match only with something like this 然后,所请求的
url
必须与相同数量的参数进行匹配,包括http
方法(此处为GET),在这种情况下,路由将仅与此类匹配
httP//example.com/something/method/10
Here the second parameter 10
is not bound to be digits because you didn't make it to be a digit using where(...)
so, it could be anything but two parameters must be required. 这里第二个参数
10
不一定是数字,因为您没有使用where(...)
使其成为数字where(...)
因此它可以是任何东西,但必须有两个参数。
As an alternative, you may define a missing
method in your controller like this (An idea only) 或者,您可以像这样在控制器中定义一个
missing
方法(仅一个想法)
public function missingMethod($args = array())
{
// $args will contain all parameters
}
This is a special method in Laravel
that any controller may contain it and whenever a non-existing method will be called in that controller, then this missingMethod
would be called and all the parameters will be passed to it's $args
parameter as an array, so if you have two parameters in the url
and calling method is missing in the controller then you may get those parameters within this method scope, something like [param1, param2]
and from this method you may call your desired method depending on the count of params
. 这是
Laravel
中的一种特殊方法,任何控制器都可以包含该方法,并且只要该控制器中不存在的方法将被调用,就会调用missingMethod
方法并将所有参数作为数组传递给它的$args
参数,因此如果您在url
有两个参数,并且控制器中缺少调用方法,则可以在此方法范围内获得这些参数,例如[param1, param2]
然后可以从此方法中调用所需的方法,具体取决于params
的数量。
So, if you just point the route's action to this controller which has the missing method then from the missingMethod
you may call another method using different parameters according to your other method. 因此,如果仅将路由操作指向具有缺少方法的该控制器,则可以从
missingMethod
根据其他方法使用不同的参数调用另一个方法。
Also check PHP Overloading and call_user_func_array to get the real idea of missingMethod
. 还要检查PHP Overloading和call_user_func_array以获取
missingMethod
的真实思想。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.