maduser / laravel-viewmodel
ViewModels for Laravel
Installs: 3
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:project
Requires
- php: >=7.1.3
- maduser/laravel-support: *
Requires (Dev)
- phpunit/phpunit: ^7.0
README
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:
composer require maduser/laravel-viewmodel
Quick Start
Creating a ViewModel
ViewModels are simple to define. Here's an example of a ViewModel that displays a quote:
use Maduser\Laravel\ViewModel\ViewModel; class MyQuoteWidget extends ViewModel { protected $view = 'my-widget'; // Blade template protected $quote; // Quote string public function getQuote(): ?string { return $this->quote; } public function setQuote(?string $quote): MyQuoteWidget { $this->quote = $quote; return $this; } }
Blade Template
Create a corresponding Blade template for your ViewModel. For the MyQuoteWidget ViewModel, the my-widget.blade.php file might look like this:
<div class="widget quote"> <p>{{ $view->getQuote() }}</p> </div>
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
use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Maduser\Laravel\ViewModel\ViewModels\Page; class ExampleController extends Controller { private $exampleVars; public function __construct() { $this->exampleVars = [ 'title' => 'Welcome Home', 'text' => 'An inspiring quote here' ]; } public function showPage(): Responsable { // Creating and returning a ViewModel instance return Page::create($this->exampleVars); } }
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...
$userWidget = UserWidget::create([ 'profile' => UserProfile::create(), 'activity' => UserActivity::create(['activities' => $user->activities]) ]);