Download the PHP package marcot89/laravel-magic-routes without Composer
On this page you can find all versions of the php package marcot89/laravel-magic-routes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download marcot89/laravel-magic-routes
More information about marcot89/laravel-magic-routes
Files in marcot89/laravel-magic-routes
Package laravel-magic-routes
Short Description Magic Routes for Laravel Applications
License MIT
Homepage https://github.com/marcot89/laravel-magic-routes
Informations about the package laravel-magic-routes
LaravelMagicRoutes
Create RESTfull routes for your laravel app based on your controller names and namespaces to avoid overrides and define a pattern to your projects routes. It is compatible with laravel 7+.
Installation
Via Composer
Usage
Add MagicRoutes
trait to the controller to generate routes for it automatically:
For now there is no route for your controller because you don't have any public
method declared on it. A new route will be generated for every public method added to the controller. So if we add the common crud public methods like this:
We will have these generated routes based on those public methods:
# Customize Http Methods for Routes
Any other public method in the controller will be generated a route with a GET http method as default, but you can customize it with a prefix. Let's see an example.
# Route Params
By convention the first parameter will be set before the action name. All other parameters will be added after.
Will generate:
# Middlewares
There are two ways to declare a middleware for a controller:
Using Protected Property
Using Constructor
# Namespaced Routes
The controller namespace will generate a prefix for that route:
Any public method declared in this controller will generate urls with prefix like:
And named routes like:
# Invokable Controllers
If you like to create a controller for every action you can use the invokable controllers in a namespaced that makes sense for your route. Example:
This will generate an URL like:
# Nested RESTfull Routes
Sometimes you want or need to define nested resources. Let's say we have posts of a user but we want nested routes for that. You can do it with a namespaced controller like this:
This will generate the following route:
# Resource URLs Plural vs Singular
RESTfull resources is always in plural. So the convention defines URL resources in plural no matter the name of the controller. If your controller is UserController
or UsersController
both will generate the same resource URL /users
.
But you can disable this behavior using the plural
property:
With this property false
will generate routes following the controller name. Now if your controller is UserController
it will generate a route /user
. If it is UsersController
it will generate /users
.
Note: For invokable controllers the plural property is always disabled.
Change log
Please see the changelog for more information on what has changed recently.
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email author email instead of using the issue tracker.
Credits
- Marco Avila
- All Contributors
MIT License
Please see the license file for more information.