How to rewrite resource routing in Laravel? This article mainly introduces you to the implementation method of rewriting resource routing custom URL in Laravel. Friends in need can refer to it. I hope to be helpful.
Preface
This article mainly introduces the relevant content about rewriting resource routing custom URLs in Laravel, and shares it for your reference. Study, not much to say below, let’s take a look at the detailed introduction:
Reason for rewriting
Recently used During the Laravel development project, Laravel's resource routing was used in order to simplify the routing code. Route::resource('photo', 'PhotoController');
By default , the routing table generated by Laravel is as follows:
Path | Action | Route Name | |
---|---|---|---|
/photo | index | photo.index | |
/photo/create | create | photo.create | |
/photo | store | photo.store | |
/photo/{photo} | show | photo.show | |
/photo/{photo}/edit | edit | photo.edit | |
/photo/{photo} | update | photo.update | |
/photo/{photo} | destroy | photo.destroy |
##Implementation steps
After querying the Laravel source code, we found that the method generated by this path is in the Illuminate\Routing\ResourceRegistrar.php class. We need to override the addResourceEdit method of this class.
Create a new class \App\Routing\ResourceRegistrar.php with the following code:
namespace App\Routing; use Illuminate\Routing\ResourceRegistrar as OriginalRegistrar; class ResourceRegistrar extends OriginalRegistrar { /** * Add the edit method for a resourceful route. * * @param string $name * @param string $base * @param string $controller * @param array $options * @return \Illuminate\Routing\Route */ protected function addResourceEdit($name, $base, $controller, $options) { $uri = $this->getResourceUri($name).'/'.static::$verbs['edit'].'/{'.$base.'}'; $action = $this->getResourceAction($name, $controller, 'edit', $options); return $this->router->get($uri, $action); } }
The generated route will meet the needs.
Laravel optimized split routing file laravel writing APP interface (API) The above is the detailed content of Detailed explanation of how to rewrite resource routing in Laravel. For more information, please follow other related articles on the PHP Chinese website!public function boot()
{
//重写资源路由
$registrar = new \App\Routing\ResourceRegistrar($this->app['router']);
$this->app->bind('Illuminate\Routing\ResourceRegistrar', function () use ($registrar) {
return $registrar;
});
}