|
| 1 | +# Generic Controllers |
| 2 | + |
| 3 | +Generic Controllers is a extension system for your [Laravel](https://laravel.com) application.<br> |
| 4 | +Generic Controllers includes a set of generic representations that are ideally suited for solving a number of routine tasks. |
| 5 | + |
| 6 | +# Installation |
| 7 | + |
| 8 | +Install using composer: |
| 9 | + |
| 10 | +``` |
| 11 | +composer require webtack/generic-controller |
| 12 | +``` |
| 13 | + |
| 14 | +# Simple examples |
| 15 | + |
| 16 | +Use Generic as the parent classes. You can specify these classes as the parent for your custom view class and override the attribute values in the body of your class |
| 17 | + |
| 18 | +## TemplateController |
| 19 | +Suitable for displaying static pages that do not receive data from the database. |
| 20 | + |
| 21 | +```php |
| 22 | + |
| 23 | +use Webtack\GenericController\TemplateController; |
| 24 | + |
| 25 | +class AboutController extends TemplateController { |
| 26 | + |
| 27 | + /** |
| 28 | + * Init templateName property from view |
| 29 | + * |
| 30 | + * @return string |
| 31 | + */ |
| 32 | + function templateName() { |
| 33 | + return "generic-about"; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +``` |
| 38 | +Create a router and call the method in it @asView |
| 39 | + |
| 40 | +```php |
| 41 | +Route::get(/about', ['uses' => 'AboutController@asView']); |
| 42 | + |
| 43 | +``` |
| 44 | + |
| 45 | +## DetailController |
| 46 | +It should be used when you want to get one model for a given key |
| 47 | + |
| 48 | +```php |
| 49 | + |
| 50 | +use App\Models\Article; |
| 51 | +use Webtack\GenericController\DetailController; |
| 52 | + |
| 53 | +class ArticlePageController extends DetailController { |
| 54 | + |
| 55 | + /** |
| 56 | + * Init Model from Query |
| 57 | + * |
| 58 | + * @return \Illuminate\Database\Eloquent\Model |
| 59 | + */ |
| 60 | + function model() { |
| 61 | + return new Article(); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +``` |
| 66 | +Create a router and call the method in it @asView |
| 67 | + |
| 68 | +```php |
| 69 | +Route::get(/article/{id}', ['uses' => 'ArticlePageController@asView']); |
| 70 | + |
| 71 | +``` |
| 72 | + |
| 73 | +### Context data |
| 74 | + |
| 75 | +```php |
| 76 | + protected function getContextData($request, $column = []){} |
| 77 | +``` |
| 78 | +The method responsible for obtaining data from the database. <br> |
| 79 | +By default, there will be an attempt to get the model by the parameter ** id ** from the route |
| 80 | + |
| 81 | +But you can override this behavior by overloading this method. It must return an array with a context name key and an object of the model. |
| 82 | + |
| 83 | +```php |
| 84 | +protected function getContextData($request, $column = []) { |
| 85 | + $article = $this->model(); |
| 86 | + $data = $article->where(['name' => 'Jhoon'])->first(); |
| 87 | + |
| 88 | + return ['article' => $data]; |
| 89 | +} |
| 90 | + |
| 91 | +``` |
| 92 | + |
| 93 | +Or override get method |
| 94 | + |
| 95 | +```php |
| 96 | + |
| 97 | + public function get($request, $name) { |
| 98 | + $context = $this->getContextData($request, ['name' => $name]); |
| 99 | + return $this->renderToResponse($context); |
| 100 | + } |
| 101 | +``` |
| 102 | +Supported methods correspond to request types. |
| 103 | + |
| 104 | +```php |
| 105 | + |
| 106 | + public function get($request); |
| 107 | + public function post($request); |
| 108 | + public function put($request); |
| 109 | + public function delete($request); |
| 110 | + public function path($request); |
| 111 | + public function options($request); |
| 112 | + |
| 113 | +``` |
| 114 | +All methods accept the first object **\Illuminate\Http\Request $request** |
| 115 | +The following parameters can be those that you defined in your routes. |
| 116 | + |
| 117 | +### View Name |
| 118 | + |
| 119 | +By default, DetailController will search for a view with the name of the "art-page-class" class new, and you will also be able to override it in the **templateName** method |
| 120 | + |
| 121 | +Note the auxiliary methods **templatePrefix** and **templateSuffix**. <br> |
| 122 | +If you define them, the data will be appended to the name of the view returned by the method **templateName** |
| 123 | + |
| 124 | +### Context name |
| 125 | + |
| 126 | +The name of the access variable in the view will be taken from the name of the model class starting with a small letter. |
| 127 | + |
| 128 | +```php |
| 129 | +{{ $article->title }} |
| 130 | + |
| 131 | +``` |
| 132 | + |
| 133 | +Of course you can override this in method **contextObjectName** |
| 134 | + |
| 135 | +```php |
| 136 | +protected function contextObjectName() { |
| 137 | + return "foobar"; |
| 138 | +} |
| 139 | + |
| 140 | +``` |
| 141 | + |
| 142 | +## ListController |
| 143 | +Use to display the list of entities |
| 144 | + |
| 145 | +```php |
| 146 | + |
| 147 | +use Webtack\GenericController\ListController; |
| 148 | + |
| 149 | +class BlogPageController extends ListController { |
| 150 | + |
| 151 | + /** |
| 152 | + * @return \Illuminate\Database\Eloquent\Model |
| 153 | + */ |
| 154 | + public function model() { |
| 155 | + return new Blog(); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Init templateName from view |
| 160 | + * |
| 161 | + * @return string |
| 162 | + */ |
| 163 | + public function templateName() { |
| 164 | + return 'blog'; |
| 165 | + } |
| 166 | + |
| 167 | + protected function templatePrefix() { |
| 168 | + return 'blog.'; |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +``` |
| 173 | +Create a router and call the method in it @asView |
| 174 | + |
| 175 | +```php |
| 176 | +Route::get(/blog', ['uses' => 'BlogPageController@asView']); |
| 177 | + |
| 178 | +``` |
| 179 | + |
| 180 | +By default, the name of the view will be appended with the suffix **- list**, but you can undo or override this behavior in the corresponding method, of course. |
| 181 | + |
| 182 | +## P.S. |
| 183 | +This is a small set of possibilities. But I think in the future I will expand it if necessary. |
0 commit comments