Download the PHP package mkd/laravel-state-management without Composer
On this page you can find all versions of the php package mkd/laravel-state-management. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mkd/laravel-state-management
More information about mkd/laravel-state-management
Files in mkd/laravel-state-management
Package laravel-state-management
Short Description a state management package for Laravel, enabling shared and persistent application state across services, requests, and files without the need for reinitialization. It supports casting, default state handling, and custom methods for managing complex state in your Laravel applications
License MIT
Homepage https://github.com/mustafakhaleddev/laravel-state-management
Informations about the package laravel-state-management
Laravel State Management
A state management solution for Laravel applications inspired by Redux, designed to manage complex application state across services, caching layers, and requests, with support for casting, default state handling, and custom methods.
Features
- Shared State Across Application:Stores are shared globally across the application, making them easily accessible without reinitialization in multiple files.
- State Persistence and Rehydration: Manage application state easily, persisting and rehydrating data as needed.
- Casting Attributes: Automatically cast attributes to types like collections or custom classes using Laravel's castable functionality.
- Default State Handling: Define fallback states to be used when rehydration fails.
- Custom Store Logic: Add custom methods to interact with specific store states and manage application logic.
Installation
Install the package using Composer:
Basic Usage
Step 1: Create a Store
You can create a store class using the store:make
Artisan command:
This command will generate a new store class in your app/Stores
directory.
Step 2: Define Your Store
In the generated store, define your attributes and casts. The store will manage the state related to these attributes:
Step 3: Using a Store
To interact with a store and manage the state, you can retrieve the store instance and access its methods:
Step 4: Handling Persistent State
You can persist and rehydrate states based on a unique key, enabling state restoration between requests:
Step 5: Defining Custom Casts
If you need custom casting for certain attributes, use the store-cast:make
command:
Then, define the casting logic in the generated cast class:
Store Rehydration Example
Commands
store:make <StoreName>
: Generates a new store class.store-cast:make <CastName>
: Generates a new custom cast class.
Example Stores
SettingsStore
This store manages application settings and allows for the dynamic update of user preferences.
UserNotification
This store handles sending notifications, such as emails, to users.
Persist
By Default Persist is saving the state object in cache so it can be easy rehydrated later with the key
You can override persist
logic by implementing your own logic in store class
rehydrate
By Default rehydrate is setting the state from cache based on store key
You can override rehydrate
logic by implementing your own logic in store class
License
This package is open-sourced software licensed under the MIT license.