1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TechPivot\Phalcon\Enterprise\Dispatcher\Plugins; |
4
|
|
|
|
5
|
|
|
use Phalcon\Events\Event; |
6
|
|
|
use Phalcon\Mvc\Dispatcher; |
7
|
|
|
use Phalcon\Mvc\Dispatcher\Exception; |
8
|
|
|
use Phalcon\Mvc\User\Plugin; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* IndexRedirector. |
12
|
|
|
* |
13
|
|
|
* Ensures that any dispatched route that includes the explicit default index, typically "index", as the |
14
|
|
|
* action or controller is prohibited. The default behavior of allowing the explicit default index is |
15
|
|
|
* a by product of Phalcon's routing system that will allow explicitly matched controllers and actions. |
16
|
|
|
* |
17
|
|
|
* Prohibiting this behavior is useful in ensuring accurate Search Engine Optimization as well as |
18
|
|
|
* providing a level of increased security as default routes can expose information about the underlying |
19
|
|
|
* system architecture. |
20
|
|
|
*/ |
21
|
|
|
class IndexRedirector extends Plugin |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The default action index. Defaults to "index" |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $defaultActionIndex; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The default handler index. Defaults to "index" |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $defaultHandlerIndex; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* IndexRedirectorPlugin constructor. |
39
|
|
|
* |
40
|
|
|
* @param string $defaultActionIndex The default action index. Defaults to "index" |
41
|
|
|
* @param string $defaultHandlerIndex The default handler index. Defaults to "index" |
42
|
|
|
*/ |
43
|
|
|
public function __construct($defaultActionIndex = 'index', $defaultHandlerIndex = 'index') |
44
|
|
|
{ |
45
|
|
|
$this->defaultActionIndex = $defaultActionIndex; |
46
|
|
|
$this->defaultHandlerIndex = $defaultHandlerIndex; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Triggered before the dispatch loop begins. |
51
|
|
|
* |
52
|
|
|
* @param \Phalcon\Events\Event $event The beforeDispatchLoop event. |
53
|
|
|
* @param \Phalcon\Mvc\Dispatcher $dispatcher The application dispatcher instance. |
54
|
|
|
*/ |
55
|
|
|
public function beforeDispatchLoop(Event $event, Dispatcher $dispatcher) |
56
|
|
|
{ |
57
|
|
|
/** @var \Phalcon\Mvc\RouterInterface $router */ |
58
|
|
|
$router = $this->getDI()->getShared('router'); |
59
|
|
|
|
60
|
|
|
// Action Validation |
61
|
|
|
switch ($router->getActionName()) { |
62
|
|
|
case $this->defaultActionIndex: |
63
|
|
|
$exception = new Exception('Action "index" was not found on handler "' . |
64
|
|
|
$dispatcher->getControllerName() . '"', Dispatcher::EXCEPTION_HANDLER_NOT_FOUND); |
65
|
|
|
|
66
|
|
|
$eventsManager = $dispatcher->getEventsManager(); |
67
|
|
|
if ($eventsManager->fire('dispatch:beforeException', $dispatcher, $exception) === false) { |
68
|
|
|
break; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
throw $exception; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Handler Validation |
75
|
|
|
switch ($router->getControllerName()) { |
76
|
|
|
case $this->defaultHandlerIndex: |
77
|
|
|
// Allow the default pattern |
78
|
|
|
if ($router->getMatchedRoute()->getPattern() === '/') { |
79
|
|
|
break; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$exception = new Exception('"' . $dispatcher->getNamespaceName() . |
83
|
|
|
'\IndexController" handler class cannot be loaded', Dispatcher::EXCEPTION_HANDLER_NOT_FOUND); |
84
|
|
|
|
85
|
|
|
$eventsManager = $dispatcher->getEventsManager(); |
86
|
|
|
if ($eventsManager->fire('dispatch:beforeException', $dispatcher, $exception) === false) { |
87
|
|
|
break; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
throw $exception; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|