mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 13:32:16 +00:00
[TASK] More fixes to the RouterRessource router.
This commit is contained in:
@@ -183,7 +183,7 @@ abstract class RouterEntry {
|
|||||||
$controller = explode('@', $this->getCallback());
|
$controller = explode('@', $this->getCallback());
|
||||||
$className = $this->getNamespace() . '\\' . $controller[0];
|
$className = $this->getNamespace() . '\\' . $controller[0];
|
||||||
$class = $this->loadClass($className);
|
$class = $this->loadClass($className);
|
||||||
$method = strtolower($requestMethod) . ucfirst($controller[1]);
|
$method = $requestMethod . ucfirst($controller[1]);
|
||||||
|
|
||||||
if (!method_exists($class, $method)) {
|
if (!method_exists($class, $method)) {
|
||||||
throw new RouterException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
|
throw new RouterException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
|
||||||
|
|||||||
@@ -20,7 +20,32 @@ class RouterRessource extends RouterEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function renderRoute($requestMethod) {
|
public function renderRoute($requestMethod) {
|
||||||
return parent::renderRoute($requestMethod);
|
// Load middleware
|
||||||
|
if($this->getMiddleware()) {
|
||||||
|
$this->loadClass($this->getMiddleware());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(is_object($this->getCallback()) && is_callable($this->getCallback())) {
|
||||||
|
|
||||||
|
// When the callback is a function
|
||||||
|
call_user_func_array($this->getCallback(), $this->getParameters());
|
||||||
|
} else {
|
||||||
|
// When the callback is a method
|
||||||
|
$controller = explode('@', $this->getCallback());
|
||||||
|
$className = $this->getNamespace() . '\\' . $controller[0];
|
||||||
|
$class = $this->loadClass($className);
|
||||||
|
$method = strtolower($controller[1]);
|
||||||
|
|
||||||
|
if (!method_exists($class, $method)) {
|
||||||
|
throw new RouterException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
call_user_func_array(array($class, $method), $this->getParameters());
|
||||||
|
|
||||||
|
return $class;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function call($method, $parameters) {
|
protected function call($method, $parameters) {
|
||||||
@@ -50,38 +75,37 @@ class RouterRessource extends RouterEntry {
|
|||||||
if (count($path)) {
|
if (count($path)) {
|
||||||
|
|
||||||
// Delete
|
// Delete
|
||||||
if($this->postMethod == 'DELETE' && $requestMethod == self::REQUEST_TYPE_POST) {
|
if($this->postMethod === 'DELETE' && $requestMethod === self::REQUEST_TYPE_POST) {
|
||||||
return $this->call('destroy', $args);
|
return $this->call('destroy', $args);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update
|
// Update
|
||||||
if(in_array($this->postMethod, array('PUT', 'PATCH')) && $requestMethod == self::REQUEST_TYPE_POST) {
|
if(in_array($this->postMethod, array('PUT', 'PATCH')) && $requestMethod === self::REQUEST_TYPE_POST) {
|
||||||
return $this->call('update', array_merge(array($action), $args));
|
return $this->call('update', array_merge(array($action), $args));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Edit
|
// Edit
|
||||||
if(isset($args[0]) && strtolower($args[0]) == 'edit' && $requestMethod == self::REQUEST_TYPE_GET) {
|
if(isset($args[0]) && strtolower($args[0]) === 'edit' && $requestMethod === self::REQUEST_TYPE_GET) {
|
||||||
return $this->call('edit', array_merge(array($action), array_slice($args, 1)));
|
return $this->call('edit', array_merge(array($action), array_slice($args, 1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create
|
// Create
|
||||||
if(strtolower($action) == 'create' && $this->postMethod == 'GET') {
|
if(strtolower($action) === 'create' && $this->postMethod === 'GET') {
|
||||||
return $this->call('create', $args);
|
return $this->call('create', $args);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save
|
// Save
|
||||||
if($requestMethod == 'POST') {
|
if($requestMethod === self::REQUEST_TYPE_POST) {
|
||||||
return $this->call('store', $args);
|
return $this->call('store', $args);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show
|
// Show
|
||||||
if($action && $requestMethod == 'GET') {
|
if($action && $requestMethod === self::REQUEST_TYPE_GET) {
|
||||||
return $this->call('show', array_merge(array($action), $args));
|
return $this->call('show', array_merge(array($action), $args));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Index
|
// Index
|
||||||
return $this->call('index', $args);
|
return $this->call('index', $args);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user