Download the PHP package jojomak13/laravel-attributes without Composer
On this page you can find all versions of the php package jojomak13/laravel-attributes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jojomak13/laravel-attributes
More information about jojomak13/laravel-attributes
Files in jojomak13/laravel-attributes
Package laravel-attributes
Short Description This package provides the ability to create custom attributes for Laravel controllers. You can use these attributes to add functionality like authorization, validation, or any other custom behavior to your controller methods.
License MIT
Informations about the package laravel-attributes
Laravel Attributes
This package provides the ability to create custom attributes for Laravel controllers. You can use these attributes to add functionality like authorization, validation, or any other custom behavior to your controller methods.
Installation
Basic Usage
Let's walk through creating a custom attribute for authorization using Laravel policies.
Creating an Attribute
First, generate a new attribute class using the provided artisan command:
This will create a new attribute class in the App\Attributes
namespace.
Attribute Structure
Each attribute class contains two main methods:
__construct
: Defines the parameters you want to pass to your attributehandle
: Contains the logic for your attribute's functionality
Example Implementation
Here's an example of implementing a policy-based authorization attribute:
Using Attributes in Controllers
To use attributes in your controllers, follow these steps:
- Import the
HasAttributes
trait - Import your custom attribute class
- Add the attribute to your controller methods
Here's an example:
In this example, the PolicyAttribute
will check if the current user has permission to view posts before executing the index
method.
Additional Notes
- Attributes are executed before the controller method runs
- You can stack multiple attributes on a single method
- The
HasAttributes
trait is required for attribute functionality - Custom attributes must implement the
ICustomAttribute
interface
For more advanced usage and additional examples, please refer to the package documentation.
All versions of laravel-attributes with dependencies
illuminate/support Version ^11.38
illuminate/console Version ^11.38
illuminate/filesystem Version ^11.38