mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 16:57:53 +00:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 258d9d05c7 | |||
| 2cc97c120f | |||
| 7e7319de06 | |||
| 5ad7dcf9fd | |||
| 531b35532b | |||
| dfd32c0904 | |||
| c258f937e8 | |||
| efe5767220 | |||
| 5415d73f4d | |||
| 60c0bd6355 | |||
| 8370d3d94e | |||
| df3acb6605 |
@@ -158,11 +158,11 @@ namespace Demo\Handlers;
|
||||
use Pecee\Handlers\IExceptionHandler;
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||
use Pecee\SimpleRouter\RouterEntry;
|
||||
use Pecee\SimpleRouter\Route\ILoadableRoute;
|
||||
|
||||
class CustomExceptionHandler implements IExceptionHandler
|
||||
{
|
||||
public function handleError(Request $request, RouterEntry &$route = null, \Exception $error)
|
||||
public function handleError(Request $request, ILoadableRoute &$route = null, \Exception $error)
|
||||
{
|
||||
|
||||
/* You can use the exception handler to format errors depending on the request and type. */
|
||||
@@ -214,18 +214,19 @@ Route::group(['domain' => '{account}.myapp.com'], function () {
|
||||
|
||||
The prefix group array attribute may be used to prefix each route in the group with a given URI. For example, you may want to prefix all route URIs within the group with admin:
|
||||
|
||||
### Doing it the object oriented (hardcore) way
|
||||
### Adding routes manually (with no helper)
|
||||
|
||||
The ```SimpleRouter``` class referenced in the previous example, is just a simple helper class that knows how to communicate with the ```RouterBase``` class.
|
||||
The ```SimpleRouter``` class referenced in the previous example, is just a simple helper class that knows how to communicate with the ```Router``` class.
|
||||
If you are up for a challenge, want the full control or simply just want to create your own ```Router``` helper class, this example is for you.
|
||||
|
||||
```php
|
||||
use \Pecee\SimpleRouter\RouterBase;
|
||||
use \Pecee\SimpleRouter\RouterRoute;
|
||||
use \Pecee\SimpleRouter\Router;
|
||||
use \Pecee\SimpleRouter\Route\RouteUrl;
|
||||
|
||||
$router = RouterBase::getInstance();
|
||||
/* Grap the router instance */
|
||||
$router = Router::getInstance();
|
||||
|
||||
$route = new RouterRoute('/answer/1', function() {
|
||||
$route = new RouteUrl('/answer/1', function() {
|
||||
|
||||
die('this callback will match /answer/1');
|
||||
|
||||
@@ -239,9 +240,11 @@ $route->setPrefix('v1');
|
||||
$router->addRoute($route);
|
||||
```
|
||||
|
||||
### Extending
|
||||
|
||||
This is a simple example of an integration into a framework.
|
||||
|
||||
The framework has it's own ```Router``` class which inherits from the ```SimpleRouter``` class. This allows the framework to add custom functionality.
|
||||
The framework has it's own ```Router``` class which inherits from the ```SimpleRouter``` class. This allows the framework to add custom functionality like loading a custom `routes.php` file or add debugging information etc.
|
||||
|
||||
```php
|
||||
namespace Demo;
|
||||
@@ -330,22 +333,78 @@ function input() {
|
||||
|
||||
## Getting urls
|
||||
|
||||
**In ```routes.php``` we have added this route:**
|
||||
By default all controller and resource routes will use a simplified version of their url as name.
|
||||
|
||||
**Get routes using custom name (single route)**
|
||||
|
||||
```php
|
||||
SimpleRouter::get('/item/{id}', 'myController@show', ['as' => 'item']);
|
||||
SimpleRouter::get('/product-view/{id}', 'ProductsController@show', ['as' => 'product']);
|
||||
|
||||
url('product', ['id' => 22], ['category' => 'shoes']);
|
||||
|
||||
# output
|
||||
# /product-view/22/?category=shoes
|
||||
```
|
||||
|
||||
**In the template we then call:**
|
||||
**Getting the url using the name (controller route)**
|
||||
|
||||
```php
|
||||
url('item', ['id' => 22], ['category' => 'shoes']);
|
||||
SimpleRouter::controller('/images', 'ImagesController', ['as' => 'picture']);
|
||||
|
||||
url('picture@getView', null, ['category' => 'shoes']);
|
||||
url('picture', 'getView', ['category' => 'shoes']);
|
||||
|
||||
# output
|
||||
# /images/view/?category=shows
|
||||
# /images/view/?category=shows
|
||||
```
|
||||
|
||||
**Result url is:**
|
||||
**Getting the url using class**
|
||||
|
||||
```php
|
||||
/item/22/?category=shoes
|
||||
SimpleRouter::get('/product-view/{id}', 'ProductsController@show', ['as' => 'product']);
|
||||
SimpleRouter::controller('/images', 'ImagesController');
|
||||
|
||||
url('ProductsController@show', ['id' => 22], ['category' => 'shoes']);
|
||||
url('ImagesController@getImage', null, ['id' => 22]);
|
||||
|
||||
# output
|
||||
# /product-view/22/?category=shoes
|
||||
# /images/image/?category=shows
|
||||
```
|
||||
|
||||
**Using custom names for methods on a controller/resource route**
|
||||
|
||||
```php
|
||||
SimpleRouter::controller('gadgets', 'GadgetsController', ['names' => ['getIphoneInfo' => 'iphone']]);
|
||||
|
||||
url('gadgets.iphone');
|
||||
|
||||
# output
|
||||
# /gadgets/iphoneinfo/
|
||||
```
|
||||
|
||||
**Getting REST/resource controller urls**
|
||||
|
||||
```php
|
||||
SimpleRouter::resource('/phones', 'PhonesController');
|
||||
|
||||
url('phones');
|
||||
url('phones.index');
|
||||
url('phones.create');
|
||||
url('phones.edit');
|
||||
|
||||
// etc..
|
||||
|
||||
# output
|
||||
# /phones/
|
||||
# /phones/create/
|
||||
# /phones/edit/
|
||||
```
|
||||
|
||||
**Return the current url**
|
||||
```php
|
||||
url();
|
||||
```
|
||||
|
||||
## Custom CSRF verifier
|
||||
@@ -379,13 +438,13 @@ SimpleRouter::csrfVerifier(new \Demo\Middleware\CsrfVerifier());
|
||||
|
||||
Sometimes it can be necessary to keep urls stored in the database, file or similar. In this example, we want the url ```/my-cat-is-beatiful``` to load the route ```/article/view/1``` which the router knows, because it's defined in the ```routes.php``` file.
|
||||
|
||||
To interfere with the router, we create a class that inherits from ```RouterBootManager```. This class will be loaded before any other rules in ```routes.php``` and allow us to "change" the current route, if any of our criteria are fulfilled (like coming from the url ```/my-cat-is-beatiful```).
|
||||
To interfere with the router, we create a class that implements the ```IRouterBootManager``` interface. This class will be loaded before any other rules in ```routes.php``` and allow us to "change" the current route, if any of our criteria are fulfilled (like coming from the url ```/my-cat-is-beatiful```).
|
||||
|
||||
```php
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\RouterBootManager;
|
||||
use Pecee\SimpleRouter\IRouterBootManager;
|
||||
|
||||
class CustomRouterRules extends RouterBootManager {
|
||||
class CustomRouterRules implement IRouterBootManager {
|
||||
|
||||
public function boot(Request $request) {
|
||||
|
||||
@@ -421,7 +480,7 @@ The last thing we need to do, is to add our custom boot-manager to the ```routes
|
||||
## Easily overwrite route about to be loaded
|
||||
Sometimes it can be useful to manipulate the route about to be loaded.
|
||||
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\RouterBase``` instance's `loadedRoute` property.
|
||||
All information about the current route is stored in the ```\Pecee\SimpleRouter\Router``` instance's `loadedRoute` property.
|
||||
|
||||
For easy access you can use the shortcut method `\Pecee\SimpleRouter\SimpleRouter::router()`.
|
||||
|
||||
@@ -442,7 +501,7 @@ $route->setMethod('hello');
|
||||
### Examples
|
||||
|
||||
It's only possible to change the route BEFORE the route has initially been loaded. If you want to redirect to another route, we highly recommend that you
|
||||
modify the `RouterEntry` object from a `Middleware` or `ExceptionHandler`, like the examples below.
|
||||
modify the `IRoute` object from a `Middleware` or `ExceptionHandler`, like the examples below.
|
||||
|
||||
#### Rewriting to new route
|
||||
|
||||
@@ -456,11 +515,11 @@ namespace Demo\Middlewares;
|
||||
|
||||
use Pecee\Http\Middleware\IMiddleware;
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\RouterEntry;
|
||||
use Pecee\SimpleRouter\Route\ILoadableRoute;
|
||||
|
||||
class CustomMiddleware implements Middleware {
|
||||
|
||||
public function handle(Request $request, RouterEntry &$route) {
|
||||
public function handle(Request $request, ILoadableRoute &$route) {
|
||||
|
||||
$request->setUri(url('home'));
|
||||
|
||||
@@ -484,11 +543,11 @@ namespace Demo\Middlewares;
|
||||
|
||||
use Pecee\Http\Middleware\IMiddleware;
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\RouterEntry;
|
||||
use Pecee\SimpleRouter\Route\ILoadableRoute;
|
||||
|
||||
class CustomMiddleware implements Middleware {
|
||||
|
||||
public function handle(Request $request, RouterEntry &$route) {
|
||||
public function handle(Request $request, ILoadableRoute &$route) {
|
||||
|
||||
$route->callback('DefaultController@home');
|
||||
|
||||
|
||||
@@ -4,11 +4,11 @@ namespace Demo\Handlers;
|
||||
use Pecee\Handlers\IExceptionHandler;
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||
use Pecee\SimpleRouter\RouterEntry;
|
||||
use Pecee\SimpleRouter\Route\ILoadableRoute;
|
||||
|
||||
class CustomExceptionHandler implements IExceptionHandler
|
||||
{
|
||||
public function handleError(Request $request, RouterEntry &$route = null, \Exception $error)
|
||||
public function handleError(Request $request, ILoadableRoute &$route = null, \Exception $error)
|
||||
{
|
||||
|
||||
/* You can use the exception handler to format errors depending on the request and type. */
|
||||
|
||||
@@ -3,11 +3,11 @@ namespace Demo\Middlewares;
|
||||
|
||||
use Pecee\Http\Middleware\IMiddleware;
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\RouterEntry;
|
||||
use Pecee\SimpleRouter\Route\ILoadableRoute;
|
||||
|
||||
class ApiVerification implements IMiddleware
|
||||
{
|
||||
public function handle(Request $request, RouterEntry &$route)
|
||||
public function handle(Request $request, ILoadableRoute &$route)
|
||||
{
|
||||
// Do authentication
|
||||
$request->authenticated = true;
|
||||
|
||||
@@ -3,7 +3,7 @@ use Pecee\SimpleRouter\SimpleRouter;
|
||||
|
||||
function url($controller, $parameters = null, $getParams = null)
|
||||
{
|
||||
SimpleRouter::getRoute($controller, $parameters, $getParams);
|
||||
SimpleRouter::getUrl($controller, $parameters, $getParams);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,16 +2,16 @@
|
||||
namespace Pecee\Handlers;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\RouterEntry;
|
||||
use Pecee\SimpleRouter\Route\ILoadableRoute;
|
||||
|
||||
interface IExceptionHandler
|
||||
{
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param RouterEntry|null $route
|
||||
* @param ILoadableRoute $route
|
||||
* @param \Exception $error
|
||||
* @return Request|null
|
||||
*/
|
||||
public function handleError(Request $request, RouterEntry &$route = null, \Exception $error);
|
||||
public function handleError(Request $request, ILoadableRoute &$route = null, \Exception $error);
|
||||
|
||||
}
|
||||
@@ -4,7 +4,7 @@ namespace Pecee\Http\Middleware;
|
||||
use Pecee\CsrfToken;
|
||||
use Pecee\Exceptions\TokenMismatchException;
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\RouterEntry;
|
||||
use Pecee\SimpleRouter\Route\ILoadableRoute;
|
||||
|
||||
class BaseCsrfVerifier implements IMiddleware
|
||||
{
|
||||
@@ -51,7 +51,7 @@ class BaseCsrfVerifier implements IMiddleware
|
||||
return false;
|
||||
}
|
||||
|
||||
public function handle(Request $request, RouterEntry &$route = null)
|
||||
public function handle(Request $request, ILoadableRoute &$route = null)
|
||||
{
|
||||
|
||||
if ($request->getMethod() !== 'get' && !$this->skip($request)) {
|
||||
|
||||
@@ -2,15 +2,15 @@
|
||||
namespace Pecee\Http\Middleware;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\RouterEntry;
|
||||
use Pecee\SimpleRouter\Route\ILoadableRoute;
|
||||
|
||||
interface IMiddleware
|
||||
{
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param RouterEntry $route
|
||||
* @param ILoadableRoute $route
|
||||
* @return Request|null
|
||||
*/
|
||||
public function handle(Request $request, RouterEntry &$route);
|
||||
public function handle(Request $request, ILoadableRoute &$route);
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
<?php
|
||||
namespace Pecee\SimpleRouter;
|
||||
|
||||
interface IControllerRoute
|
||||
{
|
||||
public function getController();
|
||||
|
||||
public function setController($controller);
|
||||
|
||||
public function getMethod();
|
||||
|
||||
public function setMethod($method);
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
<?php
|
||||
namespace Pecee\SimpleRouter;
|
||||
|
||||
interface ILoadableRoute
|
||||
{
|
||||
public function getUrl();
|
||||
|
||||
public function setUrl($url);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace Pecee\SimpleRouter;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
|
||||
interface IRouterBootManager
|
||||
{
|
||||
/**
|
||||
* Called when router loads it's routes
|
||||
*
|
||||
* @param Request $request
|
||||
* @return Request
|
||||
*/
|
||||
public function boot(Request $request);
|
||||
}
|
||||
@@ -1,151 +0,0 @@
|
||||
<?php
|
||||
namespace Pecee\SimpleRouter;
|
||||
|
||||
abstract class LoadableRoute extends RouterEntry implements ILoadableRoute
|
||||
{
|
||||
const PARAMETERS_REGEX_MATCH = '%s([\w\-\_]*?)\%s{0,1}%s';
|
||||
const PARAMETER_MODIFIERS = '{}';
|
||||
const PARAMETER_OPTIONAL_SYMBOL = '?';
|
||||
|
||||
protected $url;
|
||||
protected $names = [];
|
||||
|
||||
public function getUrl()
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set url
|
||||
*
|
||||
* @param string $url
|
||||
* @return static
|
||||
*/
|
||||
public function setUrl($url)
|
||||
{
|
||||
$this->url = ($url === '/') ? '/' : '/' . trim($url, '/') . '/';
|
||||
$regex = sprintf(static::PARAMETERS_REGEX_MATCH, static::PARAMETER_MODIFIERS[0], static::PARAMETER_OPTIONAL_SYMBOL, static::PARAMETER_MODIFIERS[1]);
|
||||
|
||||
if (preg_match_all('/' . $regex . '/is', $this->url, $matches)) {
|
||||
foreach ($matches[1] as $key) {
|
||||
$this->parameters[$key] = null;
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the provided name of the router (first if multiple).
|
||||
* Alias for LoadableRoute::getName().
|
||||
*
|
||||
* @see LoadableRoute::getName()
|
||||
* @return string|array
|
||||
*/
|
||||
public function getAlias()
|
||||
{
|
||||
return $this->getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the provided name for the router (first if multiple).
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->names[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get route names
|
||||
* @return array
|
||||
*/
|
||||
public function getNames()
|
||||
{
|
||||
return $this->names;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if route has given name.
|
||||
* Alias for LoadableRoute::hasName();
|
||||
*
|
||||
* @see LoadableRoute::hasName()
|
||||
* @param $name
|
||||
*/
|
||||
public function hasAlias($name)
|
||||
{
|
||||
$this->hasName($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if route has given name.
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasName($name)
|
||||
{
|
||||
return (in_array($name, $this->names, false) !== false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the router name, which makes it easier to obtain the url or router at a later point.
|
||||
* Alias for LoadableRoute::setName().
|
||||
*
|
||||
* @see LoadableRoute::setName()
|
||||
* @param string|array $name
|
||||
* @return static
|
||||
*/
|
||||
public function setAlias($name)
|
||||
{
|
||||
return $this->setName($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the router name, which makes it easier to obtain the url or router at a later point.
|
||||
*
|
||||
* @param string $name
|
||||
* @return static $this
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
array_push($this->names, $name);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set multiple names for the route
|
||||
*
|
||||
* @param array $names
|
||||
* @return static $this
|
||||
*/
|
||||
public function setNames(array $names)
|
||||
{
|
||||
$this->names = $names;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge with information from another route.
|
||||
*
|
||||
* @param array $values
|
||||
* @return static
|
||||
*/
|
||||
public function merge(array $values)
|
||||
{
|
||||
if (isset($values['as'])) {
|
||||
$this->setNames((array)$values['as']);
|
||||
}
|
||||
|
||||
if (isset($values['prefix'])) {
|
||||
$this->setUrl($values['prefix'] . $this->getUrl());
|
||||
}
|
||||
|
||||
parent::merge($values);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace Pecee\SimpleRouter\Route;
|
||||
|
||||
interface IControllerRoute extends IRoute
|
||||
{
|
||||
/**
|
||||
* Get controller class-name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getController();
|
||||
|
||||
/**
|
||||
* Set controller class-name
|
||||
*
|
||||
* @param string $controller
|
||||
* @return static
|
||||
*/
|
||||
public function setController($controller);
|
||||
|
||||
/**
|
||||
* Return active method
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMethod();
|
||||
|
||||
/**
|
||||
* Set active method
|
||||
*
|
||||
* @param string $method
|
||||
* @return static
|
||||
*/
|
||||
public function setMethod($method);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
namespace Pecee\SimpleRouter\Route;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
|
||||
interface IGroupRoute extends IRoute
|
||||
{
|
||||
/**
|
||||
* Method called to check if a domain matches
|
||||
*
|
||||
* @param Request $request
|
||||
* @return bool
|
||||
*/
|
||||
public function matchDomain(Request $request);
|
||||
|
||||
/**
|
||||
* Set exception-handlers for group
|
||||
*
|
||||
* @param array $handlers
|
||||
* @return static $this
|
||||
*/
|
||||
public function setExceptionHandlers(array $handlers);
|
||||
|
||||
/**
|
||||
* Get exception-handlers for group
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getExceptionHandlers();
|
||||
|
||||
/**
|
||||
* Get domains for domain.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getDomains();
|
||||
|
||||
/**
|
||||
* Set allowed domains for group.
|
||||
*
|
||||
* @param array $domains
|
||||
* @return $this
|
||||
*/
|
||||
public function setDomains(array $domains);
|
||||
|
||||
/**
|
||||
* Set prefix that child-routes will inherit.
|
||||
*
|
||||
* @param string $prefix
|
||||
* @return string
|
||||
*/
|
||||
public function setPrefix($prefix);
|
||||
|
||||
/**
|
||||
* Get prefix.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPrefix();
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
namespace Pecee\SimpleRouter\Route;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
|
||||
interface ILoadableRoute extends IRoute
|
||||
{
|
||||
/**
|
||||
* Find url that matches method, parameters or name.
|
||||
* Used when calling the url() helper.
|
||||
*
|
||||
* @param string|null $method
|
||||
* @param array|null $parameters
|
||||
* @param string|null $name
|
||||
* @return string
|
||||
*/
|
||||
public function findUrl($method = null, $parameters = null, $name = null);
|
||||
|
||||
/**
|
||||
* Loads and renders middlewares-classes
|
||||
*
|
||||
* @param Request $request
|
||||
* @param ILoadableRoute $route
|
||||
*/
|
||||
public function loadMiddleware(Request $request, ILoadableRoute &$route);
|
||||
|
||||
public function getUrl();
|
||||
|
||||
public function setUrl($url);
|
||||
|
||||
/**
|
||||
* Returns the provided name for the router.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName();
|
||||
|
||||
/**
|
||||
* Check if route has given name.
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasName($name);
|
||||
|
||||
/**
|
||||
* Sets the router name, which makes it easier to obtain the url or router at a later point.
|
||||
*
|
||||
* @param string $name
|
||||
* @return static $this
|
||||
*/
|
||||
public function setName($name);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
namespace Pecee\SimpleRouter\Route;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
|
||||
interface IRoute
|
||||
{
|
||||
/**
|
||||
* Method called to check if a domain matches
|
||||
*
|
||||
* @param Request $request
|
||||
* @return bool
|
||||
*/
|
||||
public function matchRoute(Request $request);
|
||||
|
||||
/**
|
||||
* Called when route is matched.
|
||||
* Returns class to be rendered.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return object
|
||||
*/
|
||||
public function renderRoute(Request $request);
|
||||
|
||||
/**
|
||||
* Returns callback name/identifier for the current route based on the callback.
|
||||
* Useful if you need to get a unique identifier for the loaded route, for instance
|
||||
* when using translations etc.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getIdentifier();
|
||||
|
||||
/**
|
||||
* Set allowed request methods
|
||||
*
|
||||
* @param array $methods
|
||||
* @return static $this
|
||||
*/
|
||||
public function setRequestMethods(array $methods);
|
||||
|
||||
/**
|
||||
* Get allowed request methods
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRequestMethods();
|
||||
|
||||
/**
|
||||
* @return IRoute|null
|
||||
*/
|
||||
public function getParent();
|
||||
|
||||
/**
|
||||
* Get the group for the route.
|
||||
*
|
||||
* @return IGroupRoute|null
|
||||
*/
|
||||
public function getGroup();
|
||||
|
||||
/**
|
||||
* Set group
|
||||
*
|
||||
* @param IGroupRoute $group
|
||||
* @return static $this
|
||||
*/
|
||||
public function setGroup(IGroupRoute $group);
|
||||
|
||||
/**
|
||||
* Set parent route
|
||||
*
|
||||
* @param IRoute $parent
|
||||
* @return static $this
|
||||
*/
|
||||
public function setParent(IRoute $parent);
|
||||
|
||||
/**
|
||||
* Set callback
|
||||
*
|
||||
* @param string $callback
|
||||
* @return static
|
||||
*/
|
||||
public function setCallback($callback);
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCallback();
|
||||
|
||||
public function getMethod();
|
||||
|
||||
public function getClass();
|
||||
|
||||
public function setMethod($method);
|
||||
|
||||
/**
|
||||
* @param string $namespace
|
||||
* @return static $this
|
||||
*/
|
||||
public function setNamespace($namespace);
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getNamespace();
|
||||
|
||||
/**
|
||||
* @param string $namespace
|
||||
* @return static $this
|
||||
*/
|
||||
public function setDefaultNamespace($namespace);
|
||||
|
||||
public function getDefaultNamespace();
|
||||
|
||||
/**
|
||||
* Get regular expression match used for matching route (if defined).
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMatch();
|
||||
|
||||
/**
|
||||
* Add regular expression match for the entire route.
|
||||
*
|
||||
* @param string $regex
|
||||
* @return static
|
||||
*/
|
||||
public function setMatch($regex);
|
||||
|
||||
/**
|
||||
* Get parameter names.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getWhere();
|
||||
|
||||
/**
|
||||
* Set parameter names.
|
||||
*
|
||||
* @param array $options
|
||||
* @return static
|
||||
*/
|
||||
public function setWhere(array $options);
|
||||
|
||||
/**
|
||||
* Get parameters
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getParameters();
|
||||
|
||||
/**
|
||||
* Get parameters
|
||||
*
|
||||
* @param array $parameters
|
||||
* @return static $this
|
||||
*/
|
||||
public function setParameters(array $parameters);
|
||||
|
||||
/**
|
||||
* Merge with information from another route.
|
||||
*
|
||||
* @param array $settings
|
||||
* @param bool $merge
|
||||
* @return static $this
|
||||
*/
|
||||
public function setSettings(array $settings, $merge = false);
|
||||
|
||||
/**
|
||||
* Export route settings to array so they can be merged with another route.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray();
|
||||
|
||||
/**
|
||||
* Get middlewares array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getMiddlewares();
|
||||
|
||||
/**
|
||||
* Set middleware class-name
|
||||
*
|
||||
* @param string $middleware
|
||||
* @return static
|
||||
*/
|
||||
public function setMiddleware($middleware);
|
||||
|
||||
/**
|
||||
* Set middlewares array
|
||||
*
|
||||
* @param array $middlewares
|
||||
* @return $this
|
||||
*/
|
||||
public function setMiddlewares(array $middlewares);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
namespace Pecee\SimpleRouter\Route;
|
||||
|
||||
use Pecee\Http\Middleware\IMiddleware;
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\Exceptions\HttpException;
|
||||
|
||||
abstract class LoadableRoute extends Route implements ILoadableRoute
|
||||
{
|
||||
const PARAMETERS_REGEX_MATCH = '%s([\w\-\_]*?)\%s{0,1}%s';
|
||||
|
||||
protected $url;
|
||||
protected $name;
|
||||
|
||||
/**
|
||||
* Loads and renders middlewares-classes
|
||||
*
|
||||
* @param Request $request
|
||||
* @param ILoadableRoute $route
|
||||
* @throws HttpException
|
||||
*/
|
||||
public function loadMiddleware(Request $request, ILoadableRoute &$route)
|
||||
{
|
||||
if (count($this->getMiddlewares()) > 0) {
|
||||
foreach ($this->getMiddlewares() as $middleware) {
|
||||
|
||||
$middleware = $this->loadClass($middleware);
|
||||
if (!($middleware instanceof IMiddleware)) {
|
||||
throw new HttpException($middleware . ' must be instance of Middleware');
|
||||
}
|
||||
|
||||
$middleware->handle($request, $route);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set url
|
||||
*
|
||||
* @param string $url
|
||||
* @return static
|
||||
*/
|
||||
public function setUrl($url)
|
||||
{
|
||||
$this->url = ($url === '/') ? '/' : '/' . trim($url, '/') . '/';
|
||||
$regex = sprintf(static::PARAMETERS_REGEX_MATCH, $this->paramModifiers[0], $this->paramOptionalSymbol, $this->paramModifiers[1]);
|
||||
|
||||
if (preg_match_all('/' . $regex . '/is', $this->url, $matches)) {
|
||||
foreach ($matches[1] as $key) {
|
||||
$this->parameters[$key] = null;
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getUrl()
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find url that matches method, parameters or name.
|
||||
* Used when calling the url() helper.
|
||||
*
|
||||
* @param string|null $method
|
||||
* @param array|null $parameters
|
||||
* @param string|null $name
|
||||
* @return string
|
||||
*/
|
||||
public function findUrl($method = null, $parameters = null, $name = null)
|
||||
{
|
||||
$url = '';
|
||||
|
||||
$parameters = (array)$parameters;
|
||||
|
||||
if ($this->getGroup() !== null && count($this->getGroup()->getDomains()) > 0) {
|
||||
$url .= '//' . $this->getGroup()->getDomains()[0];
|
||||
}
|
||||
|
||||
$url .= $this->getUrl();
|
||||
|
||||
$params = array_merge($this->getParameters(), $parameters);
|
||||
|
||||
/* Url that contains parameters that aren't recognized */
|
||||
$unknownParams = [];
|
||||
|
||||
/* Create the param string - {} */
|
||||
$param1 = $this->paramModifiers[0] . '%s' . $this->paramModifiers[1];
|
||||
|
||||
/* Create the param string with the optional symbol - {?} */
|
||||
$param2 = $this->paramModifiers[0] . '%s' . $this->paramOptionalSymbol . $this->paramModifiers[1];
|
||||
|
||||
/* Let's parse the values of any {} parameter in the url */
|
||||
foreach ($params as $param => $value) {
|
||||
$value = (isset($parameters[$param])) ? $parameters[$param] : $value;
|
||||
|
||||
if (stripos($url, $param1) !== false || stripos($url, $param) !== false) {
|
||||
$url = str_ireplace([sprintf($param1, $param), sprintf($param2, $param)], $value, $url);
|
||||
} else {
|
||||
$unknownParams[$param] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$url .= join('/', $unknownParams);
|
||||
|
||||
return rtrim($url, '/') . '/';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the provided name for the router.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if route has given name.
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasName($name)
|
||||
{
|
||||
return (strtolower($this->name) === strtolower($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the router name, which makes it easier to obtain the url or router at a later point.
|
||||
* Alias for LoadableRoute::setName().
|
||||
*
|
||||
* @see LoadableRoute::setName()
|
||||
* @param string|array $name
|
||||
* @return static
|
||||
*/
|
||||
public function name($name)
|
||||
{
|
||||
return $this->setName($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the router name, which makes it easier to obtain the url or router at a later point.
|
||||
*
|
||||
* @param string $name
|
||||
* @return static $this
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge with information from another route.
|
||||
*
|
||||
* @param array $values
|
||||
* @param bool $merge
|
||||
* @return static
|
||||
*/
|
||||
public function setSettings(array $values, $merge = false)
|
||||
{
|
||||
if (isset($values['as'])) {
|
||||
if ($this->name !== null && $merge !== false) {
|
||||
$this->setName($values['as'] . '.' . $this->name);
|
||||
} else {
|
||||
$this->setName($values['as']);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($values['prefix'])) {
|
||||
$this->setUrl($values['prefix'] . $this->getUrl());
|
||||
}
|
||||
|
||||
parent::setSettings($values, $merge);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
<?php
|
||||
namespace Pecee\SimpleRouter;
|
||||
namespace Pecee\SimpleRouter\Route;
|
||||
|
||||
use Pecee\Http\Middleware\IMiddleware;
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\Exceptions\HttpException;
|
||||
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||
|
||||
abstract class RouterEntry
|
||||
abstract class Route implements IRoute
|
||||
{
|
||||
const REQUEST_TYPE_GET = 'get';
|
||||
const REQUEST_TYPE_POST = 'post';
|
||||
@@ -24,6 +23,8 @@ abstract class RouterEntry
|
||||
self::REQUEST_TYPE_DELETE,
|
||||
];
|
||||
|
||||
protected $paramModifiers = '{}';
|
||||
protected $paramOptionalSymbol = '?';
|
||||
protected $group;
|
||||
protected $parent;
|
||||
protected $callback;
|
||||
@@ -37,13 +38,36 @@ abstract class RouterEntry
|
||||
protected $parameters = [];
|
||||
protected $middlewares = [];
|
||||
|
||||
protected function loadClass($name)
|
||||
public function renderRoute(Request $request)
|
||||
{
|
||||
if (!class_exists($name)) {
|
||||
throw new HttpException(sprintf('Class %s does not exist', $name), 500);
|
||||
if ($this->getCallback() !== null && 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 = $controller[1];
|
||||
|
||||
if (!method_exists($class, $method)) {
|
||||
throw new NotFoundHttpException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
|
||||
}
|
||||
|
||||
$parameters = array_filter($this->getParameters(), function ($var) {
|
||||
return ($var !== null);
|
||||
});
|
||||
|
||||
call_user_func_array([$class, $method], $parameters);
|
||||
|
||||
return $class;
|
||||
}
|
||||
|
||||
return new $name();
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function parseParameters($route, $url, $parameterRegex = '[\w]+')
|
||||
@@ -125,51 +149,13 @@ abstract class RouterEntry
|
||||
return null;
|
||||
}
|
||||
|
||||
public function loadMiddleware(Request $request, RouterEntry &$route)
|
||||
protected function loadClass($name)
|
||||
{
|
||||
if (count($this->getMiddlewares()) > 0) {
|
||||
foreach ($this->getMiddlewares() as $middleware) {
|
||||
|
||||
$middleware = $this->loadClass($middleware);
|
||||
if (!($middleware instanceof IMiddleware)) {
|
||||
throw new HttpException($middleware . ' must be instance of Middleware');
|
||||
}
|
||||
|
||||
$middleware->handle($request, $route);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function renderRoute(Request $request)
|
||||
{
|
||||
if ($this->getCallback() !== null && 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 = $controller[1];
|
||||
|
||||
if (!method_exists($class, $method)) {
|
||||
throw new NotFoundHttpException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
|
||||
}
|
||||
|
||||
$parameters = array_filter($this->getParameters(), function ($var) {
|
||||
return ($var !== null);
|
||||
});
|
||||
|
||||
call_user_func_array([$class, $method], $parameters);
|
||||
|
||||
return $class;
|
||||
if (!class_exists($name)) {
|
||||
throw new HttpException(sprintf('Class %s does not exist', $name), 500);
|
||||
}
|
||||
|
||||
return null;
|
||||
return new $name();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -203,6 +189,7 @@ abstract class RouterEntry
|
||||
|
||||
/**
|
||||
* Get allowed request methods
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRequestMethods()
|
||||
@@ -211,7 +198,7 @@ abstract class RouterEntry
|
||||
}
|
||||
|
||||
/**
|
||||
* @return LoadableRoute
|
||||
* @return IRoute|null
|
||||
*/
|
||||
public function getParent()
|
||||
{
|
||||
@@ -221,7 +208,7 @@ abstract class RouterEntry
|
||||
/**
|
||||
* Get the group for the route.
|
||||
*
|
||||
* @return RouterGroup|null
|
||||
* @return IGroupRoute|null
|
||||
*/
|
||||
public function getGroup()
|
||||
{
|
||||
@@ -231,20 +218,23 @@ abstract class RouterEntry
|
||||
/**
|
||||
* Set group
|
||||
*
|
||||
* @param RouterGroup $group
|
||||
* @param IGroupRoute $group
|
||||
* @return static $this
|
||||
*/
|
||||
public function setGroup(RouterGroup $group)
|
||||
public function setGroup(IGroupRoute $group)
|
||||
{
|
||||
$this->group = $group;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set parent route
|
||||
*
|
||||
* @param RouterEntry $parent
|
||||
* @param IRoute $parent
|
||||
* @return static $this
|
||||
*/
|
||||
public function setParent(RouterEntry $parent)
|
||||
public function setParent(IRoute $parent)
|
||||
{
|
||||
$this->parent = $parent;
|
||||
|
||||
@@ -252,6 +242,8 @@ abstract class RouterEntry
|
||||
}
|
||||
|
||||
/**
|
||||
* Set callback
|
||||
*
|
||||
* @param string $callback
|
||||
* @return static
|
||||
*/
|
||||
@@ -263,7 +255,7 @@ abstract class RouterEntry
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return string
|
||||
*/
|
||||
public function getCallback()
|
||||
{
|
||||
@@ -306,24 +298,6 @@ abstract class RouterEntry
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $middleware
|
||||
* @return static
|
||||
*/
|
||||
public function setMiddleware($middleware)
|
||||
{
|
||||
$this->middlewares[] = $middleware;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setMiddlewares(array $middlewares)
|
||||
{
|
||||
$this->middlewares = $middlewares;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $namespace
|
||||
* @return static $this
|
||||
@@ -351,14 +325,6 @@ abstract class RouterEntry
|
||||
return $this->defaultNamespace;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|array
|
||||
*/
|
||||
public function getMiddlewares()
|
||||
{
|
||||
return $this->middlewares;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
@@ -367,38 +333,6 @@ abstract class RouterEntry
|
||||
return ($this->namespace === null) ? $this->defaultNamespace : $this->namespace;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getParameters()
|
||||
{
|
||||
return $this->parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $parameters
|
||||
* @return static
|
||||
*/
|
||||
public function setParameters($parameters)
|
||||
{
|
||||
$this->parameters = $parameters;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add regular expression parameter match
|
||||
*
|
||||
* @param array $options
|
||||
* @return static
|
||||
*/
|
||||
public function setWhere(array $options)
|
||||
{
|
||||
$this->where = $options;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add regular expression match for the entire route.
|
||||
*
|
||||
@@ -412,6 +346,16 @@ abstract class RouterEntry
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get regular expression match used for matching route (if defined).
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMatch()
|
||||
{
|
||||
return $this->regex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export route settings to array so they can be merged with another route.
|
||||
*
|
||||
@@ -425,22 +369,22 @@ abstract class RouterEntry
|
||||
$values['namespace'] = $this->namespace;
|
||||
}
|
||||
|
||||
if (count($this->middlewares) > 0) {
|
||||
$values['middleware'] = $this->middlewares;
|
||||
if (count($this->requestMethods) > 0) {
|
||||
$values['method'] = $this->requestMethods;
|
||||
}
|
||||
|
||||
if (count($this->where) > 0) {
|
||||
$values['where'] = $this->where;
|
||||
}
|
||||
|
||||
if (count($this->requestMethods) > 0) {
|
||||
$values['method'] = $this->requestMethods;
|
||||
}
|
||||
|
||||
if (count($this->parameters) > 0) {
|
||||
$values['parameters'] = $this->parameters;
|
||||
}
|
||||
|
||||
if (count($this->middlewares) > 0) {
|
||||
$values['middleware'] = $this->middlewares;
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
@@ -448,19 +392,15 @@ abstract class RouterEntry
|
||||
* Merge with information from another route.
|
||||
*
|
||||
* @param array $values
|
||||
* @param bool $merge
|
||||
* @return static $this
|
||||
*/
|
||||
public function merge(array $values)
|
||||
public function setSettings(array $values, $merge = false)
|
||||
{
|
||||
if (isset($values['namespace']) && $this->namespace === null) {
|
||||
$this->setNamespace($values['namespace']);
|
||||
}
|
||||
|
||||
// Push middleware if multiple
|
||||
if (isset($values['middleware'])) {
|
||||
$this->setMiddlewares(array_merge((array)$values['middleware'], $this->middlewares));
|
||||
}
|
||||
|
||||
if (isset($values['method'])) {
|
||||
$this->setRequestMethods(array_merge($this->requestMethods, (array)$values['method']));
|
||||
}
|
||||
@@ -473,9 +413,105 @@ abstract class RouterEntry
|
||||
$this->setParameters(array_merge($this->parameters, (array)$values['parameters']));
|
||||
}
|
||||
|
||||
// Push middleware if multiple
|
||||
if (isset($values['middleware'])) {
|
||||
$this->setMiddlewares(array_merge((array)$values['middleware'], $this->middlewares));
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
abstract function matchRoute(Request $request);
|
||||
/**
|
||||
* Get parameter names.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getWhere()
|
||||
{
|
||||
return $this->where;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set parameter names.
|
||||
*
|
||||
* @param array $options
|
||||
* @return static
|
||||
*/
|
||||
public function setWhere(array $options)
|
||||
{
|
||||
$this->where = $options;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add regular expression parameter match.
|
||||
* Alias for LoadableRoute::where()
|
||||
*
|
||||
* @see LoadableRoute::where()
|
||||
* @param array $options
|
||||
* @return static
|
||||
*/
|
||||
public function where(array $options)
|
||||
{
|
||||
return $this->where($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get parameters
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getParameters()
|
||||
{
|
||||
return $this->parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get parameters
|
||||
*
|
||||
* @param array $parameters
|
||||
* @return static $this
|
||||
*/
|
||||
public function setParameters(array $parameters)
|
||||
{
|
||||
$this->parameters = $parameters;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set middleware class-name
|
||||
*
|
||||
* @param string $middleware
|
||||
* @return static
|
||||
*/
|
||||
public function setMiddleware($middleware)
|
||||
{
|
||||
$this->middlewares[] = $middleware;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set middlewares array
|
||||
*
|
||||
* @param array $middlewares
|
||||
* @return $this
|
||||
*/
|
||||
public function setMiddlewares(array $middlewares)
|
||||
{
|
||||
$this->middlewares = $middlewares;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|array
|
||||
*/
|
||||
public function getMiddlewares()
|
||||
{
|
||||
return $this->middlewares;
|
||||
}
|
||||
|
||||
}
|
||||
+89
-2
@@ -1,21 +1,82 @@
|
||||
<?php
|
||||
namespace Pecee\SimpleRouter;
|
||||
namespace Pecee\SimpleRouter\Route;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||
|
||||
class RouterController extends LoadableRoute implements IControllerRoute
|
||||
class RouteController extends LoadableRoute implements IControllerRoute
|
||||
{
|
||||
protected $defaultMethod = 'index';
|
||||
protected $controller;
|
||||
protected $method;
|
||||
protected $names = [];
|
||||
|
||||
public function __construct($url, $controller)
|
||||
{
|
||||
$this->setUrl($url);
|
||||
$this->setName(trim(str_replace('/', '.', $url), '/'));
|
||||
$this->controller = $controller;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if route has given name.
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasName($name)
|
||||
{
|
||||
if ($this->name === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Remove method/type */
|
||||
if (stripos($name, '.') !== false) {
|
||||
$method = substr($name, strrpos($name, '.') + 1);
|
||||
$newName = substr($name, 0, strrpos($name, '.'));
|
||||
|
||||
if (strtolower($this->name) === strtolower($newName) && in_array($method, $this->names)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return parent::hasName($name);
|
||||
}
|
||||
|
||||
public function findUrl($method = null, $parameters = null, $name = null)
|
||||
{
|
||||
|
||||
if (stripos($name, '.') !== false) {
|
||||
$found = array_search(substr($name, strrpos($name, '.') + 1), $this->names);
|
||||
if ($found !== false) {
|
||||
$method = $found;
|
||||
}
|
||||
}
|
||||
|
||||
$url = '';
|
||||
|
||||
$parameters = (array)$parameters;
|
||||
|
||||
/* Remove requestType from method-name, if it exists */
|
||||
if ($method !== null) {
|
||||
foreach (static::$requestTypes as $requestType) {
|
||||
if (stripos($method, $requestType) === 0) {
|
||||
$method = substr($method, strlen($requestType));
|
||||
break;
|
||||
}
|
||||
}
|
||||
$method .= '/';
|
||||
}
|
||||
|
||||
if ($this->getGroup() !== null && count($this->getGroup()->getDomains()) > 0) {
|
||||
$url .= '//' . $this->getGroup()->getDomains()[0];
|
||||
}
|
||||
|
||||
$url .= '/' . trim($this->getUrl(), '/') . '/' . strtolower($method) . join('/', $parameters);
|
||||
|
||||
return '/' . trim($url, '/') . '/';
|
||||
}
|
||||
|
||||
public function renderRoute(Request $request)
|
||||
{
|
||||
if ($this->getCallback() !== null && is_callable($this->getCallback())) {
|
||||
@@ -72,6 +133,8 @@ class RouterController extends LoadableRoute implements IControllerRoute
|
||||
}
|
||||
|
||||
/**
|
||||
* Get controller class-name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getController()
|
||||
@@ -80,6 +143,8 @@ class RouterController extends LoadableRoute implements IControllerRoute
|
||||
}
|
||||
|
||||
/**
|
||||
* Get controller class-name.
|
||||
*
|
||||
* @param string $controller
|
||||
* @return static
|
||||
*/
|
||||
@@ -91,6 +156,8 @@ class RouterController extends LoadableRoute implements IControllerRoute
|
||||
}
|
||||
|
||||
/**
|
||||
* Return active method
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMethod()
|
||||
@@ -99,6 +166,8 @@ class RouterController extends LoadableRoute implements IControllerRoute
|
||||
}
|
||||
|
||||
/**
|
||||
* Set active method
|
||||
*
|
||||
* @param string $method
|
||||
* @return static
|
||||
*/
|
||||
@@ -109,4 +178,22 @@ class RouterController extends LoadableRoute implements IControllerRoute
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge with information from another route.
|
||||
*
|
||||
* @param array $values
|
||||
* @param bool $merge
|
||||
* @return static
|
||||
*/
|
||||
public function setSettings(array $values, $merge = false)
|
||||
{
|
||||
if (isset($values['names'])) {
|
||||
$this->names = $values['names'];
|
||||
}
|
||||
|
||||
parent::setSettings($values, $merge);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
+55
-4
@@ -1,14 +1,21 @@
|
||||
<?php
|
||||
namespace Pecee\SimpleRouter;
|
||||
namespace Pecee\SimpleRouter\Route;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
|
||||
class RouterGroup extends RouterEntry
|
||||
class RouteGroup extends Route implements IGroupRoute
|
||||
{
|
||||
protected $prefix;
|
||||
protected $name;
|
||||
protected $domains = [];
|
||||
protected $exceptionHandlers = [];
|
||||
|
||||
/**
|
||||
* Method called to check if a domain matches
|
||||
*
|
||||
* @param Request $request
|
||||
* @return bool
|
||||
*/
|
||||
public function matchDomain(Request $request)
|
||||
{
|
||||
if (count($this->domains) > 0) {
|
||||
@@ -29,6 +36,12 @@ class RouterGroup extends RouterEntry
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method called to check if route matches
|
||||
*
|
||||
* @param Request $request
|
||||
* @return bool
|
||||
*/
|
||||
public function matchRoute(Request $request)
|
||||
{
|
||||
// Skip if prefix doesn't match
|
||||
@@ -39,6 +52,12 @@ class RouterGroup extends RouterEntry
|
||||
return $this->matchDomain($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set exception-handlers for group
|
||||
*
|
||||
* @param array $handlers
|
||||
* @return static $this
|
||||
*/
|
||||
public function setExceptionHandlers(array $handlers)
|
||||
{
|
||||
$this->exceptionHandlers = $handlers;
|
||||
@@ -46,16 +65,32 @@ class RouterGroup extends RouterEntry
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get exception-handlers for group
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getExceptionHandlers()
|
||||
{
|
||||
return $this->exceptionHandlers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get allowed domains for domain.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getDomains()
|
||||
{
|
||||
return $this->domains;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set allowed domains for group.
|
||||
*
|
||||
* @param array $domains
|
||||
* @return $this
|
||||
*/
|
||||
public function setDomains(array $domains)
|
||||
{
|
||||
$this->domains = $domains;
|
||||
@@ -75,6 +110,8 @@ class RouterGroup extends RouterEntry
|
||||
}
|
||||
|
||||
/**
|
||||
* Set prefix that child-routes will inherit.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPrefix()
|
||||
@@ -86,10 +123,12 @@ class RouterGroup extends RouterEntry
|
||||
* Merge with information from another route.
|
||||
*
|
||||
* @param array $values
|
||||
* @param bool $merge
|
||||
* @return static
|
||||
*/
|
||||
public function merge(array $values)
|
||||
public function setSettings(array $values, $merge = false)
|
||||
{
|
||||
|
||||
if (isset($values['prefix'])) {
|
||||
$this->setPrefix($values['prefix'] . $this->prefix);
|
||||
}
|
||||
@@ -102,7 +141,15 @@ class RouterGroup extends RouterEntry
|
||||
$this->setDomains((array)$values['domain']);
|
||||
}
|
||||
|
||||
parent::merge($values);
|
||||
if (isset($values['as'])) {
|
||||
if ($this->name !== null && $merge !== false) {
|
||||
$this->name = $values['as'] . '.' . $this->name;
|
||||
} else {
|
||||
$this->name = $values['as'];
|
||||
}
|
||||
}
|
||||
|
||||
parent::setSettings($values, $merge);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -120,6 +167,10 @@ class RouterGroup extends RouterEntry
|
||||
$values['prefix'] = $this->getPrefix();
|
||||
}
|
||||
|
||||
if ($this->name !== null) {
|
||||
$values['as'] = $this->name;
|
||||
}
|
||||
|
||||
return array_merge($values, parent::toArray());
|
||||
}
|
||||
|
||||
+82
-2
@@ -1,17 +1,62 @@
|
||||
<?php
|
||||
namespace Pecee\SimpleRouter;
|
||||
namespace Pecee\SimpleRouter\Route;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||
|
||||
class RouterResource extends LoadableRoute implements IControllerRoute
|
||||
class RouteResource extends LoadableRoute implements IControllerRoute
|
||||
{
|
||||
protected $urls = [
|
||||
'index' => '',
|
||||
'create' => 'create',
|
||||
'store' => '',
|
||||
'show' => '',
|
||||
'edit' => 'edit',
|
||||
'update' => '',
|
||||
'destroy' => '',
|
||||
];
|
||||
protected $names = [];
|
||||
protected $controller;
|
||||
|
||||
public function __construct($url, $controller)
|
||||
{
|
||||
$this->setUrl($url);
|
||||
$this->controller = $controller;
|
||||
$this->setName(trim(str_replace('/', '.', $url), '/'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if route has given name.
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasName($name)
|
||||
{
|
||||
if ($this->name === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (strtolower($this->name) === strtolower($name)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Remove method/type */
|
||||
if (stripos($name, '.') !== false) {
|
||||
$name = substr($name, 0, strrpos($name, '.'));
|
||||
}
|
||||
|
||||
return (strtolower($this->name) === strtolower($name));
|
||||
}
|
||||
|
||||
public function findUrl($method = null, $parameters = null, $name = null)
|
||||
{
|
||||
$method = array_search($name, $this->names);
|
||||
if ($method !== false) {
|
||||
return rtrim($this->url . $this->urls[$method], '/') . '/';
|
||||
}
|
||||
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
public function renderRoute(Request $request)
|
||||
@@ -120,4 +165,39 @@ class RouterResource extends LoadableRoute implements IControllerRoute
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
$this->names = [
|
||||
'index' => $this->name . '.index',
|
||||
'create' => $this->name . '.create',
|
||||
'store' => $this->name . '.store',
|
||||
'show' => $this->name . '.show',
|
||||
'edit' => $this->name . '.edit',
|
||||
'update' => $this->name . '.update',
|
||||
'destroy' => $this->name . '.destroy',
|
||||
];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge with information from another route.
|
||||
*
|
||||
* @param array $values
|
||||
* @param bool $merge
|
||||
* @return static
|
||||
*/
|
||||
public function setSettings(array $values, $merge = false)
|
||||
{
|
||||
if (isset($values['names'])) {
|
||||
$this->names = $values['names'];
|
||||
}
|
||||
|
||||
parent::setSettings($values, $merge);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
<?php
|
||||
namespace Pecee\SimpleRouter;
|
||||
namespace Pecee\SimpleRouter\Route;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
|
||||
class RouterRoute extends LoadableRoute
|
||||
class RouteUrl extends LoadableRoute
|
||||
{
|
||||
public function __construct($url, $callback)
|
||||
{
|
||||
@@ -4,14 +4,18 @@ namespace Pecee\SimpleRouter;
|
||||
use Pecee\Handlers\IExceptionHandler;
|
||||
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\Http\Response;
|
||||
use Pecee\SimpleRouter\Exceptions\HttpException;
|
||||
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||
use Pecee\SimpleRouter\Route\IControllerRoute;
|
||||
use Pecee\SimpleRouter\Route\IGroupRoute;
|
||||
use Pecee\SimpleRouter\Route\ILoadableRoute;
|
||||
use Pecee\SimpleRouter\Route\IRoute;
|
||||
|
||||
class RouterBase
|
||||
class Router
|
||||
{
|
||||
|
||||
/**
|
||||
* The instance of this class
|
||||
* @var static
|
||||
*/
|
||||
protected static $instance;
|
||||
@@ -23,14 +27,7 @@ class RouterBase
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Response
|
||||
* @var Response
|
||||
*/
|
||||
protected $response;
|
||||
|
||||
/**
|
||||
* Used to keep track of whether or not a should should be added to
|
||||
* the backstack-list for group-processing or not.
|
||||
* Defines if a route is currently being processed.
|
||||
* @var bool
|
||||
*/
|
||||
protected $processingRoute;
|
||||
@@ -42,16 +39,17 @@ class RouterBase
|
||||
protected $routes;
|
||||
|
||||
/**
|
||||
* List of
|
||||
* List of processed routes
|
||||
* @var array
|
||||
*/
|
||||
protected $controllerUrlMap;
|
||||
protected $processedRoutes;
|
||||
|
||||
/**
|
||||
* Backstack array used to keep track of sub-routes
|
||||
* Stack of routes used to keep track of sub-routes added
|
||||
* when a route is being processed.
|
||||
* @var array
|
||||
*/
|
||||
protected $backStack;
|
||||
protected $routeStack;
|
||||
|
||||
/**
|
||||
* List of added bootmanagers
|
||||
@@ -73,7 +71,7 @@ class RouterBase
|
||||
|
||||
/**
|
||||
* The current loaded route
|
||||
* @var RouterRoute|null
|
||||
* @var ILoadableRoute|null
|
||||
*/
|
||||
protected $loadedRoute;
|
||||
|
||||
@@ -111,23 +109,27 @@ class RouterBase
|
||||
{
|
||||
$this->processingRoute = false;
|
||||
$this->request = new Request();
|
||||
$this->response = new Response($this->request);
|
||||
$this->routes = [];
|
||||
$this->bootManagers = [];
|
||||
$this->backStack = [];
|
||||
$this->controllerUrlMap = [];
|
||||
$this->routeStack = [];
|
||||
$this->processedRoutes = [];
|
||||
$this->exceptionHandlers = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add route
|
||||
* @param RouterEntry $route
|
||||
* @return RouterEntry
|
||||
* @param IRoute $route
|
||||
* @return IRoute
|
||||
*/
|
||||
public function addRoute(RouterEntry $route)
|
||||
public function addRoute(IRoute $route)
|
||||
{
|
||||
if ($this->processingRoute) {
|
||||
$this->backStack[] = $route;
|
||||
/*
|
||||
* If a route is currently being processed, that means that the
|
||||
* route being added are rendered from the parent routes callback,
|
||||
* so we add them to the stack instead.
|
||||
*/
|
||||
if ($this->processingRoute === true) {
|
||||
$this->routeStack[] = $route;
|
||||
} else {
|
||||
$this->routes[] = $route;
|
||||
}
|
||||
@@ -135,13 +137,13 @@ class RouterBase
|
||||
return $route;
|
||||
}
|
||||
|
||||
protected function processRoutes(array $routes, RouterGroup $group = null, RouterEntry $parent = null)
|
||||
protected function processRoutes(array $routes, IGroupRoute $group = null, IRoute $parent = null)
|
||||
{
|
||||
// Loop through each route-request
|
||||
/* @var $route RouterEntry */
|
||||
/* @var $route IRoute */
|
||||
foreach ($routes as $route) {
|
||||
|
||||
if ($route instanceof RouterGroup) {
|
||||
if ($route instanceof IGroupRoute) {
|
||||
|
||||
$group = $route;
|
||||
|
||||
@@ -175,24 +177,24 @@ class RouterBase
|
||||
$route->setParent($parent);
|
||||
|
||||
/* Add/merge parent settings with child */
|
||||
$route->merge($parent->toArray());
|
||||
$route->setSettings($parent->toArray(), true);
|
||||
|
||||
}
|
||||
|
||||
if ($route instanceof ILoadableRoute) {
|
||||
|
||||
/* Add the route to the map, so we can find the active one when all routes has been loaded */
|
||||
$this->controllerUrlMap[] = $route;
|
||||
$this->processedRoutes[] = $route;
|
||||
}
|
||||
|
||||
if (count($this->backStack) > 0) {
|
||||
if (count($this->routeStack) > 0) {
|
||||
|
||||
/* Pop and grap the routes added when executing group callback earlier */
|
||||
$backStack = $this->backStack;
|
||||
$this->backStack = [];
|
||||
$stack = $this->routeStack;
|
||||
$this->routeStack = [];
|
||||
|
||||
/* Route any routes added to the backstack */
|
||||
$this->processRoutes($backStack, $route, $group);
|
||||
/* Route any routes added to the stack */
|
||||
$this->processRoutes($stack, $route, $group);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -204,9 +206,9 @@ class RouterBase
|
||||
|
||||
try {
|
||||
|
||||
// Initialize boot-managers
|
||||
/* Initialize boot-managers */
|
||||
if (count($this->bootManagers) > 0) {
|
||||
/* @var $manager RouterBootManager */
|
||||
/* @var $manager IRouterBootManager */
|
||||
foreach ($this->bootManagers as $manager) {
|
||||
$this->request = $manager->boot($this->request);
|
||||
|
||||
@@ -218,7 +220,7 @@ class RouterBase
|
||||
|
||||
if ($rewrite === false) {
|
||||
|
||||
// Loop through each route-request
|
||||
/* Loop through each route-request */
|
||||
$this->processRoutes($this->routes);
|
||||
|
||||
if ($this->csrfVerifier !== null) {
|
||||
@@ -230,11 +232,13 @@ class RouterBase
|
||||
$this->originalUrl = $this->request->getUri();
|
||||
}
|
||||
|
||||
/* @var $route RouterEntry */
|
||||
foreach ($this->controllerUrlMap as $route) {
|
||||
/* @var $route IRoute */
|
||||
foreach ($this->processedRoutes as $route) {
|
||||
|
||||
/* If the route matches */
|
||||
if ($route->matchRoute($this->request)) {
|
||||
|
||||
/* Check if request method matches */
|
||||
if (count($route->getRequestMethods()) > 0 && !in_array($this->request->getMethod(), $route->getRequestMethods())) {
|
||||
$routeNotAllowed = true;
|
||||
continue;
|
||||
@@ -243,6 +247,7 @@ class RouterBase
|
||||
$this->loadedRoute = $route;
|
||||
$this->loadedRoute->loadMiddleware($this->request, $this->loadedRoute);
|
||||
|
||||
/* If the request has changed, we reinitialize the router */
|
||||
if ($this->request->getUri() !== $this->originalUrl && !in_array($this->request->getUri(), $this->routeRewrites)) {
|
||||
$this->routeRewrites[] = $this->request->getUri();
|
||||
$this->routeRequest(true);
|
||||
@@ -250,6 +255,7 @@ class RouterBase
|
||||
return;
|
||||
}
|
||||
|
||||
/* Render route */
|
||||
$routeNotAllowed = false;
|
||||
$this->request->setUri($this->originalUrl);
|
||||
$this->loadedRoute->renderRoute($this->request);
|
||||
@@ -284,7 +290,9 @@ class RouterBase
|
||||
|
||||
$request = $handler->handleError($this->request, $this->loadedRoute, $e);
|
||||
|
||||
if ($request !== null && $request->getUri() !== $this->originalUrl && !in_array($request->getUri(), $this->routeRewrites)) {
|
||||
/* If the request has changed */
|
||||
if ($request !== null && $this->request->getUri() !== $this->originalUrl && !in_array($request->getUri(), $this->routeRewrites)) {
|
||||
$this->request = $request;
|
||||
$this->routeRewrites[] = $request->getUri();
|
||||
$this->routeRequest(true);
|
||||
|
||||
@@ -311,66 +319,16 @@ class RouterBase
|
||||
return '';
|
||||
}
|
||||
|
||||
protected function processUrl(LoadableRoute $route, $method = null, $parameters = null, array $getParams = [])
|
||||
{
|
||||
$url = '';
|
||||
|
||||
$parameters = (array)$parameters;
|
||||
|
||||
if ($route->getGroup() !== null && count($route->getGroup()->getDomains()) > 0) {
|
||||
$url .= '//' . $route->getGroup()->getDomains()[0];
|
||||
}
|
||||
|
||||
$url .= '/' . trim($route->getUrl(), '/');
|
||||
|
||||
if ($route instanceof IControllerRoute && $method !== null) {
|
||||
|
||||
$url .= '/' . $method . '/';
|
||||
|
||||
if (count($parameters) > 0) {
|
||||
$url .= join('/', $parameters);
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
$params = array_merge($route->getParameters(), $parameters);
|
||||
|
||||
/* Url that contains parameters that aren't recognized */
|
||||
$unknownParams = [];
|
||||
|
||||
/* Let's parse the values of any {} parameter in the url */
|
||||
foreach ($params as $param => $value) {
|
||||
$value = (isset($parameters[$param])) ? $parameters[$param] : $value;
|
||||
|
||||
/* Create the param string - {} */
|
||||
$param1 = LoadableRoute::PARAMETER_MODIFIERS[0] . $param . LoadableRoute::PARAMETER_MODIFIERS[1];
|
||||
|
||||
/* Create the param string with the optional symbol - {?} */
|
||||
$param2 = LoadableRoute::PARAMETER_MODIFIERS[0] . $param . LoadableRoute::PARAMETER_OPTIONAL_SYMBOL . LoadableRoute::PARAMETER_MODIFIERS[1];
|
||||
|
||||
if (stripos($url, $param1) !== false || stripos($url, $param) !== false) {
|
||||
$url = str_ireplace([$param1, $param2], $value, $url);
|
||||
} else {
|
||||
$unknownParams[$param] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$url = rtrim($url, '/') . '/' . join('/', $unknownParams);
|
||||
}
|
||||
|
||||
return rtrim($url, '/') . '/' . $this->arrayToParams($getParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find route by alias, class, callback or method.
|
||||
*
|
||||
* @param string $name
|
||||
* @return LoadableRoute|null
|
||||
* @return ILoadableRoute|null
|
||||
*/
|
||||
public function findRoute($name)
|
||||
{
|
||||
/* @var $route LoadableRoute */
|
||||
foreach ($this->controllerUrlMap as $route) {
|
||||
/* @var $route ILoadableRoute */
|
||||
foreach ($this->processedRoutes as $route) {
|
||||
|
||||
/* Check if the name matches with a name on the route. Should match either router alias or controller alias. */
|
||||
if ($route->hasName($name)) {
|
||||
@@ -409,11 +367,6 @@ class RouterBase
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getRoute($name = null, $parameters = null, array $getParams = null)
|
||||
{
|
||||
return $this->getUrl($name, $parameters, $getParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get url for a route by using either name/alias, class or method name.
|
||||
*
|
||||
@@ -431,7 +384,7 @@ class RouterBase
|
||||
* @param array|null $getParams
|
||||
* @return string
|
||||
*/
|
||||
public function getUrl($name = null, $parameters = null, array $getParams = null)
|
||||
public function getUrl($name = null, $parameters = null, $getParams = [])
|
||||
{
|
||||
if ($getParams !== null && is_array($getParams) === false) {
|
||||
throw new \InvalidArgumentException('Invalid type for getParams. Must be array or null');
|
||||
@@ -443,19 +396,20 @@ class RouterBase
|
||||
|
||||
/* Return current route if no options has been specified */
|
||||
if ($name === null && $parameters === null) {
|
||||
return '/' . trim(parse_url($this->request->getUri(), PHP_URL_PATH), '/') . '/' . $this->arrayToParams($getParams);
|
||||
$url = rtrim(parse_url($this->request->getUri(), PHP_URL_PATH), '/');
|
||||
return (($url === '') ? '/' : $url . '/') . $this->arrayToParams($getParams);
|
||||
}
|
||||
|
||||
/* If nothing is defined and a route is loaded we use that */
|
||||
if ($name === null && $this->loadedRoute !== null) {
|
||||
return $this->processUrl($this->loadedRoute, $this->loadedRoute->getMethod(), $parameters, $getParams);
|
||||
return $this->loadedRoute->findUrl($this->loadedRoute->getMethod(), $parameters, $name) . $this->arrayToParams($getParams);
|
||||
}
|
||||
|
||||
/* We try to find a match on the given name */
|
||||
$route = $this->findRoute($name);
|
||||
|
||||
if ($route !== null) {
|
||||
return $this->processUrl($route, $route->getMethod(), $parameters, $getParams);
|
||||
return $route->findUrl($route->getMethod(), $parameters, $name) . $this->arrayToParams($getParams);
|
||||
}
|
||||
|
||||
/* Using @ is most definitely a controller@method or alias@method */
|
||||
@@ -464,25 +418,24 @@ class RouterBase
|
||||
|
||||
/* Loop through all the routes to see if we can find a match */
|
||||
|
||||
/* @var $route LoadableRoute */
|
||||
foreach ($this->controllerUrlMap as $route) {
|
||||
/* @var $route ILoadableRoute */
|
||||
foreach ($this->processedRoutes as $route) {
|
||||
|
||||
/* Check if the route contains the name/alias */
|
||||
if ($route->hasName($controller)) {
|
||||
return $this->processUrl($route, $method, $parameters, $getParams);
|
||||
return $route->findUrl($method, $parameters, $name) . $this->arrayToParams($getParams);
|
||||
}
|
||||
|
||||
/* Check if the route controller is equal to the name */
|
||||
if ($route instanceof IControllerRoute && strtolower($route->getController()) === strtolower($controller)) {
|
||||
return $this->processUrl($route, $method, $parameters, $getParams);
|
||||
return $route->findUrl($method, $parameters, $name) . $this->arrayToParams($getParams);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/* No result so we assume that someone is using a hardcoded url and join everything together. */
|
||||
|
||||
return '/' . trim(join('/', array_merge((array)$name, (array)$parameters)), '/') . '/' . $this->arrayToParams($getParams);
|
||||
return '/' . trim(join('/', array_merge((array)$name, (array)$parameters)), '/') . $this->arrayToParams($getParams);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -505,9 +458,9 @@ class RouterBase
|
||||
|
||||
/**
|
||||
* Add bootmanager
|
||||
* @param RouterBootManager $bootManager
|
||||
* @param IRouterBootManager $bootManager
|
||||
*/
|
||||
public function addBootManager(RouterBootManager $bootManager)
|
||||
public function addBootManager(IRouterBootManager $bootManager)
|
||||
{
|
||||
$this->bootManagers[] = $bootManager;
|
||||
}
|
||||
@@ -530,15 +483,6 @@ class RouterBase
|
||||
return $this->request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get response
|
||||
* @return Response
|
||||
*/
|
||||
public function getResponse()
|
||||
{
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get csrf verifier class
|
||||
* @return BaseCsrfVerifier
|
||||
@@ -563,7 +507,7 @@ class RouterBase
|
||||
|
||||
/**
|
||||
* Get loaded route
|
||||
* @return RouterRoute|null
|
||||
* @return ILoadableRoute|null
|
||||
*/
|
||||
public function getLoadedRoute()
|
||||
{
|
||||
@@ -1,9 +0,0 @@
|
||||
<?php
|
||||
namespace Pecee\SimpleRouter;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
|
||||
abstract class RouterBootManager
|
||||
{
|
||||
abstract public function boot(Request $request);
|
||||
}
|
||||
@@ -3,18 +3,36 @@
|
||||
* ---------------------------
|
||||
* Router helper class
|
||||
* ---------------------------
|
||||
* This class is added so calls can be made statically like Router::get() making the code look more pretty.
|
||||
*
|
||||
* This class is added so calls can be made statically like Router::get() making the code look pretty.
|
||||
* It also adds some extra functionality like default-namespace.
|
||||
*/
|
||||
namespace Pecee\SimpleRouter;
|
||||
|
||||
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
||||
use Pecee\Http\Response;
|
||||
use Pecee\SimpleRouter\Exceptions\HttpException;
|
||||
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||
use Pecee\SimpleRouter\Route\IRoute;
|
||||
use Pecee\SimpleRouter\Route\RouteController;
|
||||
use Pecee\SimpleRouter\Route\RouteGroup;
|
||||
use Pecee\SimpleRouter\Route\RouteResource;
|
||||
use Pecee\SimpleRouter\Route\RouteUrl;
|
||||
|
||||
class SimpleRouter
|
||||
{
|
||||
/**
|
||||
* Default namespace added to all routes
|
||||
* @var string
|
||||
*/
|
||||
protected static $defaultNamespace;
|
||||
|
||||
/**
|
||||
* The response object
|
||||
* @var Response
|
||||
*/
|
||||
protected static $response;
|
||||
|
||||
/**
|
||||
* Start/route request
|
||||
*
|
||||
@@ -50,9 +68,9 @@ class SimpleRouter
|
||||
* Boot managers allows you to alter the routes before the routing occurs.
|
||||
* Perfect if you want to load pretty-urls from a file or database.
|
||||
*
|
||||
* @param RouterBootManager $bootManager
|
||||
* @param IRouterBootManager $bootManager
|
||||
*/
|
||||
public static function addBootManager(RouterBootManager $bootManager)
|
||||
public static function addBootManager(IRouterBootManager $bootManager)
|
||||
{
|
||||
static::router()->addBootManager($bootManager);
|
||||
}
|
||||
@@ -63,7 +81,7 @@ class SimpleRouter
|
||||
* @param string $url
|
||||
* @param string|\Closure $callback
|
||||
* @param array|null $settings
|
||||
* @return RouterRoute
|
||||
* @return RouteUrl
|
||||
*/
|
||||
public static function get($url, $callback, array $settings = null)
|
||||
{
|
||||
@@ -76,7 +94,7 @@ class SimpleRouter
|
||||
* @param string $url
|
||||
* @param string|\Closure $callback
|
||||
* @param array|null $settings
|
||||
* @return RouterRoute
|
||||
* @return RouteUrl
|
||||
*/
|
||||
public static function post($url, $callback, array $settings = null)
|
||||
{
|
||||
@@ -89,7 +107,7 @@ class SimpleRouter
|
||||
* @param string $url
|
||||
* @param string|\Closure $callback
|
||||
* @param array|null $settings
|
||||
* @return RouterRoute
|
||||
* @return RouteUrl
|
||||
*/
|
||||
public static function put($url, $callback, array $settings = null)
|
||||
{
|
||||
@@ -102,7 +120,7 @@ class SimpleRouter
|
||||
* @param string $url
|
||||
* @param string|\Closure $callback
|
||||
* @param array|null $settings
|
||||
* @return RouterRoute
|
||||
* @return RouteUrl
|
||||
*/
|
||||
public static function patch($url, $callback, array $settings = null)
|
||||
{
|
||||
@@ -115,7 +133,7 @@ class SimpleRouter
|
||||
* @param string $url
|
||||
* @param string|\Closure $callback
|
||||
* @param array|null $settings
|
||||
* @return RouterRoute
|
||||
* @return RouteUrl
|
||||
*/
|
||||
public static function options($url, $callback, array $settings = null)
|
||||
{
|
||||
@@ -128,7 +146,7 @@ class SimpleRouter
|
||||
* @param string $url
|
||||
* @param string|\Closure $callback
|
||||
* @param array|null $settings
|
||||
* @return RouterRoute
|
||||
* @return RouteUrl
|
||||
*/
|
||||
public static function delete($url, $callback, array $settings = null)
|
||||
{
|
||||
@@ -141,13 +159,13 @@ class SimpleRouter
|
||||
* @param array $settings
|
||||
* @param \Closure $callback
|
||||
* @throws \InvalidArgumentException
|
||||
* @return RouterGroup
|
||||
* @return RouteGroup
|
||||
*/
|
||||
public static function group(array $settings = [], \Closure $callback)
|
||||
{
|
||||
$group = new RouterGroup();
|
||||
$group = new RouteGroup();
|
||||
$group->setCallback($callback);
|
||||
$group->merge($settings);
|
||||
$group->setSettings($settings);
|
||||
|
||||
if (is_callable($callback) === false) {
|
||||
throw new \InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
|
||||
@@ -165,7 +183,7 @@ class SimpleRouter
|
||||
* @param callable $callback
|
||||
* @param array|null $settings
|
||||
* @see SimpleRouter::form
|
||||
* @return RouterRoute
|
||||
* @return RouteUrl
|
||||
*/
|
||||
public static function basic($url, $callback, array $settings = null)
|
||||
{
|
||||
@@ -180,7 +198,7 @@ class SimpleRouter
|
||||
* @param string|\Closure $callback
|
||||
* @param array|null $settings
|
||||
* @see SimpleRouter::form
|
||||
* @return RouterRoute
|
||||
* @return RouteUrl
|
||||
*/
|
||||
public static function form($url, $callback, array $settings = null)
|
||||
{
|
||||
@@ -194,16 +212,16 @@ class SimpleRouter
|
||||
* @param string $url
|
||||
* @param string|\Closure $callback
|
||||
* @param array|null $settings
|
||||
* @return RouterEntry|RouterRoute
|
||||
* @return RouteUrl
|
||||
*/
|
||||
public static function match(array $requestMethods, $url, $callback, array $settings = null)
|
||||
{
|
||||
$route = new RouterRoute($url, $callback);
|
||||
$route = new RouteUrl($url, $callback);
|
||||
$route->setRequestMethods($requestMethods);
|
||||
$route = static::addDefaultNamespace($route);
|
||||
|
||||
if ($settings !== null) {
|
||||
$route->merge($settings);
|
||||
$route->setSettings($settings);
|
||||
}
|
||||
|
||||
static::router()->addRoute($route);
|
||||
@@ -217,16 +235,15 @@ class SimpleRouter
|
||||
* @param string $url
|
||||
* @param string|\Closure $callback
|
||||
* @param array|null $settings
|
||||
* @return RouterRoute
|
||||
* @return RouteUrl
|
||||
*/
|
||||
public static function all($url, $callback, array $settings = null)
|
||||
{
|
||||
$route = new RouterRoute($url, $callback);
|
||||
|
||||
$route = new RouteUrl($url, $callback);
|
||||
$route = static::addDefaultNamespace($route);
|
||||
|
||||
if ($settings !== null) {
|
||||
$route->merge($settings);
|
||||
$route->setSettings($settings);
|
||||
}
|
||||
|
||||
static::router()->addRoute($route);
|
||||
@@ -240,16 +257,15 @@ class SimpleRouter
|
||||
* @param string $url
|
||||
* @param string $controller
|
||||
* @param array|null $settings
|
||||
* @return RouterController
|
||||
* @return RouteController
|
||||
*/
|
||||
public static function controller($url, $controller, array $settings = null)
|
||||
{
|
||||
$route = new RouterController($url, $controller);
|
||||
|
||||
$route = new RouteController($url, $controller);
|
||||
$route = static::addDefaultNamespace($route);
|
||||
|
||||
if ($settings !== null) {
|
||||
$route->merge($settings);
|
||||
$route->setSettings($settings);
|
||||
}
|
||||
|
||||
static::router()->addRoute($route);
|
||||
@@ -263,14 +279,14 @@ class SimpleRouter
|
||||
* @param string $url
|
||||
* @param string $controller
|
||||
* @param array|null $settings
|
||||
* @return RouterResource
|
||||
* @return RouteResource
|
||||
*/
|
||||
public static function resource($url, $controller, array $settings = null)
|
||||
{
|
||||
$route = new RouterResource($url, $controller);
|
||||
$route = new RouteResource($url, $controller);
|
||||
|
||||
if ($settings !== null) {
|
||||
$route->merge($settings);
|
||||
$route->setSettings($settings);
|
||||
}
|
||||
|
||||
static::router()->addRoute($route);
|
||||
@@ -290,37 +306,12 @@ class SimpleRouter
|
||||
* You can also use the same syntax when searching for a specific controller-class "MyController@home".
|
||||
* If no arguments is specified, it will return the url for the current loaded route.
|
||||
*
|
||||
* This method is an alias for SimpleRouter::getUrl().
|
||||
*
|
||||
* @see SimpleRouter::getUrl()
|
||||
* @param string|null $name
|
||||
* @param string|array|null $parameters
|
||||
* @param array|null $getParams
|
||||
* @return string
|
||||
*/
|
||||
public static function getRoute($name = null, $parameters = null, array $getParams = null)
|
||||
{
|
||||
return static::getUrl($name, $parameters, $getParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get url for a route by using either name/alias, class or method name.
|
||||
*
|
||||
* The name parameter supports the following values:
|
||||
* - Route name
|
||||
* - Controller/resource name (with or without method)
|
||||
* - Controller class name
|
||||
*
|
||||
* When searching for controller/resource by name, you can use this syntax "route.name@method".
|
||||
* You can also use the same syntax when searching for a specific controller-class "MyController@home".
|
||||
* If no arguments is specified, it will return the url for the current loaded route.
|
||||
*
|
||||
* @param string|null $name
|
||||
* @param string|array|null $parameters
|
||||
* @param array|null $getParams
|
||||
* @return string
|
||||
*/
|
||||
public static function getUrl($name = null, $parameters = null, array $getParams = null)
|
||||
public static function getUrl($name = null, $parameters = null, $getParams = [])
|
||||
{
|
||||
return static::router()->getUrl($name, $parameters, $getParams);
|
||||
}
|
||||
@@ -338,30 +329,34 @@ class SimpleRouter
|
||||
/**
|
||||
* Get the response object
|
||||
*
|
||||
* @return \Pecee\Http\Response
|
||||
* @return Response
|
||||
*/
|
||||
public static function response()
|
||||
{
|
||||
return static::router()->getResponse();
|
||||
if (static::$response === null) {
|
||||
static::$response = new Response(static::request());
|
||||
}
|
||||
|
||||
return static::$response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the router instance
|
||||
*
|
||||
* @return RouterBase
|
||||
* @return Router
|
||||
*/
|
||||
public static function router()
|
||||
{
|
||||
return RouterBase::getInstance();
|
||||
return Router::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepends the default namespace to all new routes added.
|
||||
*
|
||||
* @param RouterEntry $route
|
||||
* @return RouterEntry
|
||||
* @param IRoute $route
|
||||
* @return IRoute
|
||||
*/
|
||||
protected static function addDefaultNamespace(RouterEntry $route)
|
||||
protected static function addDefaultNamespace(IRoute $route)
|
||||
{
|
||||
if (static::$defaultNamespace !== null) {
|
||||
$namespace = static::$defaultNamespace;
|
||||
|
||||
@@ -6,7 +6,7 @@ use Pecee\Http\Request;
|
||||
|
||||
class DummyMiddleware implements IMiddleware
|
||||
{
|
||||
public function handle(Request $request, \Pecee\SimpleRouter\RouterEntry &$route = null)
|
||||
public function handle(Request $request, \Pecee\SimpleRouter\Route\ILoadableRoute &$route)
|
||||
{
|
||||
throw new MiddlewareLoadedException('Middleware loaded!');
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
class ExceptionHandler implements \Pecee\Handlers\IExceptionHandler
|
||||
{
|
||||
public function handleError(\Pecee\Http\Request $request, \Pecee\SimpleRouter\RouterEntry &$route = null, \Exception $error)
|
||||
public function handleError(\Pecee\Http\Request $request, \Pecee\SimpleRouter\Route\ILoadableRoute &$route = null, \Exception $error)
|
||||
{
|
||||
throw $error;
|
||||
}
|
||||
|
||||
+2
-8
@@ -85,16 +85,10 @@ class GroupTest extends PHPUnit_Framework_TestCase
|
||||
// Test method name
|
||||
SimpleRouter::get('/my/fancy/url/2', 'DummyController@start')->setName('fancy2');
|
||||
|
||||
// Test multiple names
|
||||
SimpleRouter::get('/my/fancy/url/3', 'DummyController@start', ['as' => ['fancy3', 'fancy4']]);
|
||||
|
||||
SimpleRouter::start();
|
||||
|
||||
$this->assertEquals('/my/fancy/url/1/', SimpleRouter::getRoute('fancy1'));
|
||||
$this->assertEquals('/my/fancy/url/2/', SimpleRouter::getRoute('fancy2'));
|
||||
|
||||
$this->assertEquals('/my/fancy/url/3/', SimpleRouter::getRoute('fancy3'));
|
||||
$this->assertEquals('/my/fancy/url/3/', SimpleRouter::getRoute('fancy4'));
|
||||
$this->assertEquals('/my/fancy/url/1/', SimpleRouter::getUrl('fancy1'));
|
||||
$this->assertEquals('/my/fancy/url/2/', SimpleRouter::getUrl('fancy2'));
|
||||
|
||||
}
|
||||
|
||||
|
||||
+34
-16
@@ -11,7 +11,7 @@ class RouterUrlTest extends PHPUnit_Framework_TestCase
|
||||
protected $result = false;
|
||||
|
||||
protected function getUrl($name = null, $parameters = null, array $getParams = []) {
|
||||
return SimpleRouter::getRoute($name, $parameters, $getParams);
|
||||
return SimpleRouter::getUrl($name, $parameters, $getParams);
|
||||
}
|
||||
|
||||
public function testUrls()
|
||||
@@ -23,60 +23,78 @@ class RouterUrlTest extends PHPUnit_Framework_TestCase
|
||||
// Match normal route on alias
|
||||
SimpleRouter::get('/', 'DummyController@silent', ['as' => 'home']);
|
||||
|
||||
SimpleRouter::group(['prefix' => '/admin'], function() {
|
||||
SimpleRouter::get('/about', 'DummyController@about');
|
||||
|
||||
SimpleRouter::group(['prefix' => '/admin', 'as' => 'admin'], function() {
|
||||
|
||||
// Match route with prefix on alias
|
||||
SimpleRouter::get('/{id?}', 'DummyController@start', ['as' => 'admin.home']);
|
||||
SimpleRouter::get('/{id?}', 'DummyController@start', ['as' => 'home']);
|
||||
|
||||
// Match controller with prefix and alias
|
||||
SimpleRouter::controller('/users', 'DummyController', ['as' => 'admin.users']);
|
||||
SimpleRouter::controller('/users', 'DummyController', ['as' => 'users']);
|
||||
|
||||
// Match controller with prefix and NO alias
|
||||
SimpleRouter::controller('/pages', 'DummyController');
|
||||
|
||||
});
|
||||
|
||||
SimpleRouter::group(['prefix' => 'api', 'as' => 'api'], function() {
|
||||
|
||||
// Match resource controller
|
||||
SimpleRouter::resource('phones', 'DummyController');
|
||||
|
||||
});
|
||||
|
||||
SimpleRouter::controller('gadgets', 'DummyController', ['names' => ['getIphoneInfo' => 'iphone']]);
|
||||
|
||||
// Match controller with no prefix and no alias
|
||||
SimpleRouter::controller('/cats', 'CatsController');
|
||||
|
||||
// Pretend to load page
|
||||
SimpleRouter::start();
|
||||
|
||||
$this->assertEquals('/gadgets/iphoneinfo/', $this->getUrl('gadgets.iphone'));
|
||||
|
||||
$this->assertEquals('/api/phones/create/', $this->getUrl('api.phones.create'));
|
||||
|
||||
// Should match /
|
||||
$this->assertEquals($this->getUrl('home'), '/');
|
||||
$this->assertEquals('/', $this->getUrl('home'));
|
||||
|
||||
// Should match /about/
|
||||
$this->assertEquals('/about/', $this->getUrl('DummyController@about'));
|
||||
|
||||
// Should match /admin/
|
||||
$this->assertEquals($this->getUrl('DummyController@start'), '/admin/');
|
||||
$this->assertEquals('/admin/', $this->getUrl('DummyController@start'));
|
||||
|
||||
// Should match /admin/
|
||||
$this->assertEquals($this->getUrl('admin.home'), '/admin/');
|
||||
$this->assertEquals('/admin/', $this->getUrl('admin.home'));
|
||||
|
||||
// Should match /admin/2/
|
||||
$this->assertEquals($this->getUrl('admin.home', ['id' => 2]), '/admin/2/');
|
||||
$this->assertEquals('/admin/2/', $this->getUrl('admin.home', ['id' => 2]));
|
||||
|
||||
// Should match /admin/users/
|
||||
$this->assertEquals($this->getUrl('admin.users'), '/admin/users/');
|
||||
$this->assertEquals('/admin/users/', $this->getUrl('admin.users'));
|
||||
|
||||
// Should match /admin/users/home/
|
||||
$this->assertEquals($this->getUrl('admin.users@home'), '/admin/users/home/');
|
||||
$this->assertEquals('/admin/users/home/', $this->getUrl('admin.users@home'));
|
||||
|
||||
// Should match /cats/
|
||||
$this->assertEquals($this->getUrl('CatsController'), '/cats/');
|
||||
$this->assertEquals('/cats/', $this->getUrl('CatsController'));
|
||||
|
||||
// Should match /cats/view/
|
||||
$this->assertEquals($this->getUrl('CatsController', 'view'), '/cats/view/');
|
||||
$this->assertEquals('/cats/view/', $this->getUrl('CatsController', 'view'));
|
||||
|
||||
// Should match /cats/view/
|
||||
$this->assertEquals($this->getUrl('CatsController', ['view']), '/cats/view/');
|
||||
//$this->assertEquals('/cats/view/', $this->getUrl('CatsController', ['view']));
|
||||
|
||||
// Should match /cats/view/666
|
||||
$this->assertEquals($this->getUrl('CatsController@view', ['666']), '/cats/view/666/');
|
||||
$this->assertEquals('/cats/view/666/', $this->getUrl('CatsController@getView', ['666']));
|
||||
|
||||
// Should match /funny/man/
|
||||
$this->assertEquals($this->getUrl('/funny/man'), '/funny/man/');
|
||||
$this->assertEquals('/funny/man/', $this->getUrl('/funny/man'));
|
||||
|
||||
// Should match /?jackdaniels=true&cola=yeah
|
||||
$this->assertEquals($this->getUrl('home', null, ['jackdaniels' => 'true', 'cola' => 'yeah']), '/?jackdaniels=true&cola=yeah');
|
||||
$this->assertEquals('/?jackdaniels=true&cola=yeah', $this->getUrl('home', null, ['jackdaniels' => 'true', 'cola' => 'yeah']));
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user