|
| 1 | + |
| 2 | +# AlphaWidget — Widgets For Laravel 5 |
| 3 | +---------------------------------------- |
| 4 | + |
| 5 | +[](https://packagist.org/packages/leaphly/cart-bundle) |
| 6 | + |
| 7 | + |
| 8 | +This is a very simple, easy to use Widget manager for [Laravel 5](http://laravel.com) (A version for Laravel 4.2 might come). |
| 9 | + |
| 10 | +> **Note!** |
| 11 | +> This package won't hit version `1.0.0` until I write the tests for it. |
| 12 | +> Which would probably take a while... |
| 13 | +
|
| 14 | +## Installation |
| 15 | + |
| 16 | +First, install the package via [Composer](https://getcomposer.org/). |
| 17 | +```bash |
| 18 | +composer require breda/laravel-alphaWidget |
| 19 | +``` |
| 20 | + |
| 21 | +in the `config/app.php` file, add the Service Provider |
| 22 | + |
| 23 | +```php |
| 24 | +'providers' => [ |
| 25 | +// Other providers... |
| 26 | +'BReda\AlphaWidget\ServiceProvider', |
| 27 | +] |
| 28 | +``` |
| 29 | +And lastly, register the Alias |
| 30 | +```php |
| 31 | +'aliases' => [ |
| 32 | +// Other aliases... |
| 33 | +'BReda\AlphaWidget\Facades\AlphaWidget', |
| 34 | +] |
| 35 | +``` |
| 36 | + |
| 37 | +And you're ready to use AlphaWidget! |
| 38 | + |
| 39 | +------------ |
| 40 | + |
| 41 | +## Walk Through |
| 42 | + |
| 43 | +Now, when I said that this is a simple Widget manager, I meant it! |
| 44 | +It's as simple as registering a widget alias, and it's class inside the configuration file. Like this : |
| 45 | + |
| 46 | +```php |
| 47 | +'widgets' => [ |
| 48 | +// Other widgets... |
| 49 | +'myRecentUsersWidget' => 'RecentUsers', |
| 50 | +] |
| 51 | +``` |
| 52 | + |
| 53 | +> One note to take here before we move on, is that I'am only referencing the class name, not the complete namespace. And that's what the `namespace` field in the `config/alphaWidget.php` file is here for. |
| 54 | +
|
| 55 | +> To shorten class names, put your desired namespace in the config file, just make sure that all of your widget classes declare that namespace, that's all! |
| 56 | +
|
| 57 | + |
| 58 | +And then, calling the widget is as simple as : |
| 59 | +```php |
| 60 | +AlphaWidget::render('myRecentUsersWidget'); |
| 61 | + |
| 62 | +// Or, a much better way: |
| 63 | +AlphaWidget::myRecentUsersWidget(); |
| 64 | +``` |
| 65 | + |
| 66 | +Now, what does that `RecentUsers` look like, I hear you say! |
| 67 | +It's a simple class implementing the `AlphaWidget` Contract (interface), stating that it must have the `render` method. That method, is responsible of rendering the widget contents. |
| 68 | + |
| 69 | +```php |
| 70 | +<?php namespace App\Widgets; |
| 71 | + |
| 72 | +use BReda\AlphaWidget\Contracts\AlphaWidget as WidgetContract; |
| 73 | + |
| 74 | +class RecentUsers implements WidgetContract |
| 75 | +{ |
| 76 | + |
| 77 | + /** |
| 78 | + * Render the Widget |
| 79 | + * |
| 80 | + * @return string |
| 81 | + */ |
| 82 | + public function render(){ |
| 83 | +return "Hello from the widget!"; |
| 84 | + } |
| 85 | + |
| 86 | +} |
| 87 | +``` |
| 88 | +> One note to take here! Is that the `render` method, should `return` the contents, and NOT `echo` them out! |
| 89 | +> Remember! No `echo`! Just `return`. |
| 90 | +
|
| 91 | +> Another note |
| 92 | +
|
| 93 | +#### Passing Arguments To Widget Calls |
| 94 | +You can of course, pass arguments to widget calls. Like this for example: |
| 95 | +```php |
| 96 | +AlphaWidget::render('myRecentUsersWidget', [5]); |
| 97 | + |
| 98 | +// Or, a much better way: |
| 99 | +AlphaWidget::myRecentUsersWidget(5); |
| 100 | +``` |
| 101 | + |
| 102 | +And then in your class: |
| 103 | +```php |
| 104 | +<?php namespace App\Widgets; |
| 105 | + |
| 106 | +use BReda\AlphaWidget\Contracts\AlphaWidget as WidgetContract; |
| 107 | + |
| 108 | +class RecentUsers implements WidgetContract |
| 109 | +{ |
| 110 | +/** |
| 111 | + * How much should we limit the displayed users. |
| 112 | + * |
| 113 | + * @var string |
| 114 | + */ |
| 115 | +protected $limit; |
| 116 | + |
| 117 | +/** |
| 118 | + * Create a new RecentUsers Widget instance |
| 119 | + * |
| 120 | + * @return void |
| 121 | + */ |
| 122 | +public function __construct($limit) |
| 123 | +{ |
| 124 | +$this->limit = $limit; |
| 125 | +} |
| 126 | + |
| 127 | + /** |
| 128 | + * Render the Widget |
| 129 | + * |
| 130 | + * @return mixed |
| 131 | + */ |
| 132 | + public function render(){ |
| 133 | +return "Displaying the {$this->limit} recently registerd users..."; |
| 134 | + } |
| 135 | + |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +Now! One last thing to note! Since all widgets are resolved through the Laravel's IoC container, you can type-hint any Laravel Service to be used in your Widget class! |
| 140 | + |
| 141 | +#### Practical Example |
| 142 | + |
| 143 | +An good example, would be fetching the 5 recently registered users. |
| 144 | +```php |
| 145 | +<?php namespace App\Widgets; |
| 146 | + |
| 147 | +use App\Repositories\UsersRepository; |
| 148 | +use BReda\AlphaWidget\Contracts\AlphaWidget as WidgetContract; |
| 149 | + |
| 150 | +class RecentUsers implements WidgetContract |
| 151 | +{ |
| 152 | +/** |
| 153 | + * How much should we limit the displayed users. |
| 154 | + * |
| 155 | + * @var string |
| 156 | + */ |
| 157 | +protected $limit; |
| 158 | + |
| 159 | +/** |
| 160 | + * Create a new RecentUsers Widget instance |
| 161 | + * |
| 162 | + * @return void |
| 163 | + */ |
| 164 | +public function __construct($limit, UsersRepository $users) |
| 165 | +{ |
| 166 | +$this->limit = $limit; |
| 167 | +$this->users = $users; |
| 168 | +} |
| 169 | + |
| 170 | + /** |
| 171 | + * Render the Widget |
| 172 | + * |
| 173 | + * @return mixed |
| 174 | + */ |
| 175 | + public function render(){ |
| 176 | +$users = $this->users->getRecentUsers($this->limit); |
| 177 | + |
| 178 | +return view('widgets.recent-users', ['users' => $users]); |
| 179 | + } |
| 180 | + |
| 181 | +} |
| 182 | +``` |
| 183 | +And that's it really! |
| 184 | + |
| 185 | +------------------- |
| 186 | + |
| 187 | +## Contributing |
| 188 | +Anything from bug fixes, improvements or anything similar! Pull requests are welcome! Just make sure to submit them to the `develop` branch, rather to the `master` branch, as this later only has production-ready code. |
0 commit comments