Merge pull request #64 from skipperbent/development

Development
This commit is contained in:
Simon Sessingø
2016-01-15 11:09:00 +01:00
4 changed files with 50 additions and 8 deletions
+21
View File
@@ -270,6 +270,24 @@ Register the new class in your ```routes.php```, custom ```Router``` class or wh
SimpleRouter::csrfVerifier(new \Demo\Middleware\CsrfVerifier()); SimpleRouter::csrfVerifier(new \Demo\Middleware\CsrfVerifier());
``` ```
## Easily overwrite route about to be loaded
Sometimes it can be useful to manipulate the route that's about to be loaded, for instance if a user is not authenticated or if an error occurred within your Middleware that requires
some other route to be initialised. Simple PHP Router allows you to easily change the route about to be executed. All information about the current route is stored in
the ```\Pecee\SimpleRouter\Http\Request``` object.
**Note:** Please note that it's only possible to change the route BEFORE any route has initially been loaded, so doing this in your custom ExceptionHandler or Middleware is highly recommended.
```php
$route = Request::getInstance()->getLoadedRoute();
$route->setCallback('Example\MyCustomClass@hello');
// -- or --
$route->setClass('Example\MyCustomClass');
$route->setMethod('hello');
```
## Documentation ## Documentation
While I work on a better documentation, please refer to the Laravel 5 routing documentation here: While I work on a better documentation, please refer to the Laravel 5 routing documentation here:
@@ -278,6 +296,9 @@ http://laravel.com/docs/5.1/routing
## Easily extendable ## Easily extendable
The router can be easily extended to customize your needs. The router can be easily extended to customize your needs.
## Ideas and issues
If you want a great new feature or experience any issues what-so-ever, please feel free to leave an issue and i'll look into it whenever possible.
## The MIT License (MIT) ## The MIT License (MIT)
Copyright (c) 2015 Simon Sessingø / simple-php-router Copyright (c) 2015 Simon Sessingø / simple-php-router
+11
View File
@@ -1,6 +1,8 @@
<?php <?php
namespace Pecee\Http; namespace Pecee\Http;
use Pecee\SimpleRouter\RouterBase;
class Request { class Request {
protected static $instance; protected static $instance;
@@ -10,6 +12,7 @@ class Request {
protected $host; protected $host;
protected $method; protected $method;
protected $headers; protected $headers;
protected $loadedRoute;
/** /**
* Return new instance * Return new instance
@@ -140,4 +143,12 @@ class Request {
return isset($this->data[$name]) ? $this->data[$name] : null; return isset($this->data[$name]) ? $this->data[$name] : null;
} }
/**
* Get the currently loaded route.
* @return \Pecee\SimpleRouter\RouterEntry
*/
public function getLoadedRoute() {
return $this->loadedRoute;
}
} }
+8 -8
View File
@@ -15,7 +15,6 @@ class RouterBase {
protected $processedRoutes; protected $processedRoutes;
protected $controllerUrlMap; protected $controllerUrlMap;
protected $backStack; protected $backStack;
protected $loadedRoute;
protected $defaultNamespace; protected $defaultNamespace;
protected $baseCsrfVerifier; protected $baseCsrfVerifier;
@@ -144,9 +143,10 @@ class RouterBase {
$routeNotAllowed = false; $routeNotAllowed = false;
$this->loadedRoute = $route; $this->request->loadedRoute = $route;
$route->loadMiddleware($this->request); $route->loadMiddleware($this->request);
$route->renderRoute($this->request);
$this->request->loadedRoute->renderRoute($this->request);
break; break;
} }
} }
@@ -155,7 +155,7 @@ class RouterBase {
throw new RouterException('Route or method not allowed', 403); throw new RouterException('Route or method not allowed', 403);
} }
if(!$this->loadedRoute) { if(!$this->request->loadedRoute) {
throw new RouterException(sprintf('Route not found: %s', $this->request->getUri()), 404); throw new RouterException(sprintf('Route not found: %s', $this->request->getUri()), 404);
} }
} }
@@ -178,8 +178,8 @@ class RouterBase {
* @return RouterEntry * @return RouterEntry
*/ */
public function getLoadedRoute() { public function getLoadedRoute() {
if(!($this->loadedRoute instanceof RouterGroup)) { if(!($this->request->loadedRoute instanceof RouterGroup)) {
return $this->loadedRoute; return $this->request->loadedRoute;
} }
return null; return null;
} }
@@ -327,8 +327,8 @@ class RouterBase {
return $url; return $url;
} }
if($controller === null && $this->loadedRoute !== null) { if($controller === null && $this->request->loadedRoute !== null) {
return $this->processUrl($this->loadedRoute, $this->loadedRoute->getMethod(), $parameters, $getParams); return $this->processUrl($this->request->loadedRoute, $this->request->loadedRoute->getMethod(), $parameters, $getParams);
} }
$c = ''; $c = '';
+10
View File
@@ -77,6 +77,16 @@ abstract class RouterEntry {
return null; return null;
} }
public function setMethod($method) {
$this->callback = sprintf('%s@%s', $this->getClass(), $method);
return $this;
}
public function setClass($class) {
$this->callback = sprintf('%s@%s', $class, $this->getMethod());
return $this;
}
/** /**
* @param string $prefix * @param string $prefix
* @return self * @return self