-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseController.php
73 lines (64 loc) · 1.53 KB
/
BaseController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
namespace Ilyamur\PhpOnRails\Controllers;
/**
* Base controller
*
* PHP version 8.0
*/
abstract class BaseController
{
/**
* Parameters from the matched route
* @var array
*/
protected array $routeParams = [];
/**
* Class constructor
*
* @param array $route_params Parameters from the route
*
* @return void
*/
public function __construct(array $routeParams)
{
$this->routeParams = $routeParams;
}
/**
* Magic method called when a non-existent or inaccessible method is
* called on an object of this class. Used to execute before and after
* filter methods on action methods.
*
* @param string $name Method name
* @param array $args Arguments passed to the method
*
* @return void
*/
public function __call(string $methodName, array $args): void
{
$methodName = $methodName . 'Action';
if (!method_exists($this, $methodName)) {
throw new \Exception("Method $methodName not found in controller" . get_class($this));
}
if ($this->before() !== false) {
call_user_func_array([$this, $methodName], $args);
$this->after();
}
}
/**
* Before filter - called before an action method.
*
* @return void
*/
protected function before()
{
}
/**
* After filter - called after an action method.
*
* @return void
*/
protected function after()
{
}
}