Skip to content

Commit b39a308

Browse files
committed
Merge tag 'v1.0.2'
Minimum tability
2 parents 628237d + e9acc04 commit b39a308

14 files changed

+521
-19
lines changed

README.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"license": "MIT",
44
"type": "library",
55
"version": "dev-master",
6-
"keywords": ["laravel", "controller"],
6+
"keywords": ["laravel", "controller", "generic", "webtack"],
77
"authors": [
88
{
99
"name": "Ruslan Bilyshko",
Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
<?php namespace Webtack\GenericController;
22

3-
use Illuminate\Routing\Controller as BaseController;
3+
use Illuminate\Routing\Controller as BaseRoutingController;
44
use Illuminate\Http\Request;
55
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
66

77
/**
8-
* Class GenericBaseController
8+
* Class BaseController
99
* --------------------------------------------------
1010
* Intentionally simple parent class for all views.
1111
* Only implements dispatch-by-method and simple sanity checking.
1212
*
1313
* @package Webtack\GenericView
1414
*/
15-
class GenericBaseController extends BaseController {
15+
abstract class BaseController extends BaseRoutingController {
16+
1617
/**
1718
* Allowed methods
19+
*
1820
* @var array
1921
*/
2022
protected $httpMethodNames = [
21-
'GET',
22-
'POST',
23-
'PUT',
24-
'PATCH',
25-
'DELETE',
23+
'GET',
24+
'POST',
25+
'PUT',
26+
'PATCH',
27+
'DELETE',
28+
'OPTIONS',
2629
];
2730

2831
/**
@@ -32,19 +35,19 @@ class GenericBaseController extends BaseController {
3235
*
3336
* @param \Illuminate\Http\Request $request
3437
*
35-
* @param array $parameters
36-
*
3738
* @return mixed
39+
* @internal param array $parameters
40+
*
3841
*/
39-
public function dispatch(Request $request, array $parameters) {
40-
$method_name = $request->method();
42+
public function dispatch($request) {
43+
$method_name = $request->method();
4144

4245
if (in_array($method_name, $this->httpMethodNames)) {
4346

44-
array_unshift($parameters, $request);
45-
return $this->callAction($method_name, $parameters);
47+
return $this->callAction($method_name, $this->getRequestParameters($request));
4648
}
4749
else {
50+
4851
$this->httpMethodNotAllowed($method_name);
4952
}
5053
}
@@ -57,17 +60,35 @@ public function dispatch(Request $request, array $parameters) {
5760
* @return \Illuminate\Http\Response
5861
*/
5962
public function asView(Request $request) {
60-
$parameters = $request->route()->parameters;
61-
$parameters = array_values($parameters);
62-
return $this->dispatch($request, $parameters);
63+
64+
return $this->dispatch($request);
6365
}
6466

67+
/**
68+
* If the view was called with an unsupported HTTP method, this method will be called
69+
*
70+
* @param string $method_name
71+
*/
6572
protected function httpMethodNotAllowed(string $method_name) {
6673
$class_name = __CLASS__;
67-
6874
$message = "Method: [{$method_name}] Not Allowed in property " . "[\$httpMethodNames] on class [{$class_name}]";
6975

7076
throw new MethodNotAllowedException($this->httpMethodNames, $message);
7177
}
7278

79+
/**
80+
* Passes parameters to the called method.
81+
* the rest are defined in your route
82+
*
83+
* @param \Illuminate\Http\Request $request
84+
*
85+
* @return array
86+
*/
87+
final protected function getRequestParameters($request) {
88+
$parameters = $request->route()->parameters;
89+
array_unshift($parameters, $request);
90+
91+
return $parameters;
92+
}
93+
7394
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php namespace Webtack\GenericController;
2+
3+
4+
use Webtack\GenericController\Traits\BaseTrait;
5+
use Webtack\GenericController\Traits\ContextTrait;
6+
use Webtack\GenericController\Traits\ModifierNameTrait;
7+
use Webtack\GenericController\Traits\SingleObjectContextTrait;
8+
use Webtack\GenericController\Traits\SingleObjectTemplateTrait;
9+
10+
abstract class DetailController extends BaseController {
11+
12+
use BaseTrait;
13+
use ModifierNameTrait;
14+
use SingleObjectTemplateTrait;
15+
use SingleObjectContextTrait;
16+
17+
18+
public function get($request) {
19+
$context = $this->getContextData($request);
20+
return $this->renderToResponse($context);
21+
}
22+
23+
}

src/Controllers/ListController.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php namespace Webtack\GenericController;
2+
3+
use Webtack\GenericController\Traits\BaseTrait;
4+
use Webtack\GenericController\Traits\ModifierNameTrait;
5+
use Webtack\GenericController\Traits\MultipleObjectContextTrait;
6+
use Webtack\GenericController\Traits\MultipleObjectTemplateTrait;
7+
8+
9+
abstract class ListController extends BaseController {
10+
11+
use BaseTrait;
12+
use ModifierNameTrait;
13+
use MultipleObjectContextTrait;
14+
use MultipleObjectTemplateTrait;
15+
16+
public function get($request) {
17+
$context = $this->getContextData($request);
18+
return $this->renderToResponse($context);
19+
}
20+
21+
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php namespace Webtack\GenericController;
2+
3+
use Webtack\GenericController\Traits\BaseTrait;
4+
use Webtack\GenericController\Traits\TemplateTrait;
5+
6+
7+
/**
8+
* Class TemplateController
9+
* Processes the specified template using a context that contains parameters from the URL.
10+
*
11+
* @package Webtack\GenericController
12+
*/
13+
abstract class TemplateController extends BaseController {
14+
15+
use BaseTrait;
16+
use TemplateTrait;
17+
18+
19+
public function get() {
20+
return $this->renderToResponse();
21+
}
22+
23+
/**
24+
* Init templateName property from view
25+
*
26+
* @return string
27+
*/
28+
abstract function templateName();
29+
}

src/Traits/BaseTrait.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php namespace Webtack\GenericController\Traits;
2+
3+
4+
trait BaseTrait {
5+
6+
public function __get($name) {
7+
return call_user_func_array([$this, $name], []);
8+
}
9+
10+
}

0 commit comments

Comments
 (0)