


About the implementation method of rewriting resource routing custom URL in Laravel
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:
Action | Path | Action | Route Name |
---|---|---|---|
GET | /photo | index | photo.index |
GET | /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 | ##DELETE |
destroy | photo.destroy |
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); } }
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; }); }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!
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!

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
