### Bash: Install Spatie Laravel Route Attributes Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md This command installs the spatie/laravel-route-attributes package into your Laravel project using Composer. Ensure you have Composer installed and configured for your project. ```bash composer require spatie/laravel-route-attributes ``` -------------------------------- ### Define Route Domain with Attribute (PHP) Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md This example illustrates using the `#[Domain]` attribute on a controller to set a specific domain for all routes within that controller. It includes the controller setup and the corresponding route registrations with the domain applied. ```php use Spatie\RouteAttributes\Attributes\Get; use Spatie\RouteAttributes\Attributes\Post; use Spatie\RouteAttributes\Attributes\Domain; #[Domain('my-subdomain.localhost')] class MyController { #[Get('my-get-route')] public function myGetMethod() { } #[Post('my-post-route')] public function myPostMethod() { } } ``` ```php Route::get('my-get-route', [MyController::class, 'myGetMethod'])->domain('my-subdomain.localhost'); Route::post('my-post-route', [MyController::class, 'myPostMethod'])->domain('my-subdomain.localhost'); ``` -------------------------------- ### PHP: Automatic Route Registration Example Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md This code shows the resulting Laravel route registration that occurs automatically when using the `#[Get]` attribute on a controller method. It directly translates the attribute definition into a standard `Route::get` facade call. ```php Route::get('my-route', [MyController::class, 'myMethod']); ``` -------------------------------- ### Define Route Domain from Config Key (PHP) Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md This snippet shows how to specify a route's domain using a key from a configuration file via the `#[DomainFromConfig]` attribute. It includes the configuration file example and the controller attribute usage, demonstrating dynamic domain assignment. ```php // config/domains.php return [ 'main' => env('SITE_URL', 'example.com'), 'subdomain' => env('SUBDOMAIN_URL', 'subdomain.example.com') ]; ``` ```php use Spatie\RouteAttributes\Attributes\Get; use Spatie\RouteAttributes\Attributes\Post; use Spatie\RouteAttributes\Attributes\DomainFromConfig; #[DomainFromConfig('domains.main')] class MyController { #[Get('my-get-route')] public function myGetMethod() { } } ``` ```php Route::get('my-get-route', [MyController::class, 'myGetMethod'])->domain('example.com'); ``` -------------------------------- ### PHP: Configure Directories with Namespace and Path Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md This configuration example shows how to specify directories for route attribute scanning using a namespace-to-path mapping. This is useful when controllers are located outside the default application directories, such as within separate modules. ```php 'directories' => [ 'Modules\Admin\Http\Controllers\' => base_path('admin-module/Http/Controllers'), // Or base_path('admin-module/Http/Controllers') => [ 'namespace' => 'Modules\Admin\Http\Controllers\' ], app_path('Http/Controllers'), ], ``` -------------------------------- ### Define Route Parameter Constraints with Where Annotation Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md Use the `Where` annotation on a class or method to constrain the format of route parameters. This example demonstrates defining a general numeric constraint and a specific alphanumeric constraint for different routes. ```php use Spatie\RouteAttributes\Attributes\Get; use Spatie\RouteAttributes\Attributes\Post; use Spatie\RouteAttributes\Attributes\Where; use Spatie\RouteAttributes\Attributes\WhereAlphaNumeric; #[Where('my-where', '[0-9]+')] class MyController { #[Get('my-get-route/{my-where}')] public function myGetMethod() { } #[Post('my-post-route/{my-where}/{my-alpha-numeric}')] #[WhereAlphaNumeric('my-alpha-numeric')] public function myPostMethod() { } } ``` ```php Route::get('my-get-route/{my-where}', [MyController::class, 'myGetMethod'])->where(['my-where' => '[0-9]+']); Route::post('my-post-route/{my-where}/{my-alpha-numeric}', [MyController::class, 'myPostMethod'])->where(['my-where' => '[0-9]+', 'my-alpha-numeric' => '[a-zA-Z0-9]+']); ``` -------------------------------- ### PHP: Define GET Route with Attribute Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md This snippet demonstrates how to define a GET route in a Laravel controller using the `#[Get]` attribute provided by the spatie/laravel-route-attributes package. The attribute automatically registers the route with the specified URI and controller method. ```php use Spatie\RouteAttributes\Attributes\Get; class MyController { #[Get('my-route')] public function myMethod() { } } ``` -------------------------------- ### Enable Scoped Route Bindings with Attribute (PHP) Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md This code demonstrates how to use the `#[ScopeBindings]` attribute to enable scoped Eloquent model binding for nested route parameters. It shows the attribute applied to a controller method and provides an equivalent manual route registration example. ```php use Spatie\RouteAttributes\Attributes\Get; use Spatie\RouteAttributes\Attributes\ScopeBindings; class MyController { #[Get('users/{user}/posts/{post}')] #[ScopeBindings] public function getUserPost(User $user, Post $post) { return $post; } } ``` ```php Route::get('/users/{user}/posts/{post}', function (User $user, Post $post) { return $post; })->scopeBindings(); ``` -------------------------------- ### Resource Controller Registration with Attributes Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md Registers resource controllers using the `Resource` or `ApiResource` attribute. Supports configuration of route names, parameters, shallow nesting, and API-specific actions. This simplifies the setup of standard CRUD routes. ```php use Spatie\RouteAttributes\Attributes\Resource; use Spatie\RouteAttributes\Attributes\Prefix; #[Prefix('api/v1')] #[Resource( resource: 'photos.comments', apiResource: true, shallow: true, parameters: ['comments' => 'comment:uuid'], names: 'api.v1.photoComments', except: ['destroy'] )] // OR #[ApiResource(resource: 'photos.comments', shallow: true, ...)] class PhotoCommentController { public function index(Photo $photo) { } public function store(Request $request, Photo $photo) { } public function show(Comment $comment) { } public function update(Request $request, Comment $comment) { } } ``` -------------------------------- ### Specifying Route Names with Attributes Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md Assigns a specific name to a route using the `name` parameter within HTTP verb attributes like `Get`. This allows for easy referencing of routes in views or redirects. ```php use Spatie\RouteAttributes\Attributes\Get; class MyController { #[Get('my-route', name: "my-route-name")] public function myMethod() { } } ``` -------------------------------- ### Define Default Values for Optional Route Parameters with Defaults Annotation Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md Use the `Defaults` annotation on a class or method to specify default values for optional route parameters. This example shows defaults set at the class and method levels, including overrides. ```php use Spatie\RouteAttributes\Attributes\Defaults; use Spatie\RouteAttributes\Attributes\Get; use Spatie\RouteAttributes\Attributes\Post; #[Defaults('param', 'controller-default')] class MyController extends Controller { #[Get('my-get-route/{param?}')] public function myGetMethod($param) { } #[Post('my-post-route/{param?}/{param2?}')] #[Defaults('param2', 'method-default')] public function myPostMethod($param, $param2) { } #[Get('my-default-route/{param?}/{param2?}/{param3?}')] #[Defaults('param2', 'method-default-first')] #[Defaults('param3', 'method-default-second')] public function myDefaultMethod($param, $param2, $param3) { } #[Get('my-override-route/{param?}')] #[Defaults('param', 'method-default')] public function myOverrideMethod($param) { } } ``` ```php Route::get('my-get-route/{param?}', [MyController::class, 'myGetMethod'])->setDefaults(['param', 'controller-default']); Route::post('my-post-route/{param?}/{param2?}', [MyController::class, 'myPostMethod'])->setDefaults(['param', 'controller-default', 'param2' => 'method-default']); Route::get('my-default-route/{param?}/{param2?}/{param3?}', [MyController::class, 'myDefaultMethod'])->setDefaults(['param', 'controller-default', 'param2' => 'method-default-first', 'param3' => 'method-default-second']); Route::get('my-override-route/{param?}', [MyController::class, 'myOverrideMethod'])->setDefaults(['param', 'method-default']); ``` -------------------------------- ### Disable Scoped Route Bindings with Attribute (PHP) Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md This example shows how to disable scoped Eloquent model binding using `#[ScopeBindings(false)]`. This is equivalent to calling `->withoutScopedBindings()` manually, preventing nested models from being automatically scoped. ```php use Spatie\RouteAttributes\Attributes\ScopeBindings; #[ScopeBindings(false)] class MyController { // ... methods ... } ``` -------------------------------- ### Run Tests Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md Command to execute the test suite for the project. ```bash composer test ``` -------------------------------- ### Adding Middleware Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md Explains how to apply middleware to routes, either on a per-method basis or to an entire controller class. ```APIDOC ## Adding Middleware ### Description Apply middleware to routes using the `middleware` parameter on route attributes or the `Middleware` attribute on controller classes. ### Attribute Parameter - `middleware`: (class-string|array) A middleware class or an array of middleware classes. ### Example (Method Level) ```php use Spatie\RouteAttributes\Attributes\Get; #[Get('my-route', middleware: MyMiddleware::class)] public function myMethod() { } ``` ### Example (Class Level and Method Level) ```php use Spatie\RouteAttributes\Attributes\Get; use Spatie\RouteAttributes\Attributes\Middleware; #[Middleware(MyMiddleware::class)] class MyController { #[Get('my-route')] public function firstMethod() { } #[Get('my-other-route', middleware: MyOtherMiddleware::class)] public function secondMethod() { } } ``` ### Generated Route Examples ```php // For method-level example: Route::get('my-route', [MyController::class, 'myMethod'])->middleware(MyMiddleware::class); // For class and method-level example: Route::get('my-route', [MyController::class, 'firstMethod'])->middleware(MyMiddleware::class); Route::get('my-other-route', [MyController::class, 'secondMethod'])->middleware([MyMiddleware::class, MyOtherMiddleware::class]); ``` ``` -------------------------------- ### Using Other HTTP Verbs Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md Demonstrates how to use various HTTP verb attributes like Post, Put, Patch, Delete, and Options for defining routes. ```APIDOC ## Other HTTP Verbs ### Description Use these attributes on controller methods to define routes for specific HTTP verbs. ### Attributes - `#[Spatie\RouteAttributes\Attributes\Post(uri)]` - `#[Spatie\RouteAttributes\Attributes\Put(uri)]` - `#[Spatie\RouteAttributes\Attributes\Patch(uri)]` - `#[Spatie\RouteAttributes\Attributes\Delete(uri)]` - `#[Spatie\RouteAttributes\Attributes\Options(uri)]` ### Example ```php use Spatie\RouteAttributes\Attributes\Post; use Spatie\RouteAttributes\Attributes\Put; use Spatie\RouteAttributes\Attributes\Patch; use Spatie\RouteAttributes\Attributes\Delete; use Spatie\RouteAttributes\Attributes\Options; // ... #[Post('my-uri')] public function myPostMethod() {} #[Put('my-uri')] public function myPutMethod() {} #[Patch('my-uri')] public function myPatchMethod() {} #[Delete('my-uri')] public function myDeleteMethod() {} #[Options('my-uri')] public function myOptionsMethod() {} ``` ``` -------------------------------- ### Bash: Publish Route Attributes Config File Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md This command publishes the configuration file for the spatie/laravel-route-attributes package. This allows you to customize settings like enabled status, directories to scan, and group configurations. ```bash php artisan vendor:publish --provider="Spatie\RouteAttributes\RouteAttributesServiceProvider" --tag="config" ``` -------------------------------- ### Resource Controllers Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md Explains how to register resource controllers using the `Resource` or `ApiResource` attribute, including options for customization. ```APIDOC ## Resource Controllers ### Description Register resource controllers using the `Resource` or `ApiResource` attribute. Customize route generation with `only`, `except`, `parameters`, `names`, `shallow`, and `apiResource` options. ### Attributes - `#[Spatie\RouteAttributes\Attributes\Resource(resource, apiResource: bool, shallow: bool, parameters: array, names: string|array, only: array, except: array)]` - `#[Spatie\RouteAttributes\Attributes\ApiResource(resource, apiResource: bool, shallow: bool, parameters: array, names: string|array, only: array, except: array)]` ### Example ```php use Spatie\RouteAttributes\Attributes\Resource; use Spatie\RouteAttributes\Attributes\Prefix; #[Prefix('api/v1')] #[Resource( resource: 'photos.comments', apiResource: true, shallow: true, parameters: ['comments' => 'comment:uuid'], names: 'api.v1.photoComments', except: ['destroy'], )] class PhotoCommentController { public function index(Photo $photo) {} public function store(Request $request, Photo $photo) {} public function show(Comment $comment) {} public function update(Request $request, Comment $comment) {} } ``` ### Generated Routes Example ```php // For the above example: Route::get('api/v1/photos/{photo}/comments', [PhotoCommentController::class, 'index'])->name('api.v1.photoComments.index'); Route::post('api/v1/photos/{photo}/comments', [PhotoCommentController::class, 'store'])->name('api.v1.photoComments.store'); Route::get('api/v1/comments/{comment}', [PhotoCommentController::class, 'show'])->name('api.v1.photoComments.show'); Route::match(['put', 'patch'], 'api/v1/comments/{comment}', [PhotoCommentController::class, 'update'])->name('api.v1.photoComments.update'); ``` ``` -------------------------------- ### Using Multiple Verbs Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md Covers attributes for registering routes that respond to multiple HTTP verbs. ```APIDOC ## Using Multiple Verbs ### Description Register routes that respond to all HTTP verbs or a specific set of verbs. ### Attributes - `#[Spatie\RouteAttributes\Attributes\Any(uri)]`: Responds to all common HTTP verbs. - `#[Spatie\RouteAttributes\Attributes\Route(methods: array, uri)]`: Responds to a specified array of HTTP verbs. ### Example ```php use Spatie\RouteAttributes\Attributes\Any; use Spatie\RouteAttributes\Attributes\Route; #[Any('my-uri')] public function handleAnyRequest() {} #[Route(['put', 'patch'], 'my-uri')] public function handlePutOrPatchRequest() {} ``` ``` -------------------------------- ### Define Route Prefix with Attribute (PHP) Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md This code demonstrates how to use the `#[Prefix]` attribute on a controller class to apply a common prefix to all routes defined within that controller's methods. It shows the controller definition and the resulting registered routes. ```php use Spatie\RouteAttributes\Attributes\Get; use Spatie\RouteAttributes\Attributes\Post; use Spatie\RouteAttributes\Attributes\Prefix; #[Prefix('my-prefix')] class MyController { #[Get('my-get-route')] public function myGetMethod() { } #[Post('my-post-route')] public function myPostMethod() { } } ``` ```php Route::get('my-prefix/my-get-route', [MyController::class, 'myGetMethod']); Route::post('my-prefix/my-post-route', [MyController::class, 'myPostMethod']); ``` -------------------------------- ### Use Helper Attributes for Common Route Parameter Constraints Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md For common regular expression patterns, helper attributes like `WhereAlpha`, `WhereNumber`, `WhereUuid`, etc., provide a concise way to add pattern constraints to routes. ```php use Spatie\RouteAttributes\Attributes\WhereAlpha; use Spatie\RouteAttributes\Attributes\WhereAlphaNumeric; use Spatie\RouteAttributes\Attributes\WhereIn; use Spatie\RouteAttributes\Attributes\WhereNumber; use Spatie\RouteAttributes\Attributes\WhereUlid; use Spatie\RouteAttributes\Attributes\WhereUuid; #[WhereAlpha('alpha')] #[WhereAlphaNumeric('alpha-numeric')] #[WhereIn('in', ['value1', 'value2'])] #[WhereNumber('number')] #[WhereUlid('ulid')] #[WhereUuid('uuid')] ``` -------------------------------- ### Applying Middleware with Route Attributes Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md Applies middleware to specific controller methods or entire controller classes using the `middleware` parameter in verb attributes or the `Middleware` attribute. This centralizes middleware management. ```php use Spatie\RouteAttributes\Attributes\Get; use Spatie\RouteAttributes\Attributes\Middleware; class MyController { #[Get('my-route', middleware: MyMiddleware::class)] public function myMethod() { } } #[Middleware(MyMiddleware::class)] class AnotherController { #[Get('my-route')] public function firstMethod() { } #[Get('my-other-route', middleware: MyOtherMiddleware::class)] public function secondMethod() { } } ``` -------------------------------- ### PHP: Default Route Attributes Configuration Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md This is the default configuration file for spatie/laravel-route-attributes. It enables automatic route registration and defines the directories where controllers with route attributes should be scanned. It also supports defining group configurations like middleware and prefixes for specific directories. ```php return [ /* * Automatic registration of routes will only happen if this setting is `true` */ 'enabled' => true, /* * Controllers in these directories that have routing attributes * will automatically be registered. * * Optionally, you can specify group configuration by using key/values */ 'directories' => [ app_path('Http/Controllers'), app_path('Http/Controllers/Web') => [ 'middleware' => ['web'] ], app_path('Http/Controllers/Api') => [ 'prefix' => 'api', 'middleware' => 'api' ], ], ]; ``` -------------------------------- ### Registering All HTTP Verbs with Route Attributes Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md Utilizes the `Any` attribute to register a route that responds to all standard HTTP verbs. Alternatively, the `Route` attribute can be used to specify multiple verbs for a single route definition. ```php use Spatie\RouteAttributes\Attributes\Any; use Spatie\RouteAttributes\Attributes\Route; #[Any('my-uri')] #[Route(['put', 'patch'], 'my-uri')] ``` -------------------------------- ### Enable WithTrashed Bindings in Laravel Route Attributes (PHP) Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md Demonstrates how to use the `#[WithTrashed]` annotation on a controller class and its methods to enable 'withTrashed' bindings for models. It also shows how to explicitly disable this behavior using `#[WithTrashed(false)]`. ```php use Spatie\RouteAttributes\Attributes\Get; use Spatie\RouteAttributes\Attributes\Post; use Spatie\RouteAttributes\Attributes\WithTrashed; #[WithTrashed] class MyController extends Controller { #[Get('my-get-route/{param}')] #[WithTrashed] public function myGetMethod($param) { } #[Post('my-post-route/{param}')] #[WithTrashed(false)] public function myPostMethod($param) { } #[Get('my-default-route/{param}')] public function myDefaultMethod($param) { } } ``` ```php Route::get('my-get-route/{param}', [MyController::class, 'myGetMethod'])->WithTrashed(); Route::post('my-post-route/{param}', [MyController::class, 'myPostMethod'])->withTrashed(false); Route::get('my-default-route/{param}', [MyController::class, 'myDefaultMethod'])->withTrashed(); ``` -------------------------------- ### Group Routes with Domain and Prefix using Group Annotation Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md The `Group` annotation on a class allows for the creation of multiple route groups, each with different domains and prefixes applied to all methods within that class. ```php use Spatie\RouteAttributes\Attributes\Get; use Spatie\RouteAttributes\Attributes\Post; use Spatie\RouteAttributes\Attributes\Domain; use Spatie\RouteAttributes\Attributes\Group; #[Group(domain: 'my-subdomain.localhost', prefix: 'my-prefix')] #[Group(domain: 'my-second-subdomain.localhost', prefix: 'my-second-prefix')] class MyController { #[Get('my-get-route')] public function myGetMethod() { } #[Post('my-post-route')] public function myPostMethod() { } } ``` ```php Route::get('my-get-route', [MyController::class, 'myGetMethod'])->prefix('my-prefix')->domain('my-subdomain.localhost'); Route::post('my-post-route', [MyController::class, 'myPostMethod'])->prefix('my-prefix')->domain('my-subdomain.localhost'); Route::get('my-get-route', [MyController::class, 'myGetMethod'])->prefix('my-second-prefix')->domain('my-second-subdomain.localhost'); Route::post('my-post-route', [MyController::class, 'myPostMethod'])->prefix('my-second-prefix')->domain('my-second-subdomain.localhost'); ``` -------------------------------- ### Specify Route Name Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md Details how to assign a specific name to a route using the `name` parameter. ```APIDOC ## Specify a Route Name ### Description Assign a custom name to a route using the `name` parameter available in most route attributes. ### Attribute Parameter - `name`: (string) The desired route name. ### Example ```php use Spatie\RouteAttributes\Attributes\Get; #[Get('my-route', name: 'my-route-name')] public function myMethod() { } ``` ### Generated Route Example ```php // For the above example: Route::get('my-route', [MyController::class, 'myMethod'])->name('my-route-name'); ``` ``` -------------------------------- ### HTTP Verb Attributes for Routes Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md Defines routes using various HTTP verbs like POST, PUT, PATCH, DELETE, and OPTIONS via dedicated attributes. This allows for clear and concise route definitions directly within controller methods. ```php use Spatie\RouteAttributes\Attributes\Post; use Spatie\RouteAttributes\Attributes\Put; use Spatie\RouteAttributes\Attributes\Patch; use Spatie\RouteAttributes\Attributes\Delete; use Spatie\RouteAttributes\Attributes\Options; #[Post('my-uri')] #[Put('my-uri')] #[Patch('my-uri')] #[Delete('my-uri')] #[Options('my-uri')] ``` -------------------------------- ### PHP: Filter Route Scan Directories with Patterns Source: https://github.com/spatie/laravel-route-attributes/blob/main/README.md This configuration demonstrates how to use `patterns` and `not_patterns` to control which files are scanned for route attributes within a specific directory. This is helpful for co-located files (e.g., controllers and tests) to ensure only relevant files are processed. ```php 'directories' => [ base_path('app-modules/Blog') => [ // only register routes in files that match the patterns 'patterns' => ['*Controller.php'], // do not register routes in files that match the patterns 'not_patterns' => ['*Test.php'], ], ], ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.