mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 08:47:52 +00:00
Updated documentation and added demo-project.
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace Demo\Controllers;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
|
||||
class ApiController {
|
||||
|
||||
public function index() {
|
||||
|
||||
// The variable authenticated is set to true in the ApiVerification middleware class.
|
||||
|
||||
$request = Request::getInstance();
|
||||
|
||||
header('content-type: application/json');
|
||||
|
||||
echo json_encode([
|
||||
'authenticated' => $request->authenticated
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace Demo\Controllers;
|
||||
|
||||
class DefaultController {
|
||||
|
||||
public function index() {
|
||||
|
||||
// implement
|
||||
echo 'DefaultController -> index';
|
||||
|
||||
}
|
||||
|
||||
public function contact() {
|
||||
|
||||
echo 'DefaultController -> contact';
|
||||
|
||||
}
|
||||
|
||||
public function companies($id = null) {
|
||||
|
||||
echo 'DefaultController -> companies -> id: ' . $id;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
namespace Demo\Handlers;
|
||||
|
||||
use Pecee\Handler\IExceptionHandler;
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\RouterEntry;
|
||||
|
||||
class CustomExceptionHandler implements IExceptionHandler {
|
||||
|
||||
public function handleError( Request $request, RouterEntry $router = null, \Exception $error) {
|
||||
|
||||
// Return json errors if we encounter an error on /api.
|
||||
if(stripos($request->getUri(), '/api') !== false) {
|
||||
header('content-type: application/json');
|
||||
echo json_encode([
|
||||
'error' => $error->getMessage(),
|
||||
'code' => $error->getCode()
|
||||
]);
|
||||
die();
|
||||
}
|
||||
|
||||
// else we just throw the error
|
||||
if($error->getCode() == 404) {
|
||||
die(sprintf('An error occurred (%s):<br/>%s', $error->getCode(), $error->getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace Demo\Middlewares;
|
||||
|
||||
use Pecee\Http\Middleware\IMiddleware;
|
||||
use Pecee\Http\Request;
|
||||
|
||||
class ApiVerification implements IMiddleware {
|
||||
|
||||
public function handle(Request $request) {
|
||||
|
||||
// Do authentication
|
||||
$request->authenticated = true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace Demo\Middlewares;
|
||||
|
||||
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
||||
|
||||
class CsrfVerifier extends BaseCsrfVerifier {
|
||||
|
||||
/**
|
||||
* CSRF validation will be ignored on the following urls.
|
||||
*/
|
||||
protected $except = ['/api/*'];
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Custom router which handles default middlewares, default exceptions and things
|
||||
* that should be happen before and after the router is initialised.
|
||||
*/
|
||||
|
||||
namespace Demo;
|
||||
|
||||
use Pecee\Exception\RouterException;
|
||||
use Pecee\Handler\IExceptionHandler;
|
||||
use Pecee\Http\Middleware\IMiddleware;
|
||||
use Pecee\SimpleRouter\RouterBase;
|
||||
use Pecee\SimpleRouter\SimpleRouter;
|
||||
|
||||
class Router extends SimpleRouter {
|
||||
|
||||
protected static $defaultExceptionHandler;
|
||||
protected static $defaultMiddlewares = array();
|
||||
|
||||
public static function start($defaultNamespace = null) {
|
||||
|
||||
// change this to whatever makes sense in your project
|
||||
require_once 'routes.php';
|
||||
|
||||
// Handle exceptions
|
||||
try {
|
||||
|
||||
if(count(static::$defaultMiddlewares)) {
|
||||
/* @var $middleware \Pecee\Http\Middleware\IMiddleware */
|
||||
foreach(static::$defaultMiddlewares as $middleware) {
|
||||
$middleware = new $middleware();
|
||||
if(!($middleware instanceof IMiddleware)) {
|
||||
throw new RouterException('Middleware must be implement the IMiddleware interface.');
|
||||
}
|
||||
$middleware->handle(RouterBase::getInstance()->getRequest());
|
||||
}
|
||||
}
|
||||
|
||||
// Set default namespace
|
||||
$defaultNamespace = '\\Demo\\Controllers';
|
||||
|
||||
parent::start($defaultNamespace);
|
||||
} catch(\Exception $e) {
|
||||
|
||||
$route = RouterBase::getInstance()->getLoadedRoute();
|
||||
|
||||
// Otherwise use the fallback default exceptions handler
|
||||
if(static::$defaultExceptionHandler !== null) {
|
||||
static::loadExceptionHandler(static::$defaultExceptionHandler, $route, $e);
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected static function loadExceptionHandler($class, $route, $e) {
|
||||
$class = new $class();
|
||||
|
||||
if(!($class instanceof IExceptionHandler)) {
|
||||
throw new \ErrorException('Exception handler must be an instance of \Pecee\Handler\IExceptionHandler');
|
||||
}
|
||||
|
||||
$class->handleError(RouterBase::getInstance()->getRequest(), $route, $e);
|
||||
}
|
||||
|
||||
public static function defaultExceptionHandler($handler) {
|
||||
static::$defaultExceptionHandler = $handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add default middleware that will be loaded before any route
|
||||
* @param string|array $middlewares
|
||||
*/
|
||||
public static function defaultMiddleware($middlewares) {
|
||||
if(is_array($middlewares)) {
|
||||
static::$defaultMiddlewares = $middlewares;
|
||||
} else {
|
||||
static::$defaultMiddlewares[] = $middlewares;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
/**
|
||||
* This file contains all the routes for the project
|
||||
*/
|
||||
|
||||
use Demo\Router;
|
||||
|
||||
Router::csrfVerifier(new \Demo\Middlewares\CsrfVerifier());
|
||||
Router::defaultExceptionHandler('\Demo\Handlers\CustomExceptionHandler');
|
||||
|
||||
Router::get('/', 'DefaultController@index')->setAlias('home');
|
||||
Router::get('/contact', 'DefaultController@contact')->setAlias('contact');
|
||||
Router::basic('/companies', 'DefaultController@companies')->setAlias('companies');
|
||||
Router::basic('/companies/{id}', 'DefaultController@companies')->setAlias('companies');
|
||||
|
||||
// Api
|
||||
Router::group(['prefix' => '/api', 'middleware' => 'Demo\Middlewares\ApiVerification'], function() {
|
||||
Router::resource('/demo', 'ApiController');
|
||||
});
|
||||
Reference in New Issue
Block a user