Download the PHP package maduser/laravel-viewmodel without Composer
On this page you can find all versions of the php package maduser/laravel-viewmodel. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download maduser/laravel-viewmodel
More information about maduser/laravel-viewmodel
Files in maduser/laravel-viewmodel
Package laravel-viewmodel
Short Description ViewModels for Laravel
License MIT
Informations about the package laravel-viewmodel
maduser/laravel-viewmodel
A long time ago, in a galaxy far away... Laravel did not have view components. This is how old this solution is. Still a valid approach, although no longer the Laravel way.
The maduser/laravel-viewmodel package provides an elegant way to encapsulate data and logic needed for views in Laravel applications, promoting clean separation of concerns and reusable code. By using ViewModels, you can simplify your controllers and views, making your codebase more maintainable and understandable.
Features
- Flexible Responses: ViewModel can automatically determine whether to render a view or return JSON based on the request, providing flexibility for APIs and web interfaces.
- Encapsulation of Logic: Keep your controller clean by moving presentation logic into ViewModels.
- Ease of Use: Implement ViewModel with minimal setup and use it seamlessly with Laravel's response handling.
Installation
Install the package via composer:
Quick Start
Creating a ViewModel
ViewModels are simple to define. Here's an example of a ViewModel that displays a quote:
Blade Template
Create a corresponding Blade template for your ViewModel. For the MyQuoteWidget ViewModel, the my-widget.blade.php file might look like this:
Using ViewModel in a Controller
You can use the ViewModel in a controller to pass data to your view. Here's an example of how to use a Page ViewModel
Advanced Usage
Responsable Interface
ViewModels implement Laravel's Responsable interface, allowing them to be directly returned from controller methods. Depending on the request's acceptable content types, the response can be either the rendered view or a JSON representation.
To force a response type, you can use methods like render() or toJson(). To add more acceptable content types (for example pdf), use ViewModel::macro() in conjunction with Laravel Request::macro() and Response::macro().
Usefull Methods
- __toString(): Automatically renders the view when the ViewModel is treated as a string.
- toArray(): Returns an array representation of the ViewModel, useful for formating the structure of the JSON responses.
- toJson(): Returns a JSON string representation of the ViewModel.
Nesting ViewModels
That is totally doable...