1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TechPivot\Phalcon\Enterprise\Dispatcher\Plugins; |
4
|
|
|
|
5
|
|
|
use Phalcon\Text; |
6
|
|
|
use Phalcon\Events\Event; |
7
|
|
|
use Phalcon\Mvc\User\Plugin; |
8
|
|
|
use Phalcon\Mvc\Dispatcher; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* CamelizeActionName. |
12
|
|
|
* |
13
|
|
|
* Typically URL routing uses hyphenated lowercase letters which do not directly map to equivalently |
14
|
|
|
* named controller actions. In order to fix this the action name is converted into camel case prior to |
15
|
|
|
* dispatching the request. |
16
|
|
|
* |
17
|
|
|
* For example, if the original URL is: http://example.com/admin/products/show-latest-products, |
18
|
|
|
* and you want to name your action ‘showLatestProducts’, then this plugin will automatically handle |
19
|
|
|
* converting the uncamelized URL action such that the handler's action method will be properly |
20
|
|
|
* executed when dispatched. |
21
|
|
|
* |
22
|
|
|
* Note: This will handle camelizing everything except not found routes in the dispatcher. |
23
|
|
|
* The router will need to explicitly use the uncamelized form within |
24
|
|
|
* $router::notFound(). |
25
|
|
|
*/ |
26
|
|
|
class CamelizeActionName extends Plugin |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Specifies the delimiter to use at the conclusion of all dispatching to ensure view paths properly resolve. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $uncamelizeDelimiter; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* IndexRedirectorPlugin constructor. |
37
|
|
|
* |
38
|
|
|
* @param string $uncamelizeDelimiter Specifies the delimiter to use at the conclusion of all dispatching |
39
|
|
|
* to ensure view paths properly resolve. |
40
|
|
|
*/ |
41
|
|
|
public function __construct($uncamelizeDelimiter = '-') |
42
|
|
|
{ |
43
|
|
|
$this->uncamelizeDelimiter = $uncamelizeDelimiter; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Triggered before the dispatch loop begins. |
48
|
|
|
* |
49
|
|
|
* Note: The default value for the dispatcher action name is an empty string. We no longer need to |
50
|
|
|
* check for cases where the action could be <tt>null</tt>. |
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
|
|
|
$dispatcher->setActionName(Text::camelize($dispatcher->getActionName())); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* In order for views to respect the uncamelized directory structure the action name must |
62
|
|
|
* be converted back into an uncamelized form. |
63
|
|
|
* |
64
|
|
|
* Note: Until \Phalcon\Text::uncamelize() supports variable delimiter we manually use hyphens. |
65
|
|
|
* |
66
|
|
|
* @link https://github.com/phalcon/cphalcon/issues/10396 |
67
|
|
|
* |
68
|
|
|
* @param \Phalcon\Events\Event $event The beforeDispatchLoop event. |
69
|
|
|
* @param \Phalcon\Mvc\Dispatcher $dispatcher The application dispatcher instance. |
70
|
|
|
*/ |
71
|
|
|
public function afterDispatchLoop(Event $event, Dispatcher $dispatcher) |
72
|
|
|
{ |
73
|
|
|
$dispatcher->setActionName(strtolower(preg_replace('/(.)([A-Z])/', '$1' . |
74
|
|
|
$this->uncamelizeDelimiter . '$2', $dispatcher->getActionName()))); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|