[TASK] Updated

This commit is contained in:
Simon Sessingø
2015-09-21 19:40:14 +02:00
parent 266ebdf7d7
commit 85470bbbd7
5 changed files with 184 additions and 73 deletions
+78 -5
View File
@@ -41,7 +41,7 @@ This router is heavily inspired by the Laravel 5.* router, so anything you find
### Basic example
```php
using \Pecee\SimpleRouter;
use Pecee\SimpleRouter\SimpleRouter;
/*
* This route will match the url /v1/services/answers/1/
@@ -63,13 +63,14 @@ SimpleRouter::group(['prefix' => 'v1', 'middleware' => '\MyWebsite\Middleware\So
### Doing it the object oriented (hardcore) way
The ```Router``` class is just a simple helper class that knows how to communicate with the ```SimpleRouter``` class. If you are up for a challenge, want the full control or simply just want to create your own ```Router``` helper class, this example is for you.
The ```SimpleRouter``` class referenced in the previous example, is just a simple helper class that knows how to communicate with the ```RouterBase``` class.
If you are up for a challenge, want the full control or simply just want to create your own ```Router``` helper class, this example is for you.
```php
use \Pecee\SimpleRouter;
use \Pecee\SimpleRouter\Router;
use \Pecee\SimpleRouter\RouterBase;
use \Pecee\SimpleRouter\RouterRoute;
$router = Router::getInstance();
$router = RouterBase::getInstance();
$route = new RouterRoute('/answer/1', function() {
die('this callback will match /answer/1');
@@ -83,6 +84,78 @@ $route->setPrefix('v1');
$router->addRoute($route);
```
This is a simple example of an integration into a framework.
The framework has it's own ```Router``` class which inherits from the ```SimpleRouter``` class. This allows the framework to add custom functionality.
```php
namespace MyProject;
use Pecee\Handler\ExceptionHandler;
use Pecee\SimpleRouter\SimpleRouter;
class Router extends SimpleRouter {
protected static $exceptionHandlers = array();
public static function start() {
Debug::getInstance()->add('Router initialised.');
// Load routes.php
$file = $_ENV['basePath'] . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'routes.php';
if(file_exists($file)) {
require_once $file;
}
// Init locale settings
Locale::getInstance();
// Set default namespace
$defaultNamespace = '\\'.Registry::getInstance()->get('AppName') . '\\Controller';
// Handle exceptions
try {
parent::start($defaultNamespace); // TODO: Change the autogenerated stub
} catch(\Exception $e) {
/* @var $handler ExceptionHandler */
foreach(self::$exceptionHandlers as $handler) {
$class = new $handler();
$class->handleError($e);
}
throw $e;
}
}
public static function addExceptionHandler($handler) {
self::$exceptionHandlers[] = $handler;
}
}
```
This is a basic example of a ```helper.php``` for generating urls.
```php
use Pecee\SimpleRouter\RouterBase;
function url($controller, $parameters = null, $getParams = null) {
RouterBase::getInstance()->getRoute($controller, $parameters, $getParams);
}
```
In ```routes.php``` we have added this route:
```Router::get('/item/{id}', 'myController@show');```
In the template we call:
```url('myController@show', ['id' => 22], ['category' => 'shoes']);```
Result url is:
```/item/22?category=shoes```
## Documentation
While I work on a better documentation, please refer to the Laravel 5 routing documentation here: