Download the PHP package spatie/laravel-route-attributes without Composer
On this page you can find all versions of the php package spatie/laravel-route-attributes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download spatie/laravel-route-attributes
More information about spatie/laravel-route-attributes
Files in spatie/laravel-route-attributes
Package laravel-route-attributes
Short Description Auto register routes using PHP attributes
License MIT
Homepage https://github.com/spatie/laravel-route-attributes
Informations about the package laravel-route-attributes
Use PHP 8 attributes to register routes in a Laravel app
This package provides attributes to automatically register routes. Here's a quick example:
This attribute will automatically register this route:
Are you a visual learner?
In this video you'll get an introduction to PHP 8 attributes and how this laravel-routes-attributes works under the hood.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
You can publish the config file with:
This is the contents of the published config file:
For controllers outside the applications root namespace directories can also be added using a namespace => path
pattern in the directories array. In the following example controllers from Modules\Admin\Http\Controllers
will be included.
If you are using a directory structure where you co-locate multiple types of files in the same directory and want to
be more specific about which files are checked for route attributes, you can use the patterns
and not_patterns
options. For example, if you are co-locating your tests with your controllers you could use the patterns
option to only
look in controller files, or you could use not_patterns
to configure it to not look in test files for route
attributes.
Usage
The package provides several annotations that should be put on controller classes and methods. These annotations will be used to automatically register routes
Adding a GET route
This attribute will automatically register this route:
Using other HTTP verbs
We have left no HTTP verb behind. You can use these attributes on controller methods.
Resource controllers
To register a resource controller, use the Resource
attribute as shown in the example below.
You can use only
or except
parameters to manage your resource routes availability.
You can use parameters
parameter to modify the default parameters set by the resource attribute.
You can use the names
parameter to set the route names for the resource controller actions. Pass a string value to set a base route name for each controller action or pass an array value to define the route name for each controller action.
You can use shallow
parameter to make a nested resource to apply nesting only to routes without a unique child identifier (index
, create
, store
).
You can use apiResource
boolean parameter to only include actions used in APIs. Alternatively, you can use the ApiResource
attribute, which extends the Resource
attribute class, but the parameter apiResource
is already set to true
.
Using Resource
attribute with Domain
, Prefix
and Middleware
attributes works as well.
The attribute in the example above will automatically register following routes:
Using multiple verbs
To register a route for all verbs, you can use the Any
attribute:
To register a route for a few verbs at once, you can use the Route
attribute directly:
Specify a route name
All HTTP verb attributes accept a parameter named name
that accepts a route name.
This attribute will automatically register this route:
Adding middleware
All HTTP verb attributes accept a parameter named middleware
that accepts a middleware class or an array of middleware classes.
This annotation will automatically register this route:
To apply middleware on all methods of a class you can use the Middleware
attribute. You can mix this with applying attribute on a method.
These annotations will automatically register these routes:
Specifying a prefix
You can use the Prefix
annotation on a class to prefix the routes of all methods of that class.
These annotations will automatically register these routes:
Specifying a domain
You can use the Domain
annotation on a class to prefix the routes of all methods of that class.
These annotations will automatically register these routes:
Specifying a domain from a config key
There maybe a need to define a domain from a configuration file, for example where your subdomain will be different on your development environment to your production environment.
When this is parsed, it will get the value of domains.main
from the config file and
register the route as follows;
Scoping bindings
When implicitly binding multiple Eloquent models in a single route definition, you may wish to scope the second Eloquent model such that it must be a child of the previous Eloquent model.
By adding the ScopeBindings
annotation, you can enable this behaviour:
``
This is akin to using the ->scopeBindings()
method on the route registrar manually:
By default, Laravel will enabled scoped bindings on a route when using a custom keyed implicit binding as a nested route parameter, such as /users/{user}/posts/{post:slug}
.
To disable this behaviour, you can pass false
to the attribute:
This is the equivalent of calling ->withoutScopedBindings()
on the route registrar manually.
You can also use the annotation on controllers to enable implicitly scoped bindings for all its methods. For any methods where you want to override this, you can pass false
to the attribute on those methods, just like you would normally.
Specifying where
You can use the Where
annotation on a class or method to constrain the format of your route parameters.
These annotations will automatically register these routes:
For convenience, some commonly used regular expression patterns have helper attributes that allow you to quickly add pattern constraints to your routes.
Specifying a group
You can use the Group
annotation on a class to create multiple groups with different domains and prefixes for the routes of all methods of that class.
These annotations will automatically register these routes:
Specifying defaults
You can use the Defaults
annotation on a class or method to define the default values of your optional route parameters.
These annotations will automatically register these routes:
With Trashed
You can use the WithTrashed
annotation on a class or method to enable WithTrashed bindings to the model.
You can explicitly override the behaviour using WithTrashed(false)
if it is applied at the class level
These annotations will automatically register these routes:
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Freek Van der Herten
- All Contributors
License
The MIT License (MIT). Please see License File for more information.