如何使用 root 配置 Route 资源
How to configure a Route resource with root
我正在使用 Laravel 5.1,并创建了一个路由资源,例如:Route::resource('/page', 'PagesController');
。但我不希望 /page
成为我的资源,或者 /page/about
。我宁愿 /about
等等
然而,当我使用 Route::resource('/', 'PagesController');
时,我的名字和 URI 都被毁了。要恢复我使用过的名称:
Route::resource('/page', 'PagesController', [
'names' => [
'index' => 'pages.index',
'show' => 'pages.show',
'create' => 'pages.create',
'store' => 'pages.store',
'edit' => 'pages.edit',
'update' => 'pages.update',
'destroy' => 'pages.destroy'
]
]);
这使得/
带我到索引文件,但是/about
之类的不起作用,因为URI(显示为{}
)没有任何参数.
我可以使用 $router->get('/', 'PagesController@index");
等手动设置我的所有路线,但是为所有事情设置其中之一似乎是错误的方法。
如何设置路由资源以在根目录中使用参数?
我也遇到过和你一样的问题,但我找不到任何解决方案。
查看laravel/framework/src/Illuminate/Routing/ResourceRegistrar.php中的register函数,我们可以看到$base在这种情况下为空:
// We need to extract the base resource from the resource name. Nested resources
// are supported in the framework, but we need to know what name to use for a
// place-holder on the route wildcards, which should be the base resources.
$base = $this->getResourceWildcard(last(explode('.', $name)));
$defaults = $this->resourceDefaults;
foreach ($this->getResourceMethods($defaults, $options) as $m) {
$this->{'addResource'.ucfirst($m)}($name, $base, $controller, $options);
}
这使得添加方法出错:
* Add the show method for a resourceful route.
*
* @param string $name
* @param string $base
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Route
*/
protected function addResourceShow($name, $base, $controller, $options)
{
$uri = $this->getResourceUri($name).'/{'.$base.'}';
$action = $this->getResourceAction($name, $controller, 'show', $options);
return $this->router->get($uri, $action);
}
我不确定除了手动添加每个方法之外我们还能做些什么,但我在我的案例中所做的就是这样。
我正在使用 Laravel 5.1,并创建了一个路由资源,例如:Route::resource('/page', 'PagesController');
。但我不希望 /page
成为我的资源,或者 /page/about
。我宁愿 /about
等等
然而,当我使用 Route::resource('/', 'PagesController');
时,我的名字和 URI 都被毁了。要恢复我使用过的名称:
Route::resource('/page', 'PagesController', [
'names' => [
'index' => 'pages.index',
'show' => 'pages.show',
'create' => 'pages.create',
'store' => 'pages.store',
'edit' => 'pages.edit',
'update' => 'pages.update',
'destroy' => 'pages.destroy'
]
]);
这使得/
带我到索引文件,但是/about
之类的不起作用,因为URI(显示为{}
)没有任何参数.
我可以使用 $router->get('/', 'PagesController@index");
等手动设置我的所有路线,但是为所有事情设置其中之一似乎是错误的方法。
如何设置路由资源以在根目录中使用参数?
我也遇到过和你一样的问题,但我找不到任何解决方案。
查看laravel/framework/src/Illuminate/Routing/ResourceRegistrar.php中的register函数,我们可以看到$base在这种情况下为空:
// We need to extract the base resource from the resource name. Nested resources
// are supported in the framework, but we need to know what name to use for a
// place-holder on the route wildcards, which should be the base resources.
$base = $this->getResourceWildcard(last(explode('.', $name)));
$defaults = $this->resourceDefaults;
foreach ($this->getResourceMethods($defaults, $options) as $m) {
$this->{'addResource'.ucfirst($m)}($name, $base, $controller, $options);
}
这使得添加方法出错:
* Add the show method for a resourceful route.
*
* @param string $name
* @param string $base
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Route
*/
protected function addResourceShow($name, $base, $controller, $options)
{
$uri = $this->getResourceUri($name).'/{'.$base.'}';
$action = $this->getResourceAction($name, $controller, 'show', $options);
return $this->router->get($uri, $action);
}
我不确定除了手动添加每个方法之外我们还能做些什么,但我在我的案例中所做的就是这样。