mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 00:37:52 +00:00
[FEATURE] Added ressource controller type
This commit is contained in:
@@ -22,7 +22,7 @@ class RouterBase {
|
||||
$this->backstack = array();
|
||||
$this->controllerUrlMap = array();
|
||||
$this->requestUri = $_SERVER['REQUEST_URI'];
|
||||
$this->requestMethod = strtolower(isset($_GET['_method']) ? $_GET['_method'] : $_SERVER['REQUEST_METHOD']);
|
||||
$this->requestMethod = ($_SERVER['REQUEST_METHOD'] != 'GET') ? 'post' : 'get';
|
||||
}
|
||||
|
||||
public function addRoute(RouterEntry $route) {
|
||||
|
||||
@@ -31,11 +31,9 @@ class RouterController extends RouterEntry {
|
||||
if(count($path)) {
|
||||
|
||||
$method = (!isset($path[0]) || trim($path[0]) === '') ? self::DEFAULT_METHOD : $path[0];
|
||||
|
||||
$this->method = $method;
|
||||
|
||||
array_shift($path);
|
||||
|
||||
$this->parameters = $path;
|
||||
|
||||
// Set callback
|
||||
@@ -43,11 +41,8 @@ class RouterController extends RouterEntry {
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -181,12 +181,9 @@ abstract class RouterEntry {
|
||||
} else {
|
||||
// When the callback is a method
|
||||
$controller = explode('@', $this->getCallback());
|
||||
|
||||
$className = $this->getNamespace() . '\\' . $controller[0];
|
||||
|
||||
$class = $this->loadClass($className);
|
||||
|
||||
$method = $requestMethod . ucfirst($controller[1]);
|
||||
$method = strtolower($requestMethod) . ucfirst($controller[1]);
|
||||
|
||||
if (!method_exists($class, $method)) {
|
||||
throw new RouterException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
namespace Pecee\SimpleRouter;
|
||||
|
||||
class RouterRessource extends RouterEntry {
|
||||
|
||||
const DEFAULT_METHOD = 'index';
|
||||
|
||||
protected $url;
|
||||
protected $controller;
|
||||
protected $method;
|
||||
protected $parameters;
|
||||
protected $postMethod;
|
||||
|
||||
public function __construct($url, $controller) {
|
||||
parent::__construct();
|
||||
$this->url = $url;
|
||||
$this->controller = $controller;
|
||||
$this->parameters;
|
||||
$this->postMethod = strtoupper(isset($_POST['_method']) ? $_POST['_method'] : $_SERVER['REQUEST_METHOD']);
|
||||
}
|
||||
|
||||
public function renderRoute($requestMethod) {
|
||||
return parent::renderRoute($requestMethod);
|
||||
}
|
||||
|
||||
protected function call($method, $parameters) {
|
||||
$this->setCallback($this->controller . '@' . $method);
|
||||
$this->parameters = $parameters;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function matchRoute($requestMethod, $url) {
|
||||
$url = parse_url($url);
|
||||
$url = $url['path'];
|
||||
|
||||
if(strtolower($url) == strtolower($this->url) || stripos($url, $this->url) !== false) {
|
||||
$url = rtrim($url, '/');
|
||||
|
||||
$strippedUrl = trim(substr($url, strlen($this->url)), '/');
|
||||
$path = explode('/', $strippedUrl);
|
||||
|
||||
$args = $path;
|
||||
$action = '';
|
||||
|
||||
if(count($args)) {
|
||||
$action = $args[0];
|
||||
array_shift($args);
|
||||
}
|
||||
|
||||
if (count($path)) {
|
||||
|
||||
// Delete
|
||||
if($this->postMethod == 'DELETE' && $requestMethod == self::REQUEST_TYPE_POST) {
|
||||
return $this->call('destroy', $args);
|
||||
}
|
||||
|
||||
// Update
|
||||
if(in_array($this->postMethod, array('PUT', 'PATCH')) && $requestMethod == self::REQUEST_TYPE_POST) {
|
||||
return $this->call('update', array_merge(array($action), $args));
|
||||
}
|
||||
|
||||
// Edit
|
||||
if(isset($args[0]) && strtolower($args[0]) == 'edit' && $requestMethod == self::REQUEST_TYPE_GET) {
|
||||
return $this->call('edit', array_merge(array($action), array_slice($args, 1)));
|
||||
}
|
||||
|
||||
// Create
|
||||
if(strtolower($action) == 'create' && $this->postMethod == 'GET') {
|
||||
return $this->call('create', $args);
|
||||
}
|
||||
|
||||
// Save
|
||||
if($requestMethod == 'POST') {
|
||||
return $this->call('store', $args);
|
||||
}
|
||||
|
||||
// Show
|
||||
if($action && $requestMethod == 'GET') {
|
||||
return $this->call('show', array_merge(array($action), $args));
|
||||
}
|
||||
|
||||
// Index
|
||||
return $this->call('index', $args);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -99,8 +99,12 @@ class SimpleRouter {
|
||||
return $route;
|
||||
}
|
||||
|
||||
public static function ressource($controller, $settings = array()) {
|
||||
// not yet implemented
|
||||
public static function ressource($url, $controller) {
|
||||
$route = new RouterRessource($url, $controller);
|
||||
$router = RouterBase::getInstance();
|
||||
$router->addRoute($route);
|
||||
|
||||
return $route;
|
||||
}
|
||||
|
||||
public function getRoute($controller = null, $parameters = null, $getParams = null) {
|
||||
|
||||
Reference in New Issue
Block a user