Detailed explanation of how to rewrite resource routing in Laravel

*文
Release: 2023-03-19 08:26:02
Original
2993 people have browsed it

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:

##ActionPathActionRoute NameGET/photoindexphoto.indexGET/photo/createcreatephoto.createPOST/photostorephoto.storeGET/photo/{photo}showphoto.showGET/photo/{photo}/editeditphoto.editPUT/PATCH/photo/{photo}updatephoto.updateDELETE/photo/{photo}destroyphoto.destroy
To satisfy For project requirements, the /photo/{photo}/edit path needs to be changed to /photo/edit/{photo}

##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.

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 useRoute::resource('photo', 'PhotoController');

The generated route will meet the needs.

Related recommendations:

Laravel optimized split routing file

laravel writing APP interface (API)

Using Laravel’s Queue

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!

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