Skip to content
This repository was archived by the owner on Dec 6, 2022. It is now read-only.

Commit d1ce0d9

Browse files
author
Jefersson Nathan
committed
#44 — Guard controller and action name when instantiate the Route object
1 parent 7fb11a8 commit d1ce0d9

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/Route.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,24 @@ class Route
8686
*/
8787
private $action;
8888

89+
/**
90+
* @var string
91+
*/
92+
private $controller;
93+
8994
/**
9095
* @param $resource
9196
* @param array $config
9297
*/
9398
public function __construct($resource, array $config)
9499
{
100+
$this->url = $resource;
101+
$this->config = $config;
102+
103+
list($controller, $action) = explode('::', $this->config['_controller']);
95104

96-
// @todo get action and controller when create the object instance
97-
$this->url = $resource;
98-
$this->config = $config;
105+
$this->controller = $controller;
106+
$this->action = $action;
99107
$this->methods = isset($config['methods']) ? (array) $config['methods'] : array();
100108
$this->target = isset($config['target']) ? $config['target'] : null;
101109
$this->name = isset($config['name']) ? $config['name'] : null;
@@ -205,35 +213,32 @@ public function setParameters(array $parameters)
205213

206214
public function dispatch()
207215
{
208-
list($controller, $action) = explode('::', $this->config['_controller']);
209-
210-
$this->action = !$action && trim($action) !== '' ? $action : null;
211-
212216
if ($this->parametersByName) {
213217
$this->parameters = array($this->parameters);
214218
}
215219

220+
$controller = $this->controller;
221+
216222
if ($this->container && $this->container->has($controller)) {
217223
$instance = $this->container->get($controller);
218224
call_user_func_array(
219-
// @todo action seems to be inconsistent
220-
array($instance, $this->action),
225+
array($instance, $this->getAction()),
221226
$this->parameters
222227
);
223228

224229
return;
225230
}
226231

227-
if (!is_null($this->action)) {
228-
$instance = new $action[0];
229-
call_user_func_array(array($instance, $this->action), $this->parameters);
232+
if (!is_null($this->getAction())) {
233+
$instance = new $controller;
234+
call_user_func_array(array($instance, $this->getAction()), $this->parameters);
230235
} else {
231-
$instance = new $action[0]($this->parameters);
236+
$instance = new $controller($this->parameters);
232237
}
233238
}
234239

235240
public function getAction()
236241
{
237-
return $this->action;
242+
return '' !== trim($this->action) ? $this->action : null;
238243
}
239244
}

0 commit comments

Comments
 (0)