schubu / laravel-widgets
Feature your blade view files with widgets to keep your controller classes clean
Requires
- php: ^7.2
Requires (Dev)
- orchestra/testbench: 3.8.*
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-10-21 03:12:30 UTC
README
A laravel widget package based on the laracasts episode. It helps you to keep your controller clean and DRY.
Installation
You can install the package via composer:
composer require schubu/laravel-widgets
Usage
Artisan command
You can create a widget by running this artisan command:
php artisan make:widget ExampleWidget
It will two folders:
- a
Widget
folder within theapp/Http
directory - a
widget
folder within theresource/views
directory
This task also places a class file and a blade file to get started.
app/Http/ExampleWidget.php
resource/views/example-widget.blade.php
Blade @widget directive
You can easily include your widget by using this blade directive:
@widget('ExampleWidget')'
Passing data to your widget
You can pass data in two ways:
- define a public property
- define a public method
The public methods and properties will be passed as variables to your blade view!
Example
Your class:
namespace App\Http\Widgets; use SchuBu\LaravelWidgets\LaravelWidgets; class ExampleWidget extends LaravelWidgets { public $exampleData = "Welcome to your widget!"; public function exampleMethod() { return [ "My first data", "My second data", "My third data" ]; } }
In your blade you can access your data this way:
This is {{ $exampleData }} awesome! @foreach($exampleMethod as $content) {{ $content }} <br> @endforeach
Specifying the blade filename
You can customize the blade filename by adding an additional parameter to the artisan command:
php artisan make:widget ExampleWidget MyExampleWidget
That results in a blade file named my-example-widget.blade.php
. The generated class now contains an additional protected
property $viewPath
namespace App\Http\Widgets; use SchuBu\LaravelWidgets\LaravelWidgets; class ExampleWidget extends LaravelWidgets { protected $viewPath = 'widgets.my-example-widget'; ... }
Testing
Because I'm new to testing, no testing at all happened :-(.
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
This is my first package ever. So hope you'll contribute and help improving it. Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email peter@schu-bu.de instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.