diff --git a/src/Pecee/Http/Response.php b/src/Pecee/Http/Response.php index 566aa5e..5d1a0c3 100644 --- a/src/Pecee/Http/Response.php +++ b/src/Pecee/Http/Response.php @@ -22,6 +22,11 @@ class Response { */ public function redirect($url) { header('location: ' . $url); + die(); + } + + public function refresh() { + $this->redirect(url()); } } \ No newline at end of file diff --git a/src/Pecee/SimpleRouter/RouterBase.php b/src/Pecee/SimpleRouter/RouterBase.php index 0817c9f..b5a69e3 100644 --- a/src/Pecee/SimpleRouter/RouterBase.php +++ b/src/Pecee/SimpleRouter/RouterBase.php @@ -167,11 +167,11 @@ class RouterBase { $url = $route->getUrl(); - if(($route instanceof RouterController || $route instanceof RouterRessource) && $method !== null) { + if(($route instanceof RouterController || $route instanceof RouterResource) && $method !== null) { $url .= $method; } - if($route instanceof RouterController || $route instanceof RouterRessource) { + if($route instanceof RouterController || $route instanceof RouterResource) { if(count($parameters)) { $url .= join('/', $parameters); } @@ -217,9 +217,14 @@ class RouterBase { /* @var $route RouterRoute */ foreach($this->controllerUrlMap as $route) { + // Check an alias exist, if the matches - use it + if($route instanceof RouterRoute && strtolower($route->getAlias()) === strtolower($controller)) { + return $this->processUrl($route, $route->getMethod(), $parameters, $getParams); + } + if($route instanceof RouterRoute && !is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) { $c = $route->getCallback(); - } else if($route instanceof RouterController || $route instanceof RouterRessource) { + } else if($route instanceof RouterController || $route instanceof RouterResource) { $c = $route->getController(); } @@ -234,7 +239,7 @@ class RouterBase { foreach($this->controllerUrlMap as $route) { if($route instanceof RouterRoute && !is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) { $c = $route->getClass(); - } else if($route instanceof RouterController || $route instanceof RouterRessource) { + } else if($route instanceof RouterController || $route instanceof RouterResource) { $c = $route->getController(); } diff --git a/src/Pecee/SimpleRouter/RouterResource.php b/src/Pecee/SimpleRouter/RouterResource.php new file mode 100644 index 0000000..071ff40 --- /dev/null +++ b/src/Pecee/SimpleRouter/RouterResource.php @@ -0,0 +1,155 @@ +url = $url; + $this->controller = $controller; + $this->postMethod = strtolower(($_SERVER['REQUEST_METHOD'] != 'GET') ? 'post' : 'get'); + } + + public function renderRoute(Request $request) { + // Load middleware + $this->loadMiddleware($request); + + if(is_object($this->getCallback()) && is_callable($this->getCallback())) { + // When the callback is a function + call_user_func_array($this->getCallback(), $this->getParameters()); + } else { + // When the callback is a method + $controller = explode('@', $this->getCallback()); + $className = $this->getNamespace() . '\\' . $controller[0]; + $class = $this->loadClass($className); + $method = strtolower($controller[1]); + + if (!method_exists($class, $method)) { + throw new RouterException(sprintf('Method %s does not exist in class %s', $method, $className), 404); + } + + call_user_func_array(array($class, $method), $this->getParameters()); + + return $class; + } + + return null; + } + + protected function call($method, $parameters) { + $this->setCallback($this->controller . '@' . $method); + $this->parameters = $parameters; + return $this; + } + + public function matchRoute(Request $request) { + $url = parse_url($request->getUri()); + $url = rtrim($url['path'], '/') . '/'; + + if(strtolower($url) == strtolower($this->url) || stripos($url, $this->url) === 0) { + $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($request->getMethod() === self::REQUEST_TYPE_DELETE && $this->postMethod === self::REQUEST_TYPE_POST) { + return $this->call('destroy', $args); + } + + // Update + if(in_array($request->getMethod(), array('put', 'patch')) && $this->postMethod === self::REQUEST_TYPE_POST) { + return $this->call('update', array_merge(array($action), $args)); + } + + // Edit + if(isset($args[0]) && strtolower($args[0]) === 'edit' && $this->postMethod === self::REQUEST_TYPE_GET) { + return $this->call('edit', array_merge(array($action), array_slice($args, 1))); + } + + // Create + if(strtolower($action) === 'create' && $request->getMethod() === self::REQUEST_TYPE_GET) { + return $this->call('create', $args); + } + + // Save + if($this->postMethod === self::REQUEST_TYPE_POST) { + return $this->call('store', $args); + } + + // Show + if($action && $this->postMethod === self::REQUEST_TYPE_GET) { + return $this->call('show', array_merge(array($action), $args)); + } + + // Index + return $this->call('index', $args); + } + } + + return null; + } + + /** + * @return string + */ + public function getUrl() { + return $this->url; + } + + /** + * @param string $url + */ + public function setUrl($url) { + $url = rtrim($url, '/') . '/'; + $this->url = $url; + } + + /** + * @return string + */ + public function getController() { + return $this->controller; + } + + /** + * @param string $controller + */ + public function setController($controller) { + $this->controller = $controller; + } + + /** + * @return string + */ + public function getMethod() { + return $this->method; + } + + /** + * @param string $method + */ + public function setMethod($method) { + $this->method = $method; + } + +} \ No newline at end of file diff --git a/src/Pecee/SimpleRouter/RouterRoute.php b/src/Pecee/SimpleRouter/RouterRoute.php index 162ce03..a5d012a 100644 --- a/src/Pecee/SimpleRouter/RouterRoute.php +++ b/src/Pecee/SimpleRouter/RouterRoute.php @@ -136,32 +136,6 @@ class RouterRoute extends RouterEntry { return $this; } - /** - * @param array $aliases - * @return self - */ - public function setAliases(array $aliases) { - $this->aliases = $aliases; - return $this; - } - - /** - * Add alias - * - * @param $alias - * @return self - */ - public function addAlias($alias) { - $arr = $this->aliases; - $arr[] = $alias; - $this->aliases = $arr; - return $this; - } - - public function getAliases() { - $this->aliases; - } - /** * Add request type * @@ -184,4 +158,34 @@ class RouterRoute extends RouterEntry { public function getRequestTypes() { return $this->requestTypes; } + + /** + * Get alias for the url which can be used when getting the url route. + * @return string + */ + public function getAlias(){ + return $this->alias; + } + + /** + * Set the url alias for easier getting the url route. + * @param string $alias + * @return self + */ + public function setAlias($alias){ + $this->alias = $alias; + return $this; + } + + public function setSettings($settings) { + + // Change as to alias + if(isset($settings{'as'})) { + $this->setAlias($settings['as']); + } + + return parent::setSettings($settings); + } + + } \ No newline at end of file diff --git a/src/Pecee/SimpleRouter/SimpleRouter.php b/src/Pecee/SimpleRouter/SimpleRouter.php index e99671d..8820c17 100644 --- a/src/Pecee/SimpleRouter/SimpleRouter.php +++ b/src/Pecee/SimpleRouter/SimpleRouter.php @@ -17,8 +17,9 @@ class SimpleRouter { $router->routeRequest(); } - public static function get($url, $callback) { + public static function get($url, $callback, array $settings = null) { $route = new RouterRoute($url, $callback); + $route->addSettings($settings); $route->addRequestType(RouterRoute::REQUEST_TYPE_GET); $router = RouterBase::getInstance(); @@ -27,8 +28,9 @@ class SimpleRouter { return $route; } - public static function post($url, $callback) { + public static function post($url, $callback, array $settings = null) { $route = new RouterRoute($url, $callback); + $route->addSettings($settings); $route->addRequestType(RouterRoute::REQUEST_TYPE_POST); $router = RouterBase::getInstance(); @@ -37,8 +39,9 @@ class SimpleRouter { return $route; } - public static function put($url, $callback) { + public static function put($url, $callback, array $settings = null) { $route = new RouterRoute($url, $callback); + $route->addSettings($settings); $route->addRequestType(RouterRoute::REQUEST_TYPE_PUT); $router = RouterBase::getInstance(); @@ -47,8 +50,9 @@ class SimpleRouter { return $route; } - public static function delete($url, $callback) { + public static function delete($url, $callback, array $settings = null) { $route = new RouterRoute($url, $callback); + $route->addSettings($settings); $route->addRequestType(RouterRoute::REQUEST_TYPE_DELETE); $router = RouterBase::getInstance(); @@ -71,8 +75,9 @@ class SimpleRouter { return $group; } - public static function match(array $requestTypes, $url, $callback) { + public static function match(array $requestTypes, $url, $callback, array $settings = null) { $route = new RouterRoute($url, $callback); + $route->addSettings($settings); foreach($requestTypes as $requestType) { $route->addRequestType($requestType); } @@ -83,24 +88,27 @@ class SimpleRouter { return $route; } - public static function all($url, $callback) { + public static function all($url, $callback, array $settings = null) { $route = new RouterRoute($url, $callback); + $route->addSettings($settings); $router = RouterBase::getInstance(); $router->addRoute($route); return $route; } - public static function controller($url, $controller) { + public static function controller($url, $controller, array $settings = null) { $route = new RouterController($url, $controller); + $route->addSettings($settings); $router = RouterBase::getInstance(); $router->addRoute($route); return $route; } - public static function ressource($url, $controller) { - $route = new RouterRessource($url, $controller); + public static function resource($url, $controller, array $settings = null) { + $route = new RouterResource($url, $controller); + $route->addSettings($settings); $router = RouterBase::getInstance(); $router->addRoute($route);