|
1
|
|
|
<?php /** MicroController */ |
|
2
|
|
|
|
|
3
|
|
|
namespace Micro\Mvc\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Micro\Base\Exception; |
|
6
|
|
|
use Micro\Web\Response; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Controller |
|
10
|
|
|
* |
|
11
|
|
|
* @author Oleg Lunegov <[email protected]> |
|
12
|
|
|
* @link https://github.com/lugnsk/micro |
|
13
|
|
|
* @copyright Copyright © 2013 Oleg Lunegov |
|
14
|
|
|
* @license /LICENSE |
|
15
|
|
|
* @package Micro |
|
16
|
|
|
* @subpackage Mvc\Controllers |
|
17
|
|
|
* @version 1.0 |
|
18
|
|
|
* @since 1.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class ViewController extends Controller |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var string $layout */ |
|
23
|
|
|
public $layout; |
|
24
|
|
|
/** @var bool $asWidget */ |
|
25
|
|
|
public $asWidget = false; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @inheritdoc |
|
30
|
|
|
* @throws Exception |
|
31
|
|
|
*/ |
|
32
|
|
|
public function action($name = 'index') |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
// Set widgetStack for widgets |
|
35
|
|
|
if (empty($GLOBALS['widgetStack'])) { |
|
36
|
|
|
$GLOBALS['widgetStack'] = []; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$view = null; |
|
|
|
|
|
|
40
|
|
|
$actionClass = false; |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
if (!method_exists($this, 'action' . ucfirst($name))) { |
|
44
|
|
|
$actionClass = $this->getActionClassByName($name); |
|
45
|
|
|
if (!$actionClass) { |
|
46
|
|
|
throw new Exception('Action "' . $name . '" not found into ' . get_class($this)); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$filters = method_exists($this, 'filters') ? $this->filters() : []; |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
$this->applyFilters($name, true, $filters, null); |
|
53
|
|
|
|
|
54
|
|
View Code Duplication |
if ($actionClass) { |
|
|
|
|
|
|
55
|
|
|
/** @var \Micro\mvc\Action $cl */ |
|
56
|
|
|
$cl = new $actionClass($this->container); |
|
57
|
|
|
$view = $cl->run(); |
|
58
|
|
|
} else { |
|
59
|
|
|
$view = $this->{'action' . ucfirst($name)}(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (is_object($view)) { |
|
63
|
|
|
$view->module = get_class($this->module); |
|
64
|
|
|
$view->layout = $view->layout ?: $this->layout; |
|
65
|
|
|
$view->view = $view->view ?: $name; |
|
66
|
|
|
$view->path = get_called_class(); |
|
67
|
|
|
$view = (string)$view; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$response = $this->container->response ?: new Response; |
|
|
|
|
|
|
71
|
|
|
$response->setBody($this->applyFilters($name, false, $filters, $view)); |
|
72
|
|
|
|
|
73
|
|
|
return $response; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Redirect user to path |
|
78
|
|
|
* |
|
79
|
|
|
* @access public |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $path path to redirect |
|
82
|
|
|
* |
|
83
|
|
|
* @return void|bool |
|
84
|
|
|
*/ |
|
85
|
|
|
public function redirect($path) |
|
86
|
|
|
{ |
|
87
|
|
|
if (!$this->asWidget) { |
|
88
|
|
|
header('Location: ' . $path); |
|
89
|
|
|
exit(); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: