About the implementation method of rewriting resource routing custom URL in Laravel

不言
Release: 2023-03-31 22:24:01
Original
1771 people have browsed it

This article mainly introduces you to the implementation method of rewriting resource routing custom URL in Laravel. Friends who need it can refer to it

Preface

This article mainly introduces to you the relevant content about rewriting resource routing custom URLs in Laravel, and shares it for your reference and study. I won’t say much below, let’s take a look at the detailed introduction:

Reason for rewriting

In the recent process of developing projects using Laravel, in order to simplify the routing code, I used Laravel’s resource routing,Route::resource('photo', 'PhotoController');

By default, the routing table generated by Laravel is as follows:

##POST/photostorephoto.storeGET/photo/{photo}showphoto.showGET /photo/{photo}/editeditphoto.edit##PUT/PATCH##DELETE/photo/{photo}destroyphoto.destroyIn order to meet the project requirements, the /photo/{photo}/edit path needs to be changed to /photo/ edit/{photo}
Action Path Action Route Name
GET /photo index photo.index
GET /photo/create create photo.create
/photo/{photo} update photo.update

Implementation steps

I queried the Laravel source code and found that the method for generating this path is in Illuminate\Routing\ In the ResourceRegistrar.php class, we need to override the addResourceEdit method of this class.

Override the addResourceEdit method

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);
 }
}
Copy after login

Register this class in AppServiceProvider


public function boot()
 {
  //重写资源路由
  $registrar = new \App\Routing\ResourceRegistrar($this->app['router']);
  $this->app->bind('Illuminate\Routing\ResourceRegistrar', function () use ($registrar) {
   return $registrar;
  });
 }
Copy after login

Finally use

Route::resource('photo', 'PhotoController');

The generated route meets the requirements.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the binding operation of Laravel framework routing and controller


Laravel framework routing settings

The above is the detailed content of About the implementation method of rewriting resource routing custom URL in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!