前言
在本机上,是的,确实如此。阅读here。但是如果你想要一些不同的东西,我给你一个技巧来做到这一点。首先,您可以创建自己的ResourceRegistrar。我的是[位于app/Routing/ResourceRegistrar.php]:
namespace App\Routing;
use Illuminate\Routing\ResourceRegistrar as BaseRegistrar;
class ResourceRegistrar extends BaseRegistrar
{
}
然后在你的服务提供商中注册你自己的RouteRegistrar:
$this->app->bind('Illuminate\Routing\ResourceRegistrar', 'App\Routing\ResourceRegistrar');
注意:我通过register方法在App\Providers\AppServiceProvider中注册了我自己的RouteRegistrar。
示例
我在routes.php 中添加了自己的资源控制器,如下所示:
Route::resource('photo', 'PhotoController');
所以,我应该有一个PhotoController 来处理这个请求。
实施
我们知道,对“/photo”的GET 请求将由PhotoController@index 方法处理,要将您的photo:index 操作修改为photo:root 操作,请将您的ResourceRegistrar 修改为如下所示:
namespace App\Routing;
use Illuminate\Routing\ResourceRegistrar as BaseRegistrar;
class ResourceRegistrar extends BaseRegistrar
{
protected function addResourceIndex($name, $base, $controller, $options)
{
$uri = $this->getResourceUri($name);
$action = $this->getResourceAction($name, $controller, 'root', $options);
return $this->router->get($uri, $action);
}
}
所以现在GET 对'/photo' 的请求将由PhotoController@root 方法处理。
备忘单
Verb | Path | Method to modify |
----------|-----------------------|----------------- |
GET | `/photo` | addResourceIndex |
GET | `/photo/create` | addResourceCreate |
POST | `/photo` | addResourceStore |
GET | `/photo/{photo}` | addResourceShow |
GET | `/photo/{photo}/edit` | addResourceEdit |
PUT/PATCH | `/photo/{photo}` | addResourceUpdate |
DELETE | `/photo/{photo}` | addResourceDestroy|
见ResourceRegistrarhere的基本代码。