|
@@ -9,6 +9,13 @@ class Router {
|
|
*/
|
|
*/
|
|
public static $routes = array();
|
|
public static $routes = array();
|
|
|
|
|
|
|
|
+
|
|
|
|
+ * All of the "fallback" routes that have been registered.
|
|
|
|
+ *
|
|
|
|
+ * @var array
|
|
|
|
+ */
|
|
|
|
+ public static $fallback = array();
|
|
|
|
+
|
|
|
|
|
|
* All of the route names that have been matched with URIs.
|
|
* All of the route names that have been matched with URIs.
|
|
*
|
|
*
|
|
@@ -16,6 +23,13 @@ class Router {
|
|
*/
|
|
*/
|
|
public static $names = array();
|
|
public static $names = array();
|
|
|
|
|
|
|
|
+
|
|
|
|
+ * The actions that have been reverse routed.
|
|
|
|
+ *
|
|
|
|
+ * @var array
|
|
|
|
+ */
|
|
|
|
+ public static $uses = array();
|
|
|
|
+
|
|
|
|
|
|
* The wildcard patterns supported by the router.
|
|
* The wildcard patterns supported by the router.
|
|
*
|
|
*
|
|
@@ -43,6 +57,18 @@ class Router {
|
|
*/
|
|
*/
|
|
public static $methods = array('GET', 'POST', 'PUT', 'DELETE');
|
|
public static $methods = array('GET', 'POST', 'PUT', 'DELETE');
|
|
|
|
|
|
|
|
+
|
|
|
|
+ * Register a HTTPS route with the router.
|
|
|
|
+ *
|
|
|
|
+ * @param string|array $route
|
|
|
|
+ * @param mixed $action
|
|
|
|
+ * @return void
|
|
|
|
+ */
|
|
|
|
+ public static function secure($route, $action)
|
|
|
|
+ {
|
|
|
|
+ static::register($route, $action, true);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
* Register a route with the router.
|
|
* Register a route with the router.
|
|
*
|
|
*
|
|
@@ -56,12 +82,18 @@ class Router {
|
|
*
|
|
*
|
|
* @param string|array $route
|
|
* @param string|array $route
|
|
* @param mixed $action
|
|
* @param mixed $action
|
|
|
|
+ * @param bool $https
|
|
* @return void
|
|
* @return void
|
|
*/
|
|
*/
|
|
- public static function register($route, $action)
|
|
+ public static function register($route, $action, $https = false)
|
|
{
|
|
{
|
|
|
|
+ if (is_string($route)) $route = explode(', ', $route);
|
|
|
|
+
|
|
foreach ((array) $route as $uri)
|
|
foreach ((array) $route as $uri)
|
|
{
|
|
{
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
if (starts_with($uri, '*'))
|
|
if (starts_with($uri, '*'))
|
|
{
|
|
{
|
|
static::universal(substr($uri, 2), $action);
|
|
static::universal(substr($uri, 2), $action);
|
|
@@ -69,13 +101,26 @@ class Router {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (str_contains($uri, ' /('))
|
|
|
|
+ {
|
|
|
|
+ $routes =& static::$fallback;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ $routes =& static::$routes;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (is_string($action))
|
|
if (is_string($action))
|
|
{
|
|
{
|
|
- static::$routes[$uri]['uses'] = $action;
|
|
+ $routes[$uri]['uses'] = $action;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -85,10 +130,19 @@ class Router {
|
|
{
|
|
{
|
|
if ($action instanceof Closure) $action = array($action);
|
|
if ($action instanceof Closure) $action = array($action);
|
|
|
|
|
|
- static::$routes[$uri] = (array) $action;
|
|
+ $routes[$uri] = (array) $action;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if ( ! isset($routes[$uri]['https']))
|
|
|
|
+ {
|
|
|
|
+ $routes[$uri]['https'] = $https;
|
|
}
|
|
}
|
|
|
|
|
|
- static::$routes[$uri]['handles'] = (array) $route;
|
|
+ $routes[$uri]['handles'] = (array) $route;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -140,7 +194,7 @@ class Router {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- foreach (static::$routes as $key => $value)
|
|
+ foreach (static::routes() as $key => $value)
|
|
{
|
|
{
|
|
if (isset($value['name']) and $value['name'] == $name)
|
|
if (isset($value['name']) and $value['name'] == $name)
|
|
{
|
|
{
|
|
@@ -149,6 +203,40 @@ class Router {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+ * Find the route that uses the given action and method.
|
|
|
|
+ *
|
|
|
|
+ * @param string $action
|
|
|
|
+ * @param string $method
|
|
|
|
+ * @return array
|
|
|
|
+ */
|
|
|
|
+ public static function uses($action, $method = 'GET')
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (isset(static::$uses[$method.$action]))
|
|
|
|
+ {
|
|
|
|
+ return static::$uses[$method.$action];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Bundle::routes(Bundle::name($action));
|
|
|
|
+
|
|
|
|
+ foreach (static::routes() as $uri => $route)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (isset($route['uses']) and $route['uses'] == $action)
|
|
|
|
+ {
|
|
|
|
+ if (starts_with($uri, $method))
|
|
|
|
+ {
|
|
|
|
+ return static::$uses[$method.$action] = array($uri => $route);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
* Search the routes for the route matching a method and URI.
|
|
* Search the routes for the route matching a method and URI.
|
|
*
|
|
*
|
|
@@ -192,7 +280,7 @@ class Router {
|
|
|
|
|
|
if ($bundle !== DEFAULT_BUNDLE)
|
|
if ($bundle !== DEFAULT_BUNDLE)
|
|
{
|
|
{
|
|
- $uri = str_replace(Bundle::get($bundle)->handles, '', $uri);
|
|
+ $uri = str_replace(Bundle::option($bundle, 'handles'), '', $uri);
|
|
|
|
|
|
$uri = ltrim($uri, '/');
|
|
$uri = ltrim($uri, '/');
|
|
}
|
|
}
|
|
@@ -210,7 +298,7 @@ class Router {
|
|
*/
|
|
*/
|
|
protected static function match($destination)
|
|
protected static function match($destination)
|
|
{
|
|
{
|
|
- foreach (static::$routes as $route => $action)
|
|
+ foreach (static::routes() as $route => $action)
|
|
{
|
|
{
|
|
|
|
|
|
|
|
|
|
@@ -280,7 +368,7 @@ class Router {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
+
|
|
$prefix = Bundle::prefix($bundle);
|
|
$prefix = Bundle::prefix($bundle);
|
|
|
|
|
|
$action = array('uses' => $prefix.$controller.'@'.$method);
|
|
$action = array('uses' => $prefix.$controller.'@'.$method);
|
|
@@ -328,7 +416,7 @@ class Router {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
+
|
|
$key = str_replace($search, $replace, $key, $count);
|
|
$key = str_replace($search, $replace, $key, $count);
|
|
|
|
|
|
if ($count > 0)
|
|
if ($count > 0)
|
|
@@ -339,4 +427,14 @@ class Router {
|
|
return strtr($key, static::$patterns);
|
|
return strtr($key, static::$patterns);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+ * Get all of the registered routes, with fallbacks at the end.
|
|
|
|
+ *
|
|
|
|
+ * @return array
|
|
|
|
+ */
|
|
|
|
+ public static function routes()
|
|
|
|
+ {
|
|
|
|
+ return array_merge(static::$routes, static::$fallback);
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|