mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 08:47:52 +00:00
@@ -18,7 +18,7 @@ The goal of this project is to create a router that is 100% compatible with the
|
|||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
|
||||||
- Basic routing (`GET`, `POST`, `PUT`, `DELETE`) with support for custom multiple verbs.
|
- Basic routing (`GET`, `POST`, `PUT`, `PATCH`, `UPDATE`, `DELETE`) with support for custom multiple verbs.
|
||||||
- Regular Expression Constraints for parameters.
|
- Regular Expression Constraints for parameters.
|
||||||
- Named routes.
|
- Named routes.
|
||||||
- Generating url to routes.
|
- Generating url to routes.
|
||||||
@@ -54,10 +54,10 @@ require_once 'routes.php';
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* The default namespace for route-callbacks, so we don't have to specify it each time.
|
* The default namespace for route-callbacks, so we don't have to specify it each time.
|
||||||
* Can be overwritten by using the namespace config option.
|
* Can be overwritten by using the namespace config option on your routes.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
SimpleRouter::setDefaultNamespace('MyWebsite\Controller');
|
SimpleRouter::setDefaultNamespace('MyWebsite');
|
||||||
|
|
||||||
// Start the routing
|
// Start the routing
|
||||||
SimpleRouter::start();
|
SimpleRouter::start();
|
||||||
@@ -78,45 +78,74 @@ use Pecee\SimpleRouter\SimpleRouter;
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* This route will match the url /v1/services/answers/1/
|
* This route will match the url /v1/services/answers/1/
|
||||||
|
|
||||||
* The middleware is just a class that renders before the
|
* The middleware is just a class that renders before the
|
||||||
* Controller or callback is loaded. This is useful for stopping
|
* controller or callback is loaded.
|
||||||
* the request, for instance if a user is not authenticated.
|
*
|
||||||
|
* This is useful for stopping the request, for
|
||||||
|
* instance if a user is not authenticated etc.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Add CSRF support (if needed)
|
|
||||||
SimpleRouter::csrfVerifier(new \Pecee\Http\Middleware\BaseCsrfVerifier());
|
// Add your csrfVerifier here
|
||||||
|
|
||||||
|
SimpleRouter::csrfVerifier(new \Demo\Middlewares\CsrfVerifier());
|
||||||
|
|
||||||
|
SimpleRouter::group(['middleware' => 'Middlewares\Site', 'exceptionHandler' => 'Handlers\CustomExceptionHandler'], function() {
|
||||||
|
|
||||||
|
|
||||||
|
SimpleRouter::get('/answers/{id}', 'ControllerAnswers@show', ['where' => ['id' => '[0-9]+']]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Using optional parameters
|
||||||
|
*/
|
||||||
|
SimpleRouter::get('/answers/{id?}', 'ControllerAnswers@show');
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This example will route url when matching the regular expression to the method.
|
||||||
|
* For example route: domain.com/ajax/music/world -> ControllerAjax@process (parameter: music/world)
|
||||||
|
*/
|
||||||
|
|
||||||
|
SimpleRouter::all('/ajax', 'ControllerAjax@process')->setMatch('.*?\\/ajax\\/([A-Za-z0-9\\/]+)');
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restful resource (see IRestController interface for available methods)
|
||||||
|
*/
|
||||||
|
|
||||||
|
SimpleRouter::resource('/rest', 'ControllerRessource');
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load the entire controller (where url matches method names - getIndex(), postIndex(), putIndex()).
|
||||||
|
* The url paths will determine which method to render.
|
||||||
|
*
|
||||||
|
* For example:
|
||||||
|
*
|
||||||
|
* GET /animals => getIndex()
|
||||||
|
* GET /animals/view => getView()
|
||||||
|
* POST /animals/save => postSave()
|
||||||
|
*
|
||||||
|
* etc.
|
||||||
|
*/
|
||||||
|
|
||||||
|
SimpleRouter::controller('/animals', 'ControllerAnimals');
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Example of providing callback instead of Controller
|
||||||
|
*/
|
||||||
|
|
||||||
|
SimpleRouter::post('/something', function() {
|
||||||
|
|
||||||
|
die('Callback example');
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
SimpleRouter::get('/page/404', 'ControllerPage@notFound', ['as' => 'page.notfound']);
|
SimpleRouter::get('/page/404', 'ControllerPage@notFound', ['as' => 'page.notfound']);
|
||||||
|
|
||||||
SimpleRouter::group(['prefix' => '/v1', 'middleware' => '\MyWebsite\Middleware\SomeMiddlewareClass'], function() {
|
|
||||||
|
|
||||||
SimpleRouter::group(['prefix' => '/services', 'exceptionHandler' => '\MyProject\Handler\CustomExceptionHandler'], function() {
|
|
||||||
|
|
||||||
SimpleRouter::get('/answers/{id}', 'ControllerAnswers@show')->where(['id' => '[0-9]+');
|
|
||||||
|
|
||||||
// Optional parameter
|
|
||||||
SimpleRouter::get('/answers/{id?}', 'ControllerAnswers@show');
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This example will route url when matching the regular expression to the method.
|
|
||||||
* For example route: domain.com/ajax/music/world -> ControllerAjax@process (parameter: music/world)
|
|
||||||
*/
|
|
||||||
SimpleRouter::all('/ajax', 'ControllerAjax@process')->match('.*?\\/ajax\\/([A-Za-z0-9\\/]+)');
|
|
||||||
|
|
||||||
// Restful resource (see IRestController interface for available methods)
|
|
||||||
SimpleRouter::resource('/rest', 'ControllerRessource');
|
|
||||||
|
|
||||||
// Load the entire controller (where url matches method names - getIndex(), postIndex() etc)
|
|
||||||
SimpleRouter::controller('/controller', 'ControllerDefault');
|
|
||||||
|
|
||||||
// Example of providing callback instead of Controller
|
|
||||||
SimpleRouter::get('/something', function() {
|
|
||||||
die('Callback example');
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### ExceptionHandler example
|
#### ExceptionHandler example
|
||||||
@@ -126,32 +155,45 @@ This is a basic example of an ExceptionHandler implementation:
|
|||||||
```php
|
```php
|
||||||
namespace Demo\Handlers;
|
namespace Demo\Handlers;
|
||||||
|
|
||||||
|
use Pecee\Handlers\IExceptionHandler;
|
||||||
use Pecee\Http\Request;
|
use Pecee\Http\Request;
|
||||||
|
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||||
use Pecee\SimpleRouter\RouterEntry;
|
use Pecee\SimpleRouter\RouterEntry;
|
||||||
|
|
||||||
class CustomExceptionHandler implements IExceptionHandler {
|
class CustomExceptionHandler implements IExceptionHandler
|
||||||
|
{
|
||||||
|
public function handleError(Request $request, RouterEntry &$route = null, \Exception $error)
|
||||||
|
{
|
||||||
|
|
||||||
public function handleError( Request $request, RouterEntry $router = null, \Exception $error) {
|
/* You can use the exception handler to format errors depending on the request and type. */
|
||||||
|
|
||||||
// If the error-code is 404; show another route which contains the page-not-found
|
if (stripos($request->getUri(), '/api') !== false) {
|
||||||
if($error->getCode() === 404) {
|
|
||||||
|
|
||||||
// Throw your custom 404-page view
|
|
||||||
// - or -
|
|
||||||
// load another route with our 404 page
|
|
||||||
// - or -
|
|
||||||
// you can return the $request object to ignore the error and continue on rendering the route.
|
|
||||||
|
|
||||||
return $request->setUri(url('page.notfound'));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Output error as json if on api path.
|
response()->json([
|
||||||
if(stripos($request->getUri(), '/api') !== false) {
|
'error' => $error->getMessage(),
|
||||||
response()->json([ 'error' => $error->getMessage() ]);
|
'code' => $error->getCode(),
|
||||||
}
|
]);
|
||||||
|
|
||||||
// Otherwise default exception will be thrown by the router.
|
}
|
||||||
}
|
|
||||||
|
/* The router will throw the NotFoundHttpException on 404 */
|
||||||
|
if($error instanceof NotFoundHttpException) {
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Render your own custom 404-view, rewrite the request to another route,
|
||||||
|
* or simply return the $request object to ignore the error and continue on rendering the route.
|
||||||
|
*
|
||||||
|
* The code below will make the router render our page.notfound route.
|
||||||
|
*/
|
||||||
|
|
||||||
|
$request->setUri(url('page.notfound'));
|
||||||
|
return $request;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
throw $error;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -162,9 +204,11 @@ Route groups may also be used to route wildcard sub-domains. Sub-domains may be
|
|||||||
|
|
||||||
```php
|
```php
|
||||||
Route::group(['domain' => '{account}.myapp.com'], function () {
|
Route::group(['domain' => '{account}.myapp.com'], function () {
|
||||||
|
|
||||||
Route::get('user/{id}', function ($account, $id) {
|
Route::get('user/{id}', function ($account, $id) {
|
||||||
//
|
// Do stuff...
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -182,14 +226,16 @@ use \Pecee\SimpleRouter\RouterRoute;
|
|||||||
$router = RouterBase::getInstance();
|
$router = RouterBase::getInstance();
|
||||||
|
|
||||||
$route = new RouterRoute('/answer/1', function() {
|
$route = new RouterRoute('/answer/1', function() {
|
||||||
|
|
||||||
die('this callback will match /answer/1');
|
die('this callback will match /answer/1');
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
$route->setMiddleware('\HSWebserviceV1\Middleware\AuthMiddleware');
|
$route->setMiddleware('\Demo\Middlewares\AuthMiddleware');
|
||||||
$route->setNamespace('MyWebsite');
|
$route->setNamespace('MyWebsite');
|
||||||
$route->setPrefix('v1');
|
$route->setPrefix('v1');
|
||||||
|
|
||||||
// Add the route to the router
|
/* Add the route to the router */
|
||||||
$router->addRoute($route);
|
$router->addRoute($route);
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -227,8 +273,25 @@ To simplify to use of simple-router functionality, we recommend you add these he
|
|||||||
```php
|
```php
|
||||||
use Pecee\SimpleRouter\SimpleRouter;
|
use Pecee\SimpleRouter\SimpleRouter;
|
||||||
|
|
||||||
function url($controller, $parameters = null, $getParams = null) {
|
/**
|
||||||
SimpleRouter::getRoute($controller, $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 $getParams
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
function url($name = null, $parameters = null, array $getParams = array()) {
|
||||||
|
SimpleRouter::getUrl($name, $parameters, $getParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -297,10 +360,10 @@ Querystrings are ignored.
|
|||||||
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
||||||
|
|
||||||
class CsrfVerifier extends BaseCsrfVerifier {
|
class CsrfVerifier extends BaseCsrfVerifier {
|
||||||
|
|
||||||
protected $except = [
|
protected $except = [
|
||||||
'/companies/*',
|
'/companies/*',
|
||||||
'/api'
|
'/api',
|
||||||
];
|
];
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -322,7 +385,7 @@ To interfere with the router, we create a class that inherits from ```RouterBoot
|
|||||||
use Pecee\Http\Request;
|
use Pecee\Http\Request;
|
||||||
use Pecee\SimpleRouter\RouterBootManager;
|
use Pecee\SimpleRouter\RouterBootManager;
|
||||||
|
|
||||||
class CustomRouterRules extends RouterBootManager{
|
class CustomRouterRules extends RouterBootManager {
|
||||||
|
|
||||||
public function boot(Request $request) {
|
public function boot(Request $request) {
|
||||||
|
|
||||||
@@ -389,7 +452,7 @@ The example below will cause the router to re-route the request with another url
|
|||||||
|
|
||||||
|
|
||||||
```php
|
```php
|
||||||
namespace demo\Middlewares;
|
namespace Demo\Middlewares;
|
||||||
|
|
||||||
use Pecee\Http\Middleware\IMiddleware;
|
use Pecee\Http\Middleware\IMiddleware;
|
||||||
use Pecee\Http\Request;
|
use Pecee\Http\Request;
|
||||||
@@ -398,7 +461,9 @@ use Pecee\SimpleRouter\RouterEntry;
|
|||||||
class CustomMiddleware implements Middleware {
|
class CustomMiddleware implements Middleware {
|
||||||
|
|
||||||
public function handle(Request $request, RouterEntry &$route) {
|
public function handle(Request $request, RouterEntry &$route) {
|
||||||
|
|
||||||
$request->setUri(url('home'));
|
$request->setUri(url('home'));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -415,7 +480,7 @@ If you wish to change the callback from outside, please have this in mind.
|
|||||||
**NOTE: Use this method if you want to load another controller. No additional middlewares or rules will be loaded.**
|
**NOTE: Use this method if you want to load another controller. No additional middlewares or rules will be loaded.**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
namespace demo\Middlewares;
|
namespace Demo\Middlewares;
|
||||||
|
|
||||||
use Pecee\Http\Middleware\IMiddleware;
|
use Pecee\Http\Middleware\IMiddleware;
|
||||||
use Pecee\Http\Request;
|
use Pecee\Http\Request;
|
||||||
@@ -424,7 +489,9 @@ use Pecee\SimpleRouter\RouterEntry;
|
|||||||
class CustomMiddleware implements Middleware {
|
class CustomMiddleware implements Middleware {
|
||||||
|
|
||||||
public function handle(Request $request, RouterEntry &$route) {
|
public function handle(Request $request, RouterEntry &$route) {
|
||||||
|
|
||||||
$route->callback('DefaultController@home');
|
$route->callback('DefaultController@home');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -435,22 +502,25 @@ class CustomMiddleware implements Middleware {
|
|||||||
We've added the `Input` class to easy access parameters from your Controller-classes.
|
We've added the `Input` class to easy access parameters from your Controller-classes.
|
||||||
|
|
||||||
**Return single parameter value (matches both GET, POST, FILE):**
|
**Return single parameter value (matches both GET, POST, FILE):**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$value = input()->get('name');
|
$value = input()->get('name');
|
||||||
```
|
```
|
||||||
|
|
||||||
**Return parameter object (matches both GET, POST, FILE):**
|
**Return parameter object (matches both GET, POST, FILE):**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$object = input()->getObject('name');
|
$object = input()->getObject('name');
|
||||||
```
|
```
|
||||||
|
|
||||||
**Return specific GET parameter (where name is the name of your parameter):**
|
**Return specific GET parameter (where name is the name of your parameter):**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$object = input()->get->name;
|
$object = input()->get->name;
|
||||||
$object = input()->post->name;
|
$object = input()->post->name;
|
||||||
$object = input()->file->name;
|
$object = input()->file->name;
|
||||||
|
|
||||||
// -- or --
|
# -- or --
|
||||||
|
|
||||||
$object = input()->get->get($key, $defaultValue);
|
$object = input()->get->get($key, $defaultValue);
|
||||||
$object = input()->post->get($key, $defaultValue);
|
$object = input()->post->get($key, $defaultValue);
|
||||||
@@ -458,6 +528,7 @@ $object = input()->file->get($key, $defaultValue);
|
|||||||
```
|
```
|
||||||
|
|
||||||
**Return all parameters:**
|
**Return all parameters:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
// Get all
|
// Get all
|
||||||
$values = input()->all();
|
$values = input()->all();
|
||||||
|
|||||||
@@ -1,34 +1,44 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Demo\Handlers;
|
namespace Demo\Handlers;
|
||||||
|
|
||||||
use Pecee\Handler\IExceptionHandler;
|
use Pecee\Handlers\IExceptionHandler;
|
||||||
use Pecee\Http\Request;
|
use Pecee\Http\Request;
|
||||||
|
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||||
use Pecee\SimpleRouter\RouterEntry;
|
use Pecee\SimpleRouter\RouterEntry;
|
||||||
|
|
||||||
class CustomExceptionHandler implements IExceptionHandler
|
class CustomExceptionHandler implements IExceptionHandler
|
||||||
{
|
{
|
||||||
public function handleError(Request $request, RouterEntry &$route = null, \Exception $error)
|
public function handleError(Request $request, RouterEntry &$route = null, \Exception $error)
|
||||||
{
|
{
|
||||||
// Return json errors if we encounter an error on /api.
|
|
||||||
|
/* You can use the exception handler to format errors depending on the request and type. */
|
||||||
|
|
||||||
if (stripos($request->getUri(), '/api') !== false) {
|
if (stripos($request->getUri(), '/api') !== false) {
|
||||||
header('content-type: application/json');
|
|
||||||
echo json_encode([
|
response()->json([
|
||||||
'error' => $error->getMessage(),
|
'error' => $error->getMessage(),
|
||||||
'code' => $error->getCode()
|
'code' => $error->getCode(),
|
||||||
]);
|
]);
|
||||||
die();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// else we just throw the error
|
/* The router will throw the NotFoundHttpException on 404 */
|
||||||
if ($error->getCode() == 404) {
|
if($error instanceof NotFoundHttpException) {
|
||||||
|
|
||||||
// Return 404 path
|
/*
|
||||||
$request->setUri('/404');
|
* Render your own custom 404-view, rewrite the request to another route,
|
||||||
|
* or simply return the $request object to ignore the error and continue on rendering the route.
|
||||||
|
*
|
||||||
|
* The code below will make the router render our page.notfound route.
|
||||||
|
*/
|
||||||
|
|
||||||
|
$request->setUri(url('page.notfound'));
|
||||||
return $request;
|
return $request;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw $error;
|
throw $error;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -9,11 +9,13 @@ Router::csrfVerifier(new \Demo\Middlewares\CsrfVerifier());
|
|||||||
|
|
||||||
Router::group(['exceptionHandler' => 'Demo\Handlers\CustomExceptionHandler'], function () {
|
Router::group(['exceptionHandler' => 'Demo\Handlers\CustomExceptionHandler'], function () {
|
||||||
|
|
||||||
Router::get('/', 'DefaultController@index')->setAlias('home');
|
Router::get('/', 'DefaultController@index')->setName('home');
|
||||||
Router::get('/contact', 'DefaultController@contact')->setAlias('contact');
|
|
||||||
Router::get('/404', 'DefaultController@notFound')->setAlias('404');
|
Router::get('/contact', 'DefaultController@contact')->setName('contact');
|
||||||
Router::basic('/companies', 'DefaultController@companies')->setAlias('companies');
|
|
||||||
Router::basic('/companies/{id}', 'DefaultController@companies')->setAlias('companies');
|
Router::get('/404', 'DefaultController@notFound')->setName('404');
|
||||||
|
|
||||||
|
Router::basic('/companies/{id?}', 'DefaultController@companies')->setName('companies');
|
||||||
|
|
||||||
// Api
|
// Api
|
||||||
Router::group(['prefix' => '/api', 'middleware' => 'Demo\Middlewares\ApiVerification'], function () {
|
Router::group(['prefix' => '/api', 'middleware' => 'Demo\Middlewares\ApiVerification'], function () {
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Pecee\Controller;
|
namespace Pecee\Controllers;
|
||||||
|
|
||||||
interface IRestController {
|
interface IRestController {
|
||||||
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace Pecee\Exception;
|
|
||||||
|
|
||||||
class RouterException extends \Exception
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Pecee\Handler;
|
namespace Pecee\Handlers;
|
||||||
|
|
||||||
use Pecee\Http\Request;
|
use Pecee\Http\Request;
|
||||||
use Pecee\SimpleRouter\RouterEntry;
|
use Pecee\SimpleRouter\RouterEntry;
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
namespace Pecee\Http\Middleware;
|
namespace Pecee\Http\Middleware;
|
||||||
|
|
||||||
use Pecee\CsrfToken;
|
use Pecee\CsrfToken;
|
||||||
use Pecee\Exception\TokenMismatchException;
|
use Pecee\Exceptions\TokenMismatchException;
|
||||||
use Pecee\Http\Request;
|
use Pecee\Http\Request;
|
||||||
use Pecee\SimpleRouter\RouterEntry;
|
use Pecee\SimpleRouter\RouterEntry;
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Pecee\Exception;
|
namespace Pecee\Exceptions;
|
||||||
|
|
||||||
class TokenMismatchException extends \Exception
|
class TokenMismatchException extends \Exception
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
namespace Pecee\SimpleRouter\Exceptions;
|
||||||
|
|
||||||
|
class HttpException extends \Exception
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
<?php
|
||||||
|
namespace Pecee\SimpleRouter\Exceptions;
|
||||||
|
|
||||||
|
class NotFoundHttpException extends HttpException
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -8,7 +8,7 @@ abstract class LoadableRoute extends RouterEntry implements ILoadableRoute
|
|||||||
const PARAMETER_OPTIONAL_SYMBOL = '?';
|
const PARAMETER_OPTIONAL_SYMBOL = '?';
|
||||||
|
|
||||||
protected $url;
|
protected $url;
|
||||||
protected $alias;
|
protected $names = [];
|
||||||
|
|
||||||
public function getUrl()
|
public function getUrl()
|
||||||
{
|
{
|
||||||
@@ -36,44 +36,94 @@ abstract class LoadableRoute extends RouterEntry implements ILoadableRoute
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get alias for the url which can be used when getting the url route.
|
* Returns the provided name of the router (first if multiple).
|
||||||
|
* Alias for LoadableRoute::getName().
|
||||||
|
*
|
||||||
|
* @see LoadableRoute::getName()
|
||||||
* @return string|array
|
* @return string|array
|
||||||
*/
|
*/
|
||||||
public function getAlias()
|
public function getAlias()
|
||||||
{
|
{
|
||||||
return $this->alias;
|
return $this->getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if route has given alias.
|
* 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
|
* @param string $name
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasAlias($name)
|
public function hasName($name)
|
||||||
{
|
{
|
||||||
if ($this->getAlias() !== null) {
|
return (in_array($name, $this->names, false) !== false);
|
||||||
if (is_array($this->getAlias()) === true) {
|
|
||||||
foreach ($this->getAlias() as $alias) {
|
|
||||||
if (strtolower($alias) === strtolower($name)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return strtolower($this->getAlias()) === strtolower($name);
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the url alias for easier getting the url route.
|
* Sets the router name, which makes it easier to obtain the url or router at a later point.
|
||||||
* @param string|array $alias
|
* Alias for LoadableRoute::setName().
|
||||||
|
*
|
||||||
|
* @see LoadableRoute::setName()
|
||||||
|
* @param string|array $name
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setAlias($alias)
|
public function setAlias($name)
|
||||||
{
|
{
|
||||||
$this->alias = $alias;
|
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;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,12 +135,16 @@ abstract class LoadableRoute extends RouterEntry implements ILoadableRoute
|
|||||||
*/
|
*/
|
||||||
public function merge(array $values)
|
public function merge(array $values)
|
||||||
{
|
{
|
||||||
// Change as to alias
|
|
||||||
if (isset($values['as'])) {
|
if (isset($values['as'])) {
|
||||||
$this->setAlias($values['as']);
|
$this->setNames((array)$values['as']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($values['prefix'])) {
|
||||||
|
$this->setUrl($values['prefix'] . $this->getUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
parent::merge($values);
|
parent::merge($values);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Pecee\SimpleRouter;
|
namespace Pecee\SimpleRouter;
|
||||||
|
|
||||||
use Pecee\Exception\RouterException;
|
use Pecee\Handlers\IExceptionHandler;
|
||||||
use Pecee\Handler\IExceptionHandler;
|
|
||||||
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
||||||
use Pecee\Http\Request;
|
use Pecee\Http\Request;
|
||||||
use Pecee\Http\Response;
|
use Pecee\Http\Response;
|
||||||
|
use Pecee\SimpleRouter\Exceptions\HttpException;
|
||||||
|
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||||
|
|
||||||
class RouterBase
|
class RouterBase
|
||||||
{
|
{
|
||||||
@@ -111,11 +112,11 @@ class RouterBase
|
|||||||
$this->processingRoute = false;
|
$this->processingRoute = false;
|
||||||
$this->request = new Request();
|
$this->request = new Request();
|
||||||
$this->response = new Response($this->request);
|
$this->response = new Response($this->request);
|
||||||
$this->routes = array();
|
$this->routes = [];
|
||||||
$this->bootManagers = array();
|
$this->bootManagers = [];
|
||||||
$this->backStack = array();
|
$this->backStack = [];
|
||||||
$this->controllerUrlMap = array();
|
$this->controllerUrlMap = [];
|
||||||
$this->exceptionHandlers = array();
|
$this->exceptionHandlers = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -134,25 +135,16 @@ class RouterBase
|
|||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), RouterEntry $parent = null)
|
protected function processRoutes(array $routes, RouterGroup $group = null, RouterEntry $parent = null)
|
||||||
{
|
{
|
||||||
// Loop through each route-request
|
// Loop through each route-request
|
||||||
/* @var $route RouterEntry */
|
/* @var $route RouterEntry */
|
||||||
foreach ($routes as $route) {
|
foreach ($routes as $route) {
|
||||||
|
|
||||||
$newPrefixes = $prefixes;
|
|
||||||
$newSettings = $settings;
|
|
||||||
|
|
||||||
if ($parent !== null) {
|
|
||||||
$route->setParent($parent);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count($settings)) {
|
|
||||||
$route->merge($settings);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($route instanceof RouterGroup) {
|
if ($route instanceof RouterGroup) {
|
||||||
|
|
||||||
|
$group = $route;
|
||||||
|
|
||||||
if ($route->getCallback() !== null && is_callable($route->getCallback())) {
|
if ($route->getCallback() !== null && is_callable($route->getCallback())) {
|
||||||
|
|
||||||
$this->processingRoute = true;
|
$this->processingRoute = true;
|
||||||
@@ -160,33 +152,47 @@ class RouterBase
|
|||||||
$this->processingRoute = false;
|
$this->processingRoute = false;
|
||||||
|
|
||||||
if ($route->matchRoute($this->request)) {
|
if ($route->matchRoute($this->request)) {
|
||||||
// Add ExceptionHandler
|
|
||||||
|
/* Add exceptionhandlers */
|
||||||
if (count($route->getExceptionHandlers()) > 0) {
|
if (count($route->getExceptionHandlers()) > 0) {
|
||||||
$this->exceptionHandlers = array_merge($route->getExceptionHandlers(), $this->exceptionHandlers);
|
$this->exceptionHandlers = array_merge($route->getExceptionHandlers(), $this->exceptionHandlers);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$newPrefixes[] = trim($route->getPrefix(), '/');
|
if ($group !== null) {
|
||||||
$newSettings = array_merge($settings, $route->toArray());
|
|
||||||
|
/* Add the parent group */
|
||||||
|
$route->setGroup($group);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($parent !== null) {
|
||||||
|
|
||||||
|
/* Add the parent route */
|
||||||
|
$route->setParent($parent);
|
||||||
|
|
||||||
|
/* Add/merge parent settings with child */
|
||||||
|
$route->merge($parent->toArray());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($route instanceof ILoadableRoute) {
|
if ($route instanceof ILoadableRoute) {
|
||||||
|
|
||||||
if (count($prefixes)) {
|
/* Add the route to the map, so we can find the active one when all routes has been loaded */
|
||||||
$route->setUrl(trim(join('/', $prefixes) . $route->getUrl(), '/'));
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->controllerUrlMap[] = $route;
|
$this->controllerUrlMap[] = $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count($this->backStack) > 0) {
|
if (count($this->backStack) > 0) {
|
||||||
|
|
||||||
|
/* Pop and grap the routes added when executing group callback earlier */
|
||||||
$backStack = $this->backStack;
|
$backStack = $this->backStack;
|
||||||
$this->backStack = [];
|
$this->backStack = [];
|
||||||
|
|
||||||
// Route any routes added to the backstack
|
/* Route any routes added to the backstack */
|
||||||
$this->processRoutes($backStack, $newSettings, $newPrefixes, $route);
|
$this->processRoutes($backStack, $route, $group);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -205,7 +211,7 @@ class RouterBase
|
|||||||
$this->request = $manager->boot($this->request);
|
$this->request = $manager->boot($this->request);
|
||||||
|
|
||||||
if (!($this->request instanceof Request)) {
|
if (!($this->request instanceof Request)) {
|
||||||
throw new RouterException('Custom router bootmanager "' . get_class($manager) . '" must return instance of Request.');
|
throw new HttpException('Bootmanager "' . get_class($manager) . '" must return instance of ' . Request::class, 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -240,6 +246,7 @@ class RouterBase
|
|||||||
if ($this->request->getUri() !== $this->originalUrl && !in_array($this->request->getUri(), $this->routeRewrites)) {
|
if ($this->request->getUri() !== $this->originalUrl && !in_array($this->request->getUri(), $this->routeRewrites)) {
|
||||||
$this->routeRewrites[] = $this->request->getUri();
|
$this->routeRewrites[] = $this->request->getUri();
|
||||||
$this->routeRequest(true);
|
$this->routeRequest(true);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,12 +262,12 @@ class RouterBase
|
|||||||
$this->handleException($e);
|
$this->handleException($e);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($routeNotAllowed) {
|
if ($routeNotAllowed === true) {
|
||||||
$this->handleException(new RouterException('Route or method not allowed', 403));
|
$this->handleException(new HttpException('Route or method not allowed', 403));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->loadedRoute === null) {
|
if ($this->loadedRoute === null) {
|
||||||
$this->handleException(new RouterException(sprintf('Route not found: %s', $this->request->getUri()), 404));
|
$this->handleException(new NotFoundHttpException('Route not found: ' . $this->request->getUri(), 404));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -272,7 +279,7 @@ class RouterBase
|
|||||||
$handler = new $handler();
|
$handler = new $handler();
|
||||||
|
|
||||||
if (!($handler instanceof IExceptionHandler)) {
|
if (!($handler instanceof IExceptionHandler)) {
|
||||||
throw new RouterException('Exception handler must implement the IExceptionHandler interface.');
|
throw new HttpException('Exception handler must implement the IExceptionHandler interface.', 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
$request = $handler->handleError($this->request, $this->loadedRoute, $e);
|
$request = $handler->handleError($this->request, $this->loadedRoute, $e);
|
||||||
@@ -280,17 +287,17 @@ class RouterBase
|
|||||||
if ($request !== null && $request->getUri() !== $this->originalUrl && !in_array($request->getUri(), $this->routeRewrites)) {
|
if ($request !== null && $request->getUri() !== $this->originalUrl && !in_array($request->getUri(), $this->routeRewrites)) {
|
||||||
$this->routeRewrites[] = $request->getUri();
|
$this->routeRewrites[] = $request->getUri();
|
||||||
$this->routeRequest(true);
|
$this->routeRequest(true);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function arrayToParams(array $getParams = null, $includeEmpty = true)
|
public function arrayToParams(array $getParams = [], $includeEmpty = true)
|
||||||
{
|
{
|
||||||
if (is_array($getParams) === true && count($getParams) > 0) {
|
if (count($getParams) > 0) {
|
||||||
|
|
||||||
if ($includeEmpty === false) {
|
if ($includeEmpty === false) {
|
||||||
$getParams = array_filter($getParams, function ($item) {
|
$getParams = array_filter($getParams, function ($item) {
|
||||||
@@ -304,151 +311,168 @@ class RouterBase
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function processUrl(LoadableRoute $route, $method = null, $parameters = null, $getParams = null)
|
protected function processUrl(LoadableRoute $route, $method = null, $parameters = null, array $getParams = [])
|
||||||
{
|
{
|
||||||
$domain = '';
|
$url = '';
|
||||||
$parent = $route->getParent();
|
|
||||||
|
|
||||||
$parameters = (array)$parameters;
|
$parameters = (array)$parameters;
|
||||||
|
|
||||||
if ($parent !== null && $parent instanceof RouterGroup && count($parent->getDomains()) > 0) {
|
if ($route->getGroup() !== null && count($route->getGroup()->getDomains()) > 0) {
|
||||||
$domain = $parent->getDomains();
|
$url .= '//' . $route->getGroup()->getDomains()[0];
|
||||||
$domain = '//' . $domain[0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$url = $domain . '/' . trim($route->getUrl(), '/');
|
$url .= '/' . trim($route->getUrl(), '/');
|
||||||
|
|
||||||
if ($route instanceof IControllerRoute && $method !== null) {
|
if ($route instanceof IControllerRoute && $method !== null) {
|
||||||
|
|
||||||
$url .= '/' . $method . '/';
|
$url .= '/' . $method . '/';
|
||||||
|
|
||||||
if (count($parameters) > 0) {
|
if (count($parameters) > 0) {
|
||||||
$url .= join('/', (array)$parameters);
|
$url .= join('/', $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if ($parameters !== null && count($parameters) > 0) {
|
$params = array_merge($route->getParameters(), $parameters);
|
||||||
$params = array_merge($route->getParameters(), (array)$parameters);
|
|
||||||
} else {
|
|
||||||
$params = $route->getParameters();
|
|
||||||
}
|
|
||||||
|
|
||||||
$otherParams = array();
|
/* 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) {
|
foreach ($params as $param => $value) {
|
||||||
$value = (isset($parameters[$param])) ? $parameters[$param] : $value;
|
$value = (isset($parameters[$param])) ? $parameters[$param] : $value;
|
||||||
|
|
||||||
|
/* Create the param string - {} */
|
||||||
$param1 = LoadableRoute::PARAMETER_MODIFIERS[0] . $param . LoadableRoute::PARAMETER_MODIFIERS[1];
|
$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];
|
$param2 = LoadableRoute::PARAMETER_MODIFIERS[0] . $param . LoadableRoute::PARAMETER_OPTIONAL_SYMBOL . LoadableRoute::PARAMETER_MODIFIERS[1];
|
||||||
|
|
||||||
if (stripos($url, $param1) !== false || stripos($url, $param) !== false) {
|
if (stripos($url, $param1) !== false || stripos($url, $param) !== false) {
|
||||||
$url = str_ireplace([$param1, $param2], $value, $url);
|
$url = str_ireplace([$param1, $param2], $value, $url);
|
||||||
} else {
|
} else {
|
||||||
$otherParams[$param] = $value;
|
$unknownParams[$param] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$url = rtrim($url, '/') . '/' . join('/', $otherParams);
|
$url = rtrim($url, '/') . '/' . join('/', $unknownParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
$url = rtrim($url, '/') . '/';
|
return rtrim($url, '/') . '/' . $this->arrayToParams($getParams);
|
||||||
|
|
||||||
if ($getParams !== null) {
|
|
||||||
$url .= $this->arrayToParams($getParams);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $url;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find route by alias, class, callback or method.
|
* Find route by alias, class, callback or method.
|
||||||
*
|
*
|
||||||
* @param string $query
|
* @param string $name
|
||||||
* @return LoadableRoute|null
|
* @return LoadableRoute|null
|
||||||
*/
|
*/
|
||||||
public function findRoute($query)
|
public function findRoute($name)
|
||||||
{
|
{
|
||||||
/* @var $route LoadableRoute */
|
/* @var $route LoadableRoute */
|
||||||
foreach ($this->controllerUrlMap as $route) {
|
foreach ($this->controllerUrlMap as $route) {
|
||||||
|
|
||||||
// Check an alias exist, if the matches - use it
|
/* Check if the name matches with a name on the route. Should match either router alias or controller alias. */
|
||||||
// Matches either Router alias or controller alias.
|
if ($route->hasName($name)) {
|
||||||
if ($route->hasAlias($query)) {
|
|
||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Direct match to controller
|
/* Direct match to controller */
|
||||||
if ($route instanceof IControllerRoute) {
|
if ($route instanceof IControllerRoute && strtolower($route->getController()) === strtolower($name)) {
|
||||||
if (strtolower($route->getController()) === strtolower($query)) {
|
return $route;
|
||||||
return $route;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Using @ is most definitely a controller@method or alias@method
|
/* Using @ is most definitely a controller@method or alias@method */
|
||||||
if (strpos($query, '@') !== false) {
|
if (strpos($name, '@') !== false) {
|
||||||
list($controller, $method) = array_map('strtolower', explode('@', $query));
|
list($controller, $method) = array_map('strtolower', explode('@', $name));
|
||||||
|
|
||||||
if ($controller === strtolower($route->getClass()) && $method === strtolower($route->getMethod())) {
|
if ($controller === strtolower($route->getClass()) && $method === strtolower($route->getMethod())) {
|
||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use callback if it's not a function
|
/* Check if callback matches (if it's not a function) */
|
||||||
if (strpos($query, '@') !== false && strpos($route->getCallback(), '@') !== false && !is_callable($route->getCallback())) {
|
if (strpos($name, '@') !== false && strpos($route->getCallback(), '@') !== false && !is_callable($route->getCallback())) {
|
||||||
|
|
||||||
if (strtolower($query) === strtolower($route->getClass())) {
|
/* Check if the entire callback is matching */
|
||||||
|
if (strtolower($route->getCallback()) === strtolower($name) || strpos($route->getCallback(), $name) === 0) {
|
||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strtolower($route->getCallback()) === strtolower($query) || strpos($route->getCallback(), $query) === 0) {
|
/* Check if the class part of the callback matches (class@method) */
|
||||||
|
if (strtolower($name) === strtolower($route->getClass())) {
|
||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRoute($controller = null, $parameters = null, $getParams = 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.
|
||||||
|
*
|
||||||
|
* 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 function getUrl($name = null, $parameters = null, array $getParams = null)
|
||||||
{
|
{
|
||||||
if ($getParams !== null && is_array($getParams) === false) {
|
if ($getParams !== null && is_array($getParams) === false) {
|
||||||
throw new \InvalidArgumentException('Invalid type for getParams. Must be array or null');
|
throw new \InvalidArgumentException('Invalid type for getParams. Must be array or null');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return current route if no options has been specified
|
if ($getParams === null) {
|
||||||
if ($controller === null && $parameters === null) {
|
$getParams = $_GET;
|
||||||
|
|
||||||
$getParams = ($getParams !== null) ? $getParams : $_GET;
|
|
||||||
$url = parse_url($this->request->getUri(), PHP_URL_PATH) . $this->arrayToParams($getParams);
|
|
||||||
|
|
||||||
return $url;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If nothing is defined and a route is loaded we use that
|
/* Return current route if no options has been specified */
|
||||||
if ($controller === null && $this->loadedRoute !== null) {
|
if ($name === null && $parameters === null) {
|
||||||
|
return '/' . trim(parse_url($this->request->getUri(), PHP_URL_PATH), '/') . '/' . $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->processUrl($this->loadedRoute, $this->loadedRoute->getMethod(), $parameters, $getParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
$route = $this->findRoute($controller);
|
/* We try to find a match on the given name */
|
||||||
|
$route = $this->findRoute($name);
|
||||||
|
|
||||||
if ($route !== null) {
|
if ($route !== null) {
|
||||||
return $this->processUrl($route, $route->getMethod(), $parameters, $getParams);
|
return $this->processUrl($route, $route->getMethod(), $parameters, $getParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Using @ is most definitely a controller@method or alias@method
|
/* Using @ is most definitely a controller@method or alias@method */
|
||||||
if (stripos($controller, '@') !== false) {
|
if (stripos($name, '@') !== false) {
|
||||||
list($controller, $method) = explode('@', $controller);
|
list($controller, $method) = explode('@', $name);
|
||||||
|
|
||||||
|
/* Loop through all the routes to see if we can find a match */
|
||||||
|
|
||||||
/* @var $route LoadableRoute */
|
/* @var $route LoadableRoute */
|
||||||
foreach ($this->controllerUrlMap as $route) {
|
foreach ($this->controllerUrlMap as $route) {
|
||||||
|
|
||||||
if ($route->hasAlias($controller)) {
|
/* Check if the route contains the name/alias */
|
||||||
|
if ($route->hasName($controller)) {
|
||||||
return $this->processUrl($route, $method, $parameters, $getParams);
|
return $this->processUrl($route, $method, $parameters, $getParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Match controllers either by: "alias @ method" or "controller@method"
|
/* Check if the route controller is equal to the name */
|
||||||
if ($route instanceof IControllerRoute && strtolower($route->getController()) === strtolower($controller)) {
|
if ($route instanceof IControllerRoute && strtolower($route->getController()) === strtolower($controller)) {
|
||||||
return $this->processUrl($route, $method, $parameters, $getParams);
|
return $this->processUrl($route, $method, $parameters, $getParams);
|
||||||
}
|
}
|
||||||
@@ -456,19 +480,9 @@ class RouterBase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$url = [($controller === null) ? '/' : $controller];
|
/* No result so we assume that someone is using a hardcoded url and join everything together. */
|
||||||
|
|
||||||
if ($parameters !== null && count($parameters) > 0) {
|
return '/' . trim(join('/', array_merge((array)$name, (array)$parameters)), '/') . '/' . $this->arrayToParams($getParams);
|
||||||
$url = array_merge($url, (array)$parameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
$url = '/' . trim(join('/', $url), '/') . '/';
|
|
||||||
|
|
||||||
if ($getParams !== null) {
|
|
||||||
$url .= $this->arrayToParams($getParams);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $url;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -543,6 +557,7 @@ class RouterBase
|
|||||||
public function setCsrfVerifier(BaseCsrfVerifier $csrfVerifier)
|
public function setCsrfVerifier(BaseCsrfVerifier $csrfVerifier)
|
||||||
{
|
{
|
||||||
$this->csrfVerifier = $csrfVerifier;
|
$this->csrfVerifier = $csrfVerifier;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Pecee\SimpleRouter;
|
namespace Pecee\SimpleRouter;
|
||||||
|
|
||||||
use Pecee\Exception\RouterException;
|
|
||||||
use Pecee\Http\Request;
|
use Pecee\Http\Request;
|
||||||
|
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||||
|
|
||||||
class RouterController extends LoadableRoute implements IControllerRoute
|
class RouterController extends LoadableRoute implements IControllerRoute
|
||||||
{
|
{
|
||||||
@@ -31,10 +31,10 @@ class RouterController extends LoadableRoute implements IControllerRoute
|
|||||||
$method = $request->getMethod() . ucfirst($controller[1]);
|
$method = $request->getMethod() . ucfirst($controller[1]);
|
||||||
|
|
||||||
if (!method_exists($class, $method)) {
|
if (!method_exists($class, $method)) {
|
||||||
throw new RouterException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
|
throw new NotFoundHttpException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
call_user_func_array(array($class, $method), $this->getParameters());
|
call_user_func_array([$class, $method], $this->getParameters());
|
||||||
|
|
||||||
return $class;
|
return $class;
|
||||||
}
|
}
|
||||||
@@ -86,6 +86,7 @@ class RouterController extends LoadableRoute implements IControllerRoute
|
|||||||
public function setController($controller)
|
public function setController($controller)
|
||||||
{
|
{
|
||||||
$this->controller = $controller;
|
$this->controller = $controller;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,6 +105,7 @@ class RouterController extends LoadableRoute implements IControllerRoute
|
|||||||
public function setMethod($method)
|
public function setMethod($method)
|
||||||
{
|
{
|
||||||
$this->method = $method;
|
$this->method = $method;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Pecee\SimpleRouter;
|
namespace Pecee\SimpleRouter;
|
||||||
|
|
||||||
use Pecee\Exception\RouterException;
|
|
||||||
use Pecee\Http\Middleware\IMiddleware;
|
use Pecee\Http\Middleware\IMiddleware;
|
||||||
use Pecee\Http\Request;
|
use Pecee\Http\Request;
|
||||||
|
use Pecee\SimpleRouter\Exceptions\HttpException;
|
||||||
|
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||||
|
|
||||||
abstract class RouterEntry
|
abstract class RouterEntry
|
||||||
{
|
{
|
||||||
@@ -23,21 +24,23 @@ abstract class RouterEntry
|
|||||||
self::REQUEST_TYPE_DELETE,
|
self::REQUEST_TYPE_DELETE,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
protected $group;
|
||||||
protected $parent;
|
protected $parent;
|
||||||
protected $callback;
|
protected $callback;
|
||||||
|
|
||||||
protected $namespace;
|
|
||||||
protected $defaultNamespace;
|
protected $defaultNamespace;
|
||||||
|
|
||||||
|
/* Default options */
|
||||||
|
protected $namespace;
|
||||||
protected $regex;
|
protected $regex;
|
||||||
protected $requestMethods = array();
|
protected $requestMethods = [];
|
||||||
protected $where = array();
|
protected $where = [];
|
||||||
protected $parameters = array();
|
protected $parameters = [];
|
||||||
protected $middlewares = array();
|
protected $middlewares = [];
|
||||||
|
|
||||||
protected function loadClass($name)
|
protected function loadClass($name)
|
||||||
{
|
{
|
||||||
if (!class_exists($name)) {
|
if (!class_exists($name)) {
|
||||||
throw new RouterException(sprintf('Class %s does not exist', $name));
|
throw new HttpException(sprintf('Class %s does not exist', $name), 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new $name();
|
return new $name();
|
||||||
@@ -45,19 +48,18 @@ abstract class RouterEntry
|
|||||||
|
|
||||||
protected function parseParameters($route, $url, $parameterRegex = '[\w]+')
|
protected function parseParameters($route, $url, $parameterRegex = '[\w]+')
|
||||||
{
|
{
|
||||||
$parameterNames = array();
|
$parameterNames = [];
|
||||||
$regex = '';
|
$regex = '';
|
||||||
$lastCharacter = '';
|
$lastCharacter = '';
|
||||||
$isParameter = false;
|
$isParameter = false;
|
||||||
$parameter = '';
|
$parameter = '';
|
||||||
|
|
||||||
$routeLength = strlen($route);
|
for ($i = 0; $i < strlen($route); $i++) {
|
||||||
for ($i = 0; $i < $routeLength; $i++) {
|
|
||||||
|
|
||||||
$character = $route[$i];
|
$character = $route[$i];
|
||||||
|
|
||||||
if ($character === '{') {
|
if ($character === '{') {
|
||||||
// Remove "/" and "\" from regex
|
/* Remove "/" and "\" from regex */
|
||||||
if (substr($regex, strlen($regex) - 1) === '/') {
|
if (substr($regex, strlen($regex) - 1) === '/') {
|
||||||
$regex = substr($regex, 0, strlen($regex) - 2);
|
$regex = substr($regex, 0, strlen($regex) - 2);
|
||||||
}
|
}
|
||||||
@@ -66,7 +68,7 @@ abstract class RouterEntry
|
|||||||
} elseif ($isParameter && $character === '}') {
|
} elseif ($isParameter && $character === '}') {
|
||||||
$required = true;
|
$required = true;
|
||||||
|
|
||||||
// Check for optional parameter and use custom parameter regex if it exists
|
/* Check for optional parameter and use custom parameter regex if it exists */
|
||||||
if (is_array($this->where) === true && isset($this->where[$parameter])) {
|
if (is_array($this->where) === true && isset($this->where[$parameter])) {
|
||||||
$parameterRegex = $this->where[$parameter];
|
$parameterRegex = $this->where[$parameter];
|
||||||
}
|
}
|
||||||
@@ -80,8 +82,8 @@ abstract class RouterEntry
|
|||||||
}
|
}
|
||||||
|
|
||||||
$parameterNames[] = [
|
$parameterNames[] = [
|
||||||
'name' => $parameter,
|
'name' => $parameter,
|
||||||
'required' => $required
|
'required' => $required,
|
||||||
];
|
];
|
||||||
|
|
||||||
$parameter = '';
|
$parameter = '';
|
||||||
@@ -97,17 +99,17 @@ abstract class RouterEntry
|
|||||||
$lastCharacter = $character;
|
$lastCharacter = $character;
|
||||||
}
|
}
|
||||||
|
|
||||||
$parameterValues = array();
|
$parameterValues = [];
|
||||||
|
|
||||||
if (preg_match('/^' . $regex . '\/?$/is', $url, $parameterValues)) {
|
if (preg_match('/^' . $regex . '\/?$/is', $url, $parameterValues)) {
|
||||||
|
|
||||||
$parameters = array();
|
$parameters = [];
|
||||||
|
|
||||||
foreach ($parameterNames as $name) {
|
foreach ($parameterNames as $name) {
|
||||||
$parameterValue = isset($parameterValues[$name['name']]) ? $parameterValues[$name['name']] : null;
|
$parameterValue = isset($parameterValues[$name['name']]) ? $parameterValues[$name['name']] : null;
|
||||||
|
|
||||||
if ($name['required'] && $parameterValue === null) {
|
if ($name['required'] && $parameterValue === null) {
|
||||||
throw new RouterException('Missing required parameter ' . $name['name'], 404);
|
throw new HttpException('Missing required parameter ' . $name['name'], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($name['required'] === false && $parameterValue === null) {
|
if ($name['required'] === false && $parameterValue === null) {
|
||||||
@@ -130,12 +132,10 @@ abstract class RouterEntry
|
|||||||
|
|
||||||
$middleware = $this->loadClass($middleware);
|
$middleware = $this->loadClass($middleware);
|
||||||
if (!($middleware instanceof IMiddleware)) {
|
if (!($middleware instanceof IMiddleware)) {
|
||||||
throw new RouterException($middleware . ' must be instance of Middleware');
|
throw new HttpException($middleware . ' must be instance of Middleware');
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @var $class IMiddleware */
|
|
||||||
$middleware->handle($request, $route);
|
$middleware->handle($request, $route);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -144,12 +144,12 @@ abstract class RouterEntry
|
|||||||
{
|
{
|
||||||
if ($this->getCallback() !== null && is_callable($this->getCallback())) {
|
if ($this->getCallback() !== null && is_callable($this->getCallback())) {
|
||||||
|
|
||||||
// When the callback is a function
|
/* When the callback is a function */
|
||||||
call_user_func_array($this->getCallback(), $this->getParameters());
|
call_user_func_array($this->getCallback(), $this->getParameters());
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// When the callback is a method
|
/* When the callback is a method */
|
||||||
$controller = explode('@', $this->getCallback());
|
$controller = explode('@', $this->getCallback());
|
||||||
$className = $this->getNamespace() . '\\' . $controller[0];
|
$className = $this->getNamespace() . '\\' . $controller[0];
|
||||||
|
|
||||||
@@ -157,14 +157,14 @@ abstract class RouterEntry
|
|||||||
$method = $controller[1];
|
$method = $controller[1];
|
||||||
|
|
||||||
if (!method_exists($class, $method)) {
|
if (!method_exists($class, $method)) {
|
||||||
throw new RouterException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
|
throw new NotFoundHttpException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$parameters = array_filter($this->getParameters(), function ($var) {
|
$parameters = array_filter($this->getParameters(), function ($var) {
|
||||||
return ($var !== null);
|
return ($var !== null);
|
||||||
});
|
});
|
||||||
|
|
||||||
call_user_func_array(array($class, $method), $parameters);
|
call_user_func_array([$class, $method], $parameters);
|
||||||
|
|
||||||
return $class;
|
return $class;
|
||||||
}
|
}
|
||||||
@@ -184,6 +184,7 @@ abstract class RouterEntry
|
|||||||
if (strpos($this->callback, '@') !== false) {
|
if (strpos($this->callback, '@') !== false) {
|
||||||
return $this->callback;
|
return $this->callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'function_' . md5($this->callback);
|
return 'function_' . md5($this->callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,6 +197,7 @@ abstract class RouterEntry
|
|||||||
public function setRequestMethods(array $methods)
|
public function setRequestMethods(array $methods)
|
||||||
{
|
{
|
||||||
$this->requestMethods = $methods;
|
$this->requestMethods = $methods;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,13 +211,33 @@ abstract class RouterEntry
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return RouterEntry
|
* @return LoadableRoute
|
||||||
*/
|
*/
|
||||||
public function getParent()
|
public function getParent()
|
||||||
{
|
{
|
||||||
return $this->parent;
|
return $this->parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the group for the route.
|
||||||
|
*
|
||||||
|
* @return RouterGroup|null
|
||||||
|
*/
|
||||||
|
public function getGroup()
|
||||||
|
{
|
||||||
|
return $this->group;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set group
|
||||||
|
*
|
||||||
|
* @param RouterGroup $group
|
||||||
|
*/
|
||||||
|
public function setGroup(RouterGroup $group)
|
||||||
|
{
|
||||||
|
$this->group = $group;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set parent route
|
* Set parent route
|
||||||
*
|
*
|
||||||
@@ -225,6 +247,7 @@ abstract class RouterEntry
|
|||||||
public function setParent(RouterEntry $parent)
|
public function setParent(RouterEntry $parent)
|
||||||
{
|
{
|
||||||
$this->parent = $parent;
|
$this->parent = $parent;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -235,6 +258,7 @@ abstract class RouterEntry
|
|||||||
public function setCallback($callback)
|
public function setCallback($callback)
|
||||||
{
|
{
|
||||||
$this->callback = $callback;
|
$this->callback = $callback;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,8 +274,10 @@ abstract class RouterEntry
|
|||||||
{
|
{
|
||||||
if (strpos($this->callback, '@') !== false) {
|
if (strpos($this->callback, '@') !== false) {
|
||||||
$tmp = explode('@', $this->callback);
|
$tmp = explode('@', $this->callback);
|
||||||
|
|
||||||
return $tmp[1];
|
return $tmp[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,20 +285,24 @@ abstract class RouterEntry
|
|||||||
{
|
{
|
||||||
if (strpos($this->callback, '@') !== false) {
|
if (strpos($this->callback, '@') !== false) {
|
||||||
$tmp = explode('@', $this->callback);
|
$tmp = explode('@', $this->callback);
|
||||||
|
|
||||||
return $tmp[0];
|
return $tmp[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setMethod($method)
|
public function setMethod($method)
|
||||||
{
|
{
|
||||||
$this->callback = sprintf('%s@%s', $this->getClass(), $method);
|
$this->callback = sprintf('%s@%s', $this->getClass(), $method);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setClass($class)
|
public function setClass($class)
|
||||||
{
|
{
|
||||||
$this->callback = sprintf('%s@%s', $class, $this->getMethod());
|
$this->callback = sprintf('%s@%s', $class, $this->getMethod());
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -283,12 +313,14 @@ abstract class RouterEntry
|
|||||||
public function setMiddleware($middleware)
|
public function setMiddleware($middleware)
|
||||||
{
|
{
|
||||||
$this->middlewares[] = $middleware;
|
$this->middlewares[] = $middleware;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setMiddlewares(array $middlewares)
|
public function setMiddlewares(array $middlewares)
|
||||||
{
|
{
|
||||||
$this->middlewares = $middlewares;
|
$this->middlewares = $middlewares;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -299,6 +331,7 @@ abstract class RouterEntry
|
|||||||
public function setNamespace($namespace)
|
public function setNamespace($namespace)
|
||||||
{
|
{
|
||||||
$this->namespace = $namespace;
|
$this->namespace = $namespace;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -306,12 +339,15 @@ abstract class RouterEntry
|
|||||||
* @param string $namespace
|
* @param string $namespace
|
||||||
* @return static $this
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setDefaultNamespace($namespace) {
|
public function setDefaultNamespace($namespace)
|
||||||
|
{
|
||||||
$this->defaultNamespace = $namespace;
|
$this->defaultNamespace = $namespace;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDefaultNamespace() {
|
public function getDefaultNamespace()
|
||||||
|
{
|
||||||
return $this->defaultNamespace;
|
return $this->defaultNamespace;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -346,6 +382,7 @@ abstract class RouterEntry
|
|||||||
public function setParameters($parameters)
|
public function setParameters($parameters)
|
||||||
{
|
{
|
||||||
$this->parameters = $parameters;
|
$this->parameters = $parameters;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -355,21 +392,23 @@ abstract class RouterEntry
|
|||||||
* @param array $options
|
* @param array $options
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function where(array $options)
|
public function setWhere(array $options)
|
||||||
{
|
{
|
||||||
$this->where = $options;
|
$this->where = $options;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add regular expression match for url
|
* Add regular expression match for the entire route.
|
||||||
*
|
*
|
||||||
* @param string $regex
|
* @param string $regex
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function match($regex)
|
public function setMatch($regex)
|
||||||
{
|
{
|
||||||
$this->regex = $regex;
|
$this->regex = $regex;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -380,7 +419,7 @@ abstract class RouterEntry
|
|||||||
*/
|
*/
|
||||||
public function toArray()
|
public function toArray()
|
||||||
{
|
{
|
||||||
$values = array();
|
$values = [];
|
||||||
|
|
||||||
if ($this->namespace !== null) {
|
if ($this->namespace !== null) {
|
||||||
$values['namespace'] = $this->namespace;
|
$values['namespace'] = $this->namespace;
|
||||||
@@ -413,25 +452,25 @@ abstract class RouterEntry
|
|||||||
*/
|
*/
|
||||||
public function merge(array $values)
|
public function merge(array $values)
|
||||||
{
|
{
|
||||||
if (isset($values['namespace'])) {
|
if (isset($values['namespace']) && $this->namespace === null) {
|
||||||
$this->setNamespace($values['namespace']);
|
$this->setNamespace($values['namespace']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Push middleware if multiple
|
// Push middleware if multiple
|
||||||
if (isset($values['middleware'])) {
|
if (isset($values['middleware'])) {
|
||||||
$this->middlewares = array_merge((array)$values['middleware'], $this->middlewares);
|
$this->setMiddlewares(array_merge((array)$values['middleware'], $this->middlewares));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($values['method'])) {
|
if (isset($values['method'])) {
|
||||||
$this->setRequestMethods((array)$values['method']);
|
$this->setRequestMethods(array_merge($this->requestMethods, (array)$values['method']));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($values['where'])) {
|
if (isset($values['where'])) {
|
||||||
$this->where($values['where']);
|
$this->setWhere(array_merge($this->where, (array)$values['where']));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($values['parameters'])) {
|
if (isset($values['parameters'])) {
|
||||||
$this->setParameters($values['parameters']);
|
$this->setParameters(array_merge($this->parameters, (array)$values['parameters']));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ use Pecee\Http\Request;
|
|||||||
class RouterGroup extends RouterEntry
|
class RouterGroup extends RouterEntry
|
||||||
{
|
{
|
||||||
protected $prefix;
|
protected $prefix;
|
||||||
protected $domains = array();
|
protected $domains = [];
|
||||||
protected $exceptionHandlers = array();
|
protected $exceptionHandlers = [];
|
||||||
|
|
||||||
public function matchDomain(Request $request)
|
public function matchDomain(Request $request)
|
||||||
{
|
{
|
||||||
@@ -18,6 +18,7 @@ class RouterGroup extends RouterEntry
|
|||||||
|
|
||||||
if ($parameters !== null) {
|
if ($parameters !== null) {
|
||||||
$this->parameters = $parameters;
|
$this->parameters = $parameters;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,6 +42,7 @@ class RouterGroup extends RouterEntry
|
|||||||
public function setExceptionHandlers(array $handlers)
|
public function setExceptionHandlers(array $handlers)
|
||||||
{
|
{
|
||||||
$this->exceptionHandlers = $handlers;
|
$this->exceptionHandlers = $handlers;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,6 +59,7 @@ class RouterGroup extends RouterEntry
|
|||||||
public function setDomains(array $domains)
|
public function setDomains(array $domains)
|
||||||
{
|
{
|
||||||
$this->domains = $domains;
|
$this->domains = $domains;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,6 +70,7 @@ class RouterGroup extends RouterEntry
|
|||||||
public function setPrefix($prefix)
|
public function setPrefix($prefix)
|
||||||
{
|
{
|
||||||
$this->prefix = '/' . trim($prefix, '/');
|
$this->prefix = '/' . trim($prefix, '/');
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,7 +91,7 @@ class RouterGroup extends RouterEntry
|
|||||||
public function merge(array $values)
|
public function merge(array $values)
|
||||||
{
|
{
|
||||||
if (isset($values['prefix'])) {
|
if (isset($values['prefix'])) {
|
||||||
$this->setPrefix($values['prefix']);
|
$this->setPrefix($values['prefix'] . $this->prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($values['exceptionHandler'])) {
|
if (isset($values['exceptionHandler'])) {
|
||||||
@@ -103,4 +107,20 @@ class RouterGroup extends RouterEntry
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Export route settings to array so they can be merged with another route.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function toArray()
|
||||||
|
{
|
||||||
|
$values = [];
|
||||||
|
|
||||||
|
if ($this->prefix !== null) {
|
||||||
|
$values['prefix'] = $this->getPrefix();
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_merge($values, parent::toArray());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Pecee\SimpleRouter;
|
namespace Pecee\SimpleRouter;
|
||||||
|
|
||||||
use Pecee\Exception\RouterException;
|
|
||||||
use Pecee\Http\Request;
|
use Pecee\Http\Request;
|
||||||
|
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||||
|
|
||||||
class RouterResource extends LoadableRoute implements IControllerRoute
|
class RouterResource extends LoadableRoute implements IControllerRoute
|
||||||
{
|
{
|
||||||
@@ -27,7 +27,7 @@ class RouterResource extends LoadableRoute implements IControllerRoute
|
|||||||
$method = strtolower($controller[1]);
|
$method = strtolower($controller[1]);
|
||||||
|
|
||||||
if (!method_exists($class, $method)) {
|
if (!method_exists($class, $method)) {
|
||||||
throw new RouterException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
|
throw new NotFoundHttpException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
call_user_func_array([$class, $method], $this->getParameters());
|
call_user_func_array([$class, $method], $this->getParameters());
|
||||||
|
|||||||
@@ -18,11 +18,13 @@ class RouterRoute extends LoadableRoute
|
|||||||
|
|
||||||
// Match on custom defined regular expression
|
// Match on custom defined regular expression
|
||||||
if ($this->regex !== null) {
|
if ($this->regex !== null) {
|
||||||
$parameters = array();
|
$parameters = [];
|
||||||
if (preg_match('/(' . $this->regex . ')/is', $request->getHost() . $url, $parameters)) {
|
if (preg_match('/(' . $this->regex . ')/is', $request->getHost() . $url, $parameters)) {
|
||||||
$this->parameters = (array)$parameters[0];
|
$this->parameters = (array)$parameters[0];
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,6 +35,7 @@ class RouterRoute extends LoadableRoute
|
|||||||
|
|
||||||
if ($parameters !== null) {
|
if ($parameters !== null) {
|
||||||
$this->parameters = array_merge($this->parameters, $parameters);
|
$this->parameters = array_merge($this->parameters, $parameters);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,9 @@
|
|||||||
*/
|
*/
|
||||||
namespace Pecee\SimpleRouter;
|
namespace Pecee\SimpleRouter;
|
||||||
|
|
||||||
use Pecee\Exception\RouterException;
|
|
||||||
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
||||||
|
use Pecee\SimpleRouter\Exceptions\HttpException;
|
||||||
|
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||||
|
|
||||||
class SimpleRouter
|
class SimpleRouter
|
||||||
{
|
{
|
||||||
@@ -17,7 +18,8 @@ class SimpleRouter
|
|||||||
/**
|
/**
|
||||||
* Start/route request
|
* Start/route request
|
||||||
*
|
*
|
||||||
* @throws \Pecee\Exception\RouterException
|
* @throws HttpException
|
||||||
|
* @throws NotFoundHttpException
|
||||||
*/
|
*/
|
||||||
public static function start()
|
public static function start()
|
||||||
{
|
{
|
||||||
@@ -138,17 +140,17 @@ class SimpleRouter
|
|||||||
*
|
*
|
||||||
* @param array $settings
|
* @param array $settings
|
||||||
* @param \Closure $callback
|
* @param \Closure $callback
|
||||||
* @throws RouterException
|
* @throws \InvalidArgumentException
|
||||||
* @return RouterGroup
|
* @return RouterGroup
|
||||||
*/
|
*/
|
||||||
public static function group(array $settings = array(), \Closure $callback)
|
public static function group(array $settings = [], \Closure $callback)
|
||||||
{
|
{
|
||||||
$group = new RouterGroup();
|
$group = new RouterGroup();
|
||||||
$group->setCallback($callback);
|
$group->setCallback($callback);
|
||||||
$group->merge($settings);
|
$group->merge($settings);
|
||||||
|
|
||||||
if (is_callable($callback) === false) {
|
if (is_callable($callback) === false) {
|
||||||
throw new RouterException('Invalid callback provided. Only functions or methods supported');
|
throw new \InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
|
||||||
}
|
}
|
||||||
|
|
||||||
static::router()->addRoute($group);
|
static::router()->addRoute($group);
|
||||||
@@ -277,16 +279,50 @@ class SimpleRouter
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get url by controller or alias.
|
* Get url for a route by using either name/alias, class or method name.
|
||||||
*
|
*
|
||||||
* @param string $controller
|
* The name parameter supports the following values:
|
||||||
* @param array|null $parameters
|
* - 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.
|
||||||
|
*
|
||||||
|
* This method is an alias for SimpleRouter::getUrl().
|
||||||
|
*
|
||||||
|
* @see SimpleRouter::getUrl()
|
||||||
|
* @param string|null $name
|
||||||
|
* @param string|array|null $parameters
|
||||||
* @param array|null $getParams
|
* @param array|null $getParams
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function getRoute($controller = null, $parameters = null, $getParams = null)
|
public static function getRoute($name = null, $parameters = null, array $getParams = [])
|
||||||
{
|
{
|
||||||
return static::router()->getRoute($controller, $parameters, $getParams);
|
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 = [])
|
||||||
|
{
|
||||||
|
return static::router()->getUrl($name, $parameters, $getParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
class ExceptionHandler implements \Pecee\Handler\IExceptionHandler
|
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\RouterEntry &$route = null, \Exception $error)
|
||||||
{
|
{
|
||||||
|
|||||||
+12
-3
@@ -79,13 +79,22 @@ class GroupTest extends PHPUnit_Framework_TestCase
|
|||||||
SimpleRouter::request()->setUri('/my/fancy/url/1');
|
SimpleRouter::request()->setUri('/my/fancy/url/1');
|
||||||
SimpleRouter::request()->setMethod('get');
|
SimpleRouter::request()->setMethod('get');
|
||||||
|
|
||||||
|
// Test array name
|
||||||
SimpleRouter::get('/my/fancy/url/1', 'DummyController@start', ['as' => 'fancy1']);
|
SimpleRouter::get('/my/fancy/url/1', 'DummyController@start', ['as' => 'fancy1']);
|
||||||
SimpleRouter::get('/my/fancy/url/2', 'DummyController@start')->setAlias('fancy2');
|
|
||||||
|
// 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();
|
SimpleRouter::start();
|
||||||
|
|
||||||
$this->assertTrue((SimpleRouter::getRoute('fancy1') === '/my/fancy/url/1/'));
|
$this->assertEquals('/my/fancy/url/1/', SimpleRouter::getRoute('fancy1'));
|
||||||
$this->assertTrue((SimpleRouter::getRoute('fancy2') === '/my/fancy/url/2/'));
|
$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'));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ require_once 'Dummy/DummyController.php';
|
|||||||
require_once 'Dummy/Handler/ExceptionHandler.php';
|
require_once 'Dummy/Handler/ExceptionHandler.php';
|
||||||
|
|
||||||
use Pecee\SimpleRouter\SimpleRouter as SimpleRouter;
|
use Pecee\SimpleRouter\SimpleRouter as SimpleRouter;
|
||||||
|
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException as NotFoundHttpException;
|
||||||
|
|
||||||
class RouterRouteTest extends PHPUnit_Framework_TestCase
|
class RouterRouteTest extends PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
@@ -25,7 +26,7 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase
|
|||||||
try {
|
try {
|
||||||
SimpleRouter::start();
|
SimpleRouter::start();
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$found = ($e instanceof \Pecee\Exception\RouterException && $e->getCode() == 404);
|
$found = ($e instanceof NotFoundHttpException && $e->getCode() == 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->assertTrue($found);
|
$this->assertTrue($found);
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ class RouterUrlTest extends PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
protected $result = false;
|
protected $result = false;
|
||||||
|
|
||||||
protected function getUrl($controller = null, $parameters = null, $getParams = null) {
|
protected function getUrl($name = null, $parameters = null, array $getParams = []) {
|
||||||
return SimpleRouter::getRoute($controller, $parameters, $getParams);
|
return SimpleRouter::getRoute($name, $parameters, $getParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUrls()
|
public function testUrls()
|
||||||
@@ -75,8 +75,8 @@ class RouterUrlTest extends PHPUnit_Framework_TestCase
|
|||||||
// Should match /funny/man/
|
// Should match /funny/man/
|
||||||
$this->assertEquals($this->getUrl('/funny/man'), '/funny/man/');
|
$this->assertEquals($this->getUrl('/funny/man'), '/funny/man/');
|
||||||
|
|
||||||
// Should match /?jackdaniels=true
|
// Should match /?jackdaniels=true&cola=yeah
|
||||||
$this->assertEquals($this->getUrl('home', null, ['jackdaniels' => 'true']), '/?jackdaniels=true');
|
$this->assertEquals($this->getUrl('home', null, ['jackdaniels' => 'true', 'cola' => 'yeah']), '/?jackdaniels=true&cola=yeah');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user