我已将 Laravel 5.7 中Route的默认参数更改为
Route::resource('questions', 'QuestionController')->parameters(['questions' => 'question_slug']);
但是我对如何将正则表达式模式设置为该新参数感到困惑,我想在此参数上应用slug_regex
,我尝试过:
Route::resource('questions', 'QuestionController')->parameters(['questions' => 'question_slug'])->where(['slug' => '^[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*$']);
但是出现此错误:
BadMethodCallException方法Illuminate \ Routing \ PendingResourceRegistration ::哪里不存在。
答案 0 :(得分:1)
最后,我在一行代码中找到了解决方案,设置参数/参数的正则表达式验证,只需导航到YourProjectName\app\Providers\RouteServiceProvider.php
,您将在其中找到名为boot
的方法,它默认包含此代码;
public function boot()
{
//
parent::boot();
}
只需在此处添加您的参数即可;
Route::pattern('parameter', 'regex-rule-here');
所以您的代码将为
public function boot()
{
//
Route::pattern('slug', '[\w\d\-\_]+');
parent::boot();
}
要获取更多参数,只需将数组中的参数传递为:
Route::pattern(['1st-para' => 'regex-rule-here', '2nd-para' => 'regex-rule-here']);
答案 1 :(得分:0)
所以props
在Laravel中不是真正的类,实际上是Facade,它使您可以像静态访问实类一样。
当您查看Route
函数时(它是您编写Illuminate\Routing\Router
时所调用的函数),它使您可以访问的真实类是resource()
,我们可以看到它返回了Route::resource()
对象和Illuminate\Routing\PendingResourceRegistration
方法在此类上不可用。
那么如何解决您的问题?嗯,有两种方法,您可以检查where()
在您的slug
动作中是否有效,或者您可以创建一个controller
,因为资源路由可以附加middleware
。