mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 16:57:53 +00:00
Compare commits
52 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4975f24fee | |||
| 2952f6a3b6 | |||
| da7348ea82 | |||
| 16e326ad9f | |||
| 0002b45d18 | |||
| d9b2328e82 | |||
| 4a03005c68 | |||
| 52034411cf | |||
| 4c8ed5bb3d | |||
| 9fed6ffb3f | |||
| 15da599e82 | |||
| 9274acb591 | |||
| 5c7759ab72 | |||
| c7b8593185 | |||
| fb478f475c | |||
| 1142d9d4ce | |||
| 5d5c96e802 | |||
| bfdaf8ac52 | |||
| 71dc6e172f | |||
| 6ee172927f | |||
| bb5e629199 | |||
| 0cc0a59fd5 | |||
| 498fd6b07d | |||
| 96ab22a4f8 | |||
| 7f528c133b | |||
| 5a50190293 | |||
| 355ef01d63 | |||
| d3162b5a2b | |||
| 810b80487d | |||
| 18a9df56ca | |||
| 6e14ded03f | |||
| 899081f8d8 | |||
| e7b9206bc9 | |||
| cd6e800984 | |||
| 11bd5a7d11 | |||
| be32796b01 | |||
| 8b3d71a328 | |||
| 1fae638aaf | |||
| 37c8bc9f32 | |||
| 75029b330a | |||
| fd5d893040 | |||
| c1512740af | |||
| 212ae133de | |||
| ae58231fa1 | |||
| 358b25d4f1 | |||
| 3d45851d9b | |||
| ee5c2207f8 | |||
| b1ca3fc9ef | |||
| 253c0c70d4 | |||
| 53ba2d7ac5 | |||
| 315fe05769 | |||
| a57113309a |
@@ -208,12 +208,15 @@ class Router extends SimpleRouter {
|
||||
**This is a basic example of a helper function for generating urls.**
|
||||
|
||||
```php
|
||||
use Pecee\SimpleRouter\SimpleRouter;
|
||||
|
||||
use Pecee\SimpleRouter\RouterBase;
|
||||
function url($controller, $parameters = null, $getParams = null) {
|
||||
SimpleRouter::getRoute($controller, $parameters, $getParams);
|
||||
RouterBase::getInstance()->getRoute($controller, $parameters, $getParams);
|
||||
}
|
||||
```
|
||||
|
||||
**This is a basic example for getting the current csrf token**
|
||||
|
||||
```php
|
||||
/**
|
||||
* Get current csrf-token
|
||||
* @return null|string
|
||||
@@ -222,22 +225,6 @@ function csrf_token() {
|
||||
$token = new \Pecee\CsrfToken();
|
||||
return $token->getToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get request object
|
||||
* @return \Pecee\Http\Request
|
||||
*/
|
||||
function request() {
|
||||
return SimpleRouter::request();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get response object
|
||||
* @return \Pecee\Http\Response
|
||||
*/
|
||||
function response() {
|
||||
return SimpleRouter::response();
|
||||
}
|
||||
```
|
||||
|
||||
## Getting urls
|
||||
@@ -340,14 +327,12 @@ SimpleRouter::get('/article/view/{id}', 'ControllerArticle@view');
|
||||
## Easily overwrite route about to be loaded
|
||||
Sometimes it can be useful to manipulate the route that's about to be loaded, for instance if a user is not authenticated or if an error occurred within your Middleware that requires
|
||||
some other route to be initialised. Simple PHP Router allows you to easily change the route about to be executed. All information about the current route is stored in
|
||||
the ```\Pecee\SimpleRouter\Http\Request``` object. All information about the current route is as a ```\Pecee\SimpleRouter\Http\Request``` object which can always be obtained on
|
||||
the `RouterBase` instance. For easy access you can use the shortcut method `\Pecee\SimpleRouter\SimpleRouter::request()`.
|
||||
the ```\Pecee\SimpleRouter\Http\Request``` object.
|
||||
|
||||
**Note:** Please note that it's only possible to change the route BEFORE any route has initially been loaded, so doing this in your custom ExceptionHandler or Middleware is highly recommended.
|
||||
|
||||
```php
|
||||
use Pecee\SimpleRouter;
|
||||
$route = SimpleRouter::request()->getLoadedRoute();
|
||||
$route = Request::getInstance()->getLoadedRoute();
|
||||
|
||||
$route->setCallback('Example\MyCustomClass@hello');
|
||||
|
||||
@@ -363,28 +348,28 @@ We've added the `Input` class to easy access parameters from your Controller-cla
|
||||
|
||||
**Return single parameter value (matches both GET, POST, FILE):**
|
||||
```php
|
||||
$value = SimpleRouter::request()->getInput()->get('name');
|
||||
$value = Request::getInstance()->getInput()->get('name');
|
||||
```
|
||||
|
||||
**Return parameter object (matches both GET, POST, FILE):**
|
||||
```php
|
||||
$object = SimpleRouter::request()->getInput()->getObject('name');
|
||||
$object = Request::getInstance()->getInput()->getObject('name');
|
||||
```
|
||||
|
||||
**Return specific GET parameter (where name is the name of your parameter):**
|
||||
```php
|
||||
$object = SimpleRouter::request()->getInput()->get->name;
|
||||
$object = SimpleRouter::request()->getInput()->post->name;
|
||||
$object = SimpleRouter::request()->getInput()->file->name;
|
||||
$object = Request::getInstance()->getInput()->get->name;
|
||||
$object = Request::getInstance()->getInput()->post->name;
|
||||
$object = Request::getInstance()->getInput()->file->name;
|
||||
```
|
||||
|
||||
**Return all parameters:**
|
||||
```php
|
||||
// Get all
|
||||
$objects = SimpleRouter::request()->getInput()->all();
|
||||
$objects = Request::getInstance()->getInput()->all();
|
||||
|
||||
// Only match certain keys
|
||||
$objects = SimpleRouter::request()->getInput()->all([
|
||||
$objects = Request::getInstance()->getInput()->all([
|
||||
'company_name',
|
||||
'user_id'
|
||||
]);
|
||||
@@ -415,7 +400,7 @@ Example:
|
||||
* @return \Pecee\Http\Input\Input
|
||||
*/
|
||||
function input() {
|
||||
return SimpleRouter::request()->getInput();
|
||||
return \Pecee\Http\Request::getInstance()->getInput();
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
namespace Demo\Controllers;
|
||||
|
||||
use Pecee\SimpleRouter\SimpleRouter;
|
||||
use Pecee\Http\Request;
|
||||
|
||||
class ApiController {
|
||||
|
||||
@@ -9,7 +9,7 @@ class ApiController {
|
||||
|
||||
// The variable authenticated is set to true in the ApiVerification middleware class.
|
||||
|
||||
$request = SimpleRouter::request();
|
||||
$request = Request::getInstance();
|
||||
|
||||
header('content-type: application/json');
|
||||
|
||||
|
||||
@@ -13,13 +13,11 @@ class Router extends SimpleRouter {
|
||||
|
||||
public static function start($defaultNamespace = null) {
|
||||
|
||||
// Load our helpers
|
||||
require_once 'helpers.php';
|
||||
|
||||
// Load our custom routes
|
||||
// change this to whatever makes sense in your project
|
||||
require_once 'routes.php';
|
||||
|
||||
// Do initial stuff
|
||||
|
||||
parent::start('\\Demo\\Controllers');
|
||||
|
||||
}
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
<?php
|
||||
use \Pecee\SimpleRouter\SimpleRouter;
|
||||
|
||||
function url($controller, $parameters = null, $getParams = null) {
|
||||
SimpleRouter::getRoute($controller, $parameters, $getParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current csrf-token
|
||||
* @return null|string
|
||||
*/
|
||||
function csrf_token() {
|
||||
$token = new \Pecee\CsrfToken();
|
||||
return $token->getToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get request object
|
||||
* @return \Pecee\Http\Request
|
||||
*/
|
||||
function request() {
|
||||
return SimpleRouter::request();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get response object
|
||||
* @return \Pecee\Http\Response
|
||||
*/
|
||||
function response() {
|
||||
return SimpleRouter::response();
|
||||
}
|
||||
@@ -38,17 +38,31 @@ class Input {
|
||||
* @return array
|
||||
*/
|
||||
public function all(array $filter = null) {
|
||||
$output = $this->get->getData();
|
||||
$output = array_merge($output, $this->post->getData());
|
||||
|
||||
if($filter !== null) {
|
||||
$tmp = array();
|
||||
foreach($output as $key => $val) {
|
||||
if(in_array($key, $filter)) {
|
||||
$tmp[$key] = $val;
|
||||
$output = $_POST;
|
||||
|
||||
if($this->request->getMethod() === 'post') {
|
||||
|
||||
$contents = file_get_contents('php://input');
|
||||
|
||||
if (stripos(trim($contents), '{') === 0) {
|
||||
$output = json_decode($contents, true);
|
||||
if($output === false) {
|
||||
$output = array();
|
||||
}
|
||||
}
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
$output = array_merge($_GET, $output);
|
||||
|
||||
if($filter !== null) {
|
||||
$output = array_filter($output, function ($key) use ($filter) {
|
||||
if (in_array($key, $filter)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}, ARRAY_FILTER_USE_KEY);
|
||||
}
|
||||
|
||||
return $output;
|
||||
@@ -93,7 +107,7 @@ class Input {
|
||||
|
||||
if($item !== null) {
|
||||
|
||||
if(is_array($item) || $item instanceof InputFile) {
|
||||
if($item instanceof InputCollection || $item instanceof InputFile) {
|
||||
return $item;
|
||||
}
|
||||
|
||||
@@ -117,10 +131,10 @@ class Input {
|
||||
continue;
|
||||
}
|
||||
|
||||
$output = array();
|
||||
$output = new InputCollection();
|
||||
|
||||
foreach($get as $k => $g) {
|
||||
$output[$k] = new InputItem($k, $g);
|
||||
$output->{$k} = new InputItem($k, $g);
|
||||
}
|
||||
|
||||
$this->get->{$key} = $output;
|
||||
@@ -131,12 +145,10 @@ class Input {
|
||||
public function setPost() {
|
||||
$this->post = new InputCollection();
|
||||
|
||||
$postVars = array();
|
||||
$postVars = $_POST;
|
||||
|
||||
if(isset($_SERVER['REQUEST_METHOD']) && in_array($_SERVER['REQUEST_METHOD'], ['PUT', 'PATCH', 'DELETE'])) {
|
||||
if(in_array($this->request->getMethod(), ['put', 'patch', 'delete'])) {
|
||||
parse_str(file_get_contents('php://input'), $postVars);
|
||||
} else {
|
||||
$postVars = $_POST;
|
||||
}
|
||||
|
||||
if(count($postVars)) {
|
||||
@@ -147,10 +159,10 @@ class Input {
|
||||
continue;
|
||||
}
|
||||
|
||||
$output = array();
|
||||
$output = new InputCollection();
|
||||
|
||||
foreach($post as $k=>$p) {
|
||||
$output[$k] = new InputItem($k, $p);
|
||||
foreach($post as $k => $p) {
|
||||
$output->{$k} = new InputItem($k, $p);
|
||||
}
|
||||
|
||||
$this->post->{strtolower($key)} = $output;
|
||||
@@ -178,7 +190,7 @@ class Input {
|
||||
continue;
|
||||
}
|
||||
|
||||
$output = array();
|
||||
$output = new InputCollection();
|
||||
|
||||
foreach($value['name'] as $k=>$val) {
|
||||
// Strip empty values
|
||||
@@ -189,7 +201,7 @@ class Input {
|
||||
$file->setType($value['type'][$k]);
|
||||
$file->setTmpName($value['tmp_name'][$k]);
|
||||
$file->setError($value['error'][$k]);
|
||||
$output[$k] = $file;
|
||||
$output->{$k} = $file;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,8 +5,21 @@ use Pecee\Http\Input\Input;
|
||||
|
||||
class Request {
|
||||
|
||||
protected static $instance;
|
||||
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* Return new instance
|
||||
* @return static
|
||||
*/
|
||||
public static function getInstance() {
|
||||
if(self::$instance === null) {
|
||||
self::$instance = new static();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function __construct() {
|
||||
$this->data = array();
|
||||
$this->host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : array();
|
||||
|
||||
+11
-11
@@ -4,12 +4,6 @@ namespace Pecee\Http;
|
||||
|
||||
class Response {
|
||||
|
||||
protected $request;
|
||||
|
||||
public function __construct(Request &$request) {
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the http status code
|
||||
*
|
||||
@@ -37,7 +31,7 @@ class Response {
|
||||
}
|
||||
|
||||
public function refresh() {
|
||||
$this->redirect($this->request->getUri());
|
||||
$this->redirect(Request::getInstance()->getUri());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,13 +69,19 @@ class Response {
|
||||
}
|
||||
|
||||
/**
|
||||
* Json encode array
|
||||
* @param array $value
|
||||
* Json encode
|
||||
* @param array|\JsonSerializable $value
|
||||
* @throws \InvalidArgumentException;
|
||||
*/
|
||||
public function json(array $value) {
|
||||
public function json($value) {
|
||||
|
||||
if(($value instanceof \JsonSerializable) === false && is_array($value) === false) {
|
||||
throw new \InvalidArgumentException('Invalid type for parameter "value". Must be of type array or object implementing the \JsonSerializable interface.');
|
||||
}
|
||||
|
||||
$this->header('Content-type: application/json');
|
||||
echo json_encode($value);
|
||||
die();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,14 +5,12 @@ use Pecee\Exception\RouterException;
|
||||
use Pecee\Handler\IExceptionHandler;
|
||||
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\Http\Response;
|
||||
|
||||
class RouterBase {
|
||||
|
||||
protected static $instance;
|
||||
|
||||
protected $request;
|
||||
protected $response;
|
||||
protected $currentRoute;
|
||||
protected $routes;
|
||||
protected $processedRoutes;
|
||||
@@ -23,13 +21,10 @@ class RouterBase {
|
||||
protected $baseCsrfVerifier;
|
||||
protected $exceptionHandlers;
|
||||
|
||||
public function __construct() {
|
||||
$this->reset();
|
||||
}
|
||||
// TODO: clean up - cut some of the methods down to smaller pieces
|
||||
|
||||
public function reset() {
|
||||
$this->request = new Request();
|
||||
$this->response = new Response($this->request);
|
||||
public function __construct() {
|
||||
$this->request = Request::getInstance();
|
||||
$this->routes = array();
|
||||
$this->backStack = array();
|
||||
$this->controllerUrlMap = array();
|
||||
@@ -294,14 +289,6 @@ class RouterBase {
|
||||
return $this->request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get response
|
||||
* @return Response
|
||||
*/
|
||||
public function getResponse() {
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get base csrf verifier class
|
||||
* @return BaseCsrfVerifier
|
||||
@@ -354,10 +341,10 @@ class RouterBase {
|
||||
$url = $domain . '/' . trim($route->getUrl(), '/');
|
||||
|
||||
if(($route instanceof RouterController || $route instanceof RouterResource) && $method !== null) {
|
||||
if($method !== null) {
|
||||
$url .= $method;
|
||||
}
|
||||
$url .= $method;
|
||||
}
|
||||
|
||||
if($route instanceof RouterController || $route instanceof RouterResource) {
|
||||
if(count($parameters)) {
|
||||
$url .= join('/', $parameters);
|
||||
}
|
||||
@@ -432,15 +419,12 @@ class RouterBase {
|
||||
$route = $this->controllerUrlMap[$i];
|
||||
|
||||
// Check an alias exist, if the matches - use it
|
||||
if($route instanceof RouterRoute) {
|
||||
if($route instanceof RouterRoute && $route->hasAlias($controller)) {
|
||||
return $this->processUrl($route, $route->getMethod(), $parameters, $getParams);
|
||||
}
|
||||
|
||||
if($route->hasAlias($controller)) {
|
||||
return $this->processUrl($route, $route->getMethod(), $parameters, $getParams);
|
||||
}
|
||||
|
||||
if(!is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) {
|
||||
$c = $route->getCallback();
|
||||
}
|
||||
if($route instanceof RouterRoute && !is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) {
|
||||
$c = $route->getCallback();
|
||||
} else if($route instanceof RouterController || $route instanceof RouterResource) {
|
||||
$c = $route->getController();
|
||||
}
|
||||
@@ -492,10 +476,6 @@ class RouterBase {
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current router instance
|
||||
* @return static
|
||||
*/
|
||||
public static function getInstance() {
|
||||
if(static::$instance === null) {
|
||||
static::$instance = new static();
|
||||
|
||||
@@ -97,9 +97,12 @@ class RouterGroup extends RouterEntry {
|
||||
if($this->getMiddleware() !== null && isset($settings['middleware'])) {
|
||||
|
||||
if(!is_array($this->getMiddleware())) {
|
||||
$middlewares = [$this->getMiddleware(), $settings['middleware']];
|
||||
$middlewares = [
|
||||
$this->getMiddleware(),
|
||||
$settings['middleware']
|
||||
];
|
||||
} else {
|
||||
$middlewares = array_push($settings['middleware']);
|
||||
$middlewares = array_push($settings['middleware'], $this->getMiddleware());
|
||||
}
|
||||
|
||||
$settings['middleware'] = array_unique(array_reverse($middlewares));
|
||||
|
||||
@@ -13,10 +13,6 @@ use Pecee\Http\Middleware\BaseCsrfVerifier;
|
||||
|
||||
class SimpleRouter {
|
||||
|
||||
public function __construct() {
|
||||
die('test');
|
||||
}
|
||||
|
||||
/**
|
||||
* Start/route request
|
||||
* @param null $defaultNamespace
|
||||
@@ -132,12 +128,4 @@ class SimpleRouter {
|
||||
return RouterBase::getInstance()->getRoute($controller, $parameters, $getParams);
|
||||
}
|
||||
|
||||
public static function request() {
|
||||
return RouterBase::getInstance()->getRequest();
|
||||
}
|
||||
|
||||
public static function response() {
|
||||
return RouterBase::getInstance()->getResponse();
|
||||
}
|
||||
|
||||
}
|
||||
+2
-3
@@ -28,9 +28,8 @@ class GroupTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testNestedGroup() {
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/api/v1/test');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/api/v1/test');
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setMethod('get');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/api'], function() {
|
||||
\Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/v1'], function() {
|
||||
|
||||
@@ -8,9 +8,8 @@ class MiddlewareTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testMiddlewareFound() {
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/my/test/url');
|
||||
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||
\Pecee\Http\Request::getInstance()->setUri('/my/test/url');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::group(['exceptionHandler' => 'ExceptionHandler'], function() {
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start', ['middleware' => 'DummyMiddleware']);
|
||||
|
||||
+23
-30
@@ -5,12 +5,12 @@ require_once 'Dummy/DummyController.php';
|
||||
require_once 'Dummy/Handler/ExceptionHandler.php';
|
||||
|
||||
class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
|
||||
|
||||
public function testNotFound() {
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/test-param1-param2');
|
||||
|
||||
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||
\Pecee\Http\Request::getInstance()->setUri('/test-param1-param2');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::group(['exceptionHandler' => 'ExceptionHandler'], function() {
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/non-existing-path', 'DummyController@start');
|
||||
@@ -29,36 +29,33 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
||||
}
|
||||
|
||||
public function testGet() {
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/my/test/url');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/my/test/url');
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setMethod('get');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start');
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
}
|
||||
|
||||
public function testPost() {
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/my/test/url');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('post');
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/my/test/url');
|
||||
\Pecee\Http\Request::getInstance()->setMethod('post');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::post('/my/test/url', 'DummyController@start');
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
}
|
||||
|
||||
public function testPut() {
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/my/test/url');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('put');
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/my/test/url');
|
||||
\Pecee\Http\Request::getInstance()->setMethod('put');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::put('/my/test/url', 'DummyController@start');
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
}
|
||||
|
||||
public function testDelete() {
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/my/test/url');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('delete');
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/my/test/url');
|
||||
\Pecee\Http\Request::getInstance()->setMethod('delete');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::delete('/my/test/url', 'DummyController@start');
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
@@ -66,9 +63,8 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
||||
}
|
||||
|
||||
public function testMethodNotAllowed() {
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/my/test/url');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('post');
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/my/test/url');
|
||||
\Pecee\Http\Request::getInstance()->setMethod('post');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start');
|
||||
|
||||
@@ -82,9 +78,8 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testSimpleParam() {
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/test-param1');
|
||||
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||
\Pecee\Http\Request::getInstance()->setUri('/test-param1');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/test-{param1}', 'DummyController@param');
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
@@ -93,22 +88,20 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testMultiParam() {
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/test-param1-param2');
|
||||
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||
\Pecee\Http\Request::getInstance()->setUri('/test-param1-param2');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/test-{param1}-{param2}', 'DummyController@param');
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
|
||||
}
|
||||
|
||||
public function testPathParamRegex() {
|
||||
public function testPathParam() {
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
|
||||
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/test/path/123123');
|
||||
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||
\Pecee\Http\Request::getInstance()->setUri('/test/path/param1');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/test/path/{myParam}', 'DummyController@param', ['where' => ['myParam' => '([0-9]+)']]);
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/test/path/{param}', 'DummyController@param');
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user