mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 08:47:52 +00:00
[FEATURE] ResourceControllers now support nested ressources
This commit is contained in:
@@ -5,11 +5,8 @@ use Pecee\Http\Request;
|
|||||||
|
|
||||||
class RouterResource extends RouterEntry {
|
class RouterResource extends RouterEntry {
|
||||||
|
|
||||||
const DEFAULT_METHOD = 'index';
|
|
||||||
|
|
||||||
protected $url;
|
protected $url;
|
||||||
protected $controller;
|
protected $controller;
|
||||||
protected $method;
|
|
||||||
protected $postMethod;
|
protected $postMethod;
|
||||||
|
|
||||||
public function __construct($url, $controller) {
|
public function __construct($url, $controller) {
|
||||||
@@ -52,55 +49,51 @@ class RouterResource extends RouterEntry {
|
|||||||
$url = parse_url($request->getUri());
|
$url = parse_url($request->getUri());
|
||||||
$url = rtrim($url['path'], '/') . '/';
|
$url = rtrim($url['path'], '/') . '/';
|
||||||
|
|
||||||
if(strtolower($url) == strtolower($this->url) || stripos($url, $this->url) === 0) {
|
$route = rtrim($this->url, '/') . '/{id?}/{action?}';
|
||||||
$url = rtrim($url, '/');
|
|
||||||
|
|
||||||
$strippedUrl = trim(substr($url, strlen($this->url)), '/');
|
$parameters = $this->parseParameters($route, $url, '[0-9]*?');
|
||||||
$path = explode('/', $strippedUrl);
|
|
||||||
|
|
||||||
$args = $path;
|
if($parameters !== null) {
|
||||||
$action = '';
|
|
||||||
|
|
||||||
if(count($args)) {
|
if(is_array($parameters)) {
|
||||||
$action = $args[0];
|
$parameters = array_merge($this->parameters, $parameters);
|
||||||
array_shift($args);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count($path)) {
|
$action = $parameters['action'];
|
||||||
|
unset($parameters['action']);
|
||||||
|
|
||||||
// Delete
|
// Delete
|
||||||
if($request->getMethod() === self::REQUEST_TYPE_DELETE && $this->postMethod === self::REQUEST_TYPE_POST) {
|
if($request->getMethod() === self::REQUEST_TYPE_DELETE && $this->postMethod === self::REQUEST_TYPE_POST) {
|
||||||
return $this->call('destroy', $args);
|
return $this->call('destroy', $parameters);
|
||||||
}
|
|
||||||
|
|
||||||
// Update
|
|
||||||
if(in_array($request->getMethod(), array('put', 'patch')) && $this->postMethod === self::REQUEST_TYPE_POST) {
|
|
||||||
return $this->call('update', array_merge(array($action), $args));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Edit
|
|
||||||
if(isset($args[0]) && strtolower($args[0]) === 'edit' && $this->postMethod === self::REQUEST_TYPE_GET) {
|
|
||||||
return $this->call('edit', array_merge(array($action), array_slice($args, 1)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create
|
|
||||||
if(strtolower($action) === 'create' && $request->getMethod() === self::REQUEST_TYPE_GET) {
|
|
||||||
return $this->call('create', $args);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save
|
|
||||||
if($this->postMethod === self::REQUEST_TYPE_POST) {
|
|
||||||
return $this->call('store', $args);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Show
|
|
||||||
if($action && $this->postMethod === self::REQUEST_TYPE_GET) {
|
|
||||||
return $this->call('show', array_merge(array($action), $args));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Index
|
|
||||||
return $this->call('index', $args);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update
|
||||||
|
if(in_array($request->getMethod(), array(self::REQUEST_TYPE_PATCH, self::REQUEST_TYPE_PUT)) && $this->postMethod === self::REQUEST_TYPE_POST) {
|
||||||
|
return $this->call('update', $parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Edit
|
||||||
|
if(isset($action) && strtolower($action) === 'edit' && $this->postMethod === self::REQUEST_TYPE_GET) {
|
||||||
|
return $this->call('edit', $parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create
|
||||||
|
if(strtolower($action) === 'create' && $request->getMethod() === self::REQUEST_TYPE_GET) {
|
||||||
|
return $this->call('create', $parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save
|
||||||
|
if($this->postMethod === self::REQUEST_TYPE_POST) {
|
||||||
|
return $this->call('store', $parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show
|
||||||
|
if($action && $this->postMethod === self::REQUEST_TYPE_GET) {
|
||||||
|
return $this->call('show', $parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Index
|
||||||
|
return $this->call('index', $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@@ -135,18 +128,4 @@ class RouterResource extends RouterEntry {
|
|||||||
$this->controller = $controller;
|
$this->controller = $controller;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getMethod() {
|
|
||||||
return $this->method;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $method
|
|
||||||
*/
|
|
||||||
public function setMethod($method) {
|
|
||||||
$this->method = $method;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user