Example #1
0
 /**
  * Register a route with the router.
  *
  * You can enable the cache on any call just by setting enableCache as true. The expiration time is expressed in seconds, default is 2 days.
  * 
  * <code>
  *		//Register a route with a callback function
  *		Router::register("GET", "/", function() { return "Index!"; });
  *
  *		//Register multiple routes with a view
  *		Router::register("GET", array("/", "index/"), "index");
  *		
  *		//Register a route with a controller's method
  *		Router::register("GET,POST", "/", "controller@method");
  * </code>
  *
  * @param  string        $method
  * @param  string|array  $route
  * @param  mixed         $action
  * @param  bool          $enableCache
  * @param  int           $cacheExpirationTime
  * @return void
  */
 public static function register($method, $route, $action, $enableCache = false, $cacheExpirationTime = 172800)
 {
     if (is_string($method) && Str::contains($method, ",")) {
         $method = explode(",", $method);
         foreach ($method as $v) {
             self::register($v, $route, $action);
         }
         return;
     }
     if (is_string($route) && Str::contains($route, ",")) {
         $route = explode(",", $route);
         foreach ($route as $v) {
             self::register($method, $v, $action);
         }
         return;
     }
     foreach ((array) $route as $uri) {
         if ($method == "*") {
             $methods = Request::availableMethods();
             foreach ($methods as $v) {
                 self::register($v, $route, $action);
             }
             continue;
         }
         $uri = trim($uri, "/");
         if (empty($uri)) {
             $uri = "/";
         }
         if ($uri[0] == "(") {
             $routes =& self::$fallback;
         } else {
             $routes =& self::$routes;
         }
         if (!isset($routes[$method])) {
             $routes[$method] = array();
         }
         if (is_string($action)) {
             $action = array("uses" => $action);
         } else {
             if (is_callable($action)) {
                 $action = array("handler" => $action);
             }
         }
         $action["cacheEnabled"] = $enableCache;
         $action["cacheExpirationTime"] = $cacheExpirationTime;
         $routes[$method][$uri] = (array) $action;
     }
 }