Merge pull request #53 from skipperbent/feature-resource

[FEATURE] ResourceControllers now support nested ressources
This commit is contained in:
Simon Sessingø
2015-12-19 16:28:06 +01:00
5 changed files with 86 additions and 86 deletions
+5 -4
View File
@@ -1,8 +1,8 @@
# Simple PHP router # Simple PHP router
Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the Laravel router. Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the way Laravel handles routing.
## Installation ## Installation
Add the latest version pf Simple PHP Router to your ```composer.json``` Add the latest version of Simple PHP Router to your ```composer.json```
```json ```json
{ {
@@ -203,7 +203,8 @@ class Router extends SimpleRouter {
} }
``` ```
This is a basic example of a helper function for generating urls. #### Helper functions examples
**This is a basic example of a helper function for generating urls.**
```php ```php
use Pecee\SimpleRouter\RouterBase; use Pecee\SimpleRouter\RouterBase;
@@ -212,7 +213,7 @@ function url($controller, $parameters = null, $getParams = null) {
} }
``` ```
This is a basic example for getting the current csrf token **This is a basic example for getting the current csrf token**
```php ```php
/** /**
+37 -19
View File
@@ -45,7 +45,6 @@ class RouterBase {
protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), $backStack = false, $group = null) { protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), $backStack = false, $group = null) {
// Loop through each route-request // Loop through each route-request
$activeGroup = null;
$routesCount = count($routes); $routesCount = count($routes);
$mergedSettings = array(); $mergedSettings = array();
@@ -54,6 +53,7 @@ class RouterBase {
$route = $routes[$i]; $route = $routes[$i];
$route->addSettings($settings);
$route->setGroup($group); $route->setGroup($group);
if($this->defaultNamespace && !$route->getNamespace()) { if($this->defaultNamespace && !$route->getNamespace()) {
@@ -73,21 +73,20 @@ class RouterBase {
array_push($newPrefixes, rtrim($route->getPrefix(), '/')); array_push($newPrefixes, rtrim($route->getPrefix(), '/'));
} }
$route->addSettings($settings);
if(!($route instanceof RouterGroup)) { if(!($route instanceof RouterGroup)) {
if(is_array($newPrefixes) && count($newPrefixes) && $backStack) { if(is_array($newPrefixes) && count($newPrefixes) && $backStack) {
$route->setUrl( join('/', $newPrefixes) . $route->getUrl() ); $route->setUrl( join('/', $newPrefixes) . $route->getUrl() );
} }
$group = null;
$this->controllerUrlMap[] = $route; $this->controllerUrlMap[] = $route;
} }
$this->currentRoute = $route; $this->currentRoute = $route;
if($route instanceof RouterGroup && is_callable($route->getCallback())) { if($route instanceof RouterGroup && is_callable($route->getCallback())) {
$group = $route;
$route->renderRoute($this->request); $route->renderRoute($this->request);
$activeGroup = $route;
$mergedSettings = array_merge($route->getMergeableSettings(), $settings); $mergedSettings = array_merge($route->getMergeableSettings(), $settings);
} }
@@ -98,7 +97,7 @@ class RouterBase {
$this->backStack = array(); $this->backStack = array();
// Route any routes added to the backstack // Route any routes added to the backstack
$this->processRoutes($backStack, $mergedSettings, $newPrefixes, true, $activeGroup); $this->processRoutes($backStack, $mergedSettings, $newPrefixes, true, $group);
} }
} }
} }
@@ -239,7 +238,20 @@ class RouterBase {
protected function processUrl($route, $method = null, $parameters = null, $getParams = null) { protected function processUrl($route, $method = null, $parameters = null, $getParams = null) {
$url = '/' . trim($route->getUrl(), '/'); $domain = '';
if($route->getGroup() !== null && $route->getGroup()->getDomain() !== null) {
if(is_array($route->getGroup()->getDomain())) {
$domains = $route->getGroup()->getDomain();
$domain = array_shift($domains);
} else {
$domain = $route->getGroup()->getDomain();
}
$domain = '//' . $domain;
}
$url = $domain . '/' . trim($route->getUrl(), '/');
if(($route instanceof RouterController || $route instanceof RouterResource) && $method !== null) { if(($route instanceof RouterController || $route instanceof RouterResource) && $method !== null) {
$url .= $method; $url .= $method;
@@ -251,20 +263,26 @@ class RouterBase {
} }
} else { } else {
/* @var $route RouterEntry */ /* @var $route RouterEntry */
$params = $route->getParameters(); if(is_array($parameters)) {
if(count($params)) { $params = array_merge($route->getParameters(), $parameters);
$i = 0;
foreach($params as $param => $value) {
$value = (isset($parameters[$param])) ? $parameters[$param] : $value;
$url = str_ireplace(array('{' . $param. '}', '{' . $param. '?}'), $value, $url);
$i++;
}
} else { } else {
// If no parameters are specified in the route, assume that the provided parameters should be used. $params = $route->getParameters();
if(count($parameters)) {
$url = rtrim($url, '/') . '/' . join('/', $parameters);
}
} }
$otherParams = [];
$i = 0;
foreach($params as $param => $value) {
$value = (isset($parameters[$param])) ? $parameters[$param] : $value;
if(stripos($url, '{' . $param. '}') !== false || stripos($url, '{' . $param . '?}') !== false) {
$url = str_ireplace(array('{' . $param . '}', '{' . $param . '?}'), $value, $url);
} else {
$otherParams[$param] = $value;
}
$i++;
}
$url = rtrim($url, '/') . '/' . join('/', $otherParams);
} }
$url = rtrim($url, '/') . '/'; $url = rtrim($url, '/') . '/';
@@ -287,7 +305,7 @@ class RouterBase {
} }
// Return current route if no options has been specified // Return current route if no options has been specified
if($controller === null && $parameters === null && $this->loadedRoute !== null) { if($controller === null && $parameters === null) {
$getParams = (is_array($getParams)) ? array_merge($_GET, $getParams) : $_GET; $getParams = (is_array($getParams)) ? array_merge($_GET, $getParams) : $_GET;
$url = parse_url(Request::getInstance()->getUri()); $url = parse_url(Request::getInstance()->getUri());
+1 -1
View File
@@ -277,7 +277,7 @@ abstract class RouterEntry {
// Check for optional parameter // Check for optional parameter
if($lastCharacter === '?') { if($lastCharacter === '?') {
$parameter = substr($parameter, 0, strlen($parameter)-1); $parameter = substr($parameter, 0, strlen($parameter)-1);
$regex .= '(?:(?:\/{0,1}(?P<'.$parameter.'>'.$parameterRegex.')){0,1}\\/{0,1})'; $regex .= '(?:(?:\/{0,1}(?P<'.$parameter.'>[^\/]*)))\\/{0,1}';
$required = false; $required = false;
} else { } else {
$regex .= '(?:\\/{0,1}(?P<' . $parameter . '>'. $parameterRegex .')\\/{0,1})'; $regex .= '(?:\\/{0,1}(?P<' . $parameter . '>'. $parameterRegex .')\\/{0,1})';
+5 -3
View File
@@ -60,9 +60,7 @@ class RouterGroup extends RouterEntry {
throw new RouterException('Method not allowed'); throw new RouterException('Method not allowed');
} }
if($this->matchDomain($request) === null) { $this->matchDomain($request);
return null;
}
return parent::renderRoute($request); return parent::renderRoute($request);
} }
@@ -80,4 +78,8 @@ class RouterGroup extends RouterEntry {
return $this->exceptionHandler; return $this->exceptionHandler;
} }
public function getDomain() {
return $this->domain;
}
} }
+38 -59
View File
@@ -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;
}
} }