Example #1
0
 /**
  * load_routes
  *
  * Function to load all of the defined routes
  * 
  * @param mixed $config		Configuration array (or ArrayAccess class) defining our behaviors/options
  * @access public
  * @return void
  */
 public function load_routes($config = null)
 {
     // Set our config
     $this->config($config);
     // Get our routing config
     $routing_config = $this->get_config('routing');
     // If we want to load them all automatically
     if ($routing_config['load_all_automatically']) {
         // Define our routes directory
         $route_dir = $routing_config['route_directory'];
         // Get an array of all of the files in the routes directory
         $found_routes = scandir($route_dir);
         // Loop through each found route
         foreach ($found_routes as $route) {
             // Is the route a file?
             if (is_file($route_dir . $route) && strpos($route, '_') !== 0) {
                 // Define our endpoint base
                 $route_base_url = '/' . basename($route, '.php');
                 $route_namespace_url = $route_base_url;
                 $route_name = ltrim($route_namespace_url, '/');
                 // Is this our top-level route?
                 if ($route_name == $routing_config['top_level_route']) {
                     // Set our base url to the top level
                     $route_base_url = null;
                 }
                 // Instanciate the route's controller... but do it as a responder so it only instanciate's what is needed for that matched response. :)
                 Router::with($route_base_url, function () use($route_namespace_url) {
                     Router::route(function ($request, $respond, $service) use($route_namespace_url) {
                         // Instanciate the route's controller
                         Router::app()->new_route_controller($route_namespace_url);
                     });
                 });
                 // Include our routes from namespaced, separate files
                 Router::with($route_base_url, $route_dir . $route);
             }
         }
     } else {
         // Grab all of our manually assigned routes
         foreach ($routing_config['routes'] as $route) {
             // Define our endpoint base and include path
             $route_base_url = '/' . $route;
             $route_namespace_url = $route_base_url;
             $route_path = PAULUS_ROUTES_DIR . $route . '.php';
             // Is this our top-level route?
             if ($route == $routing_config['top_level_route']) {
                 // Set our base url to the top level
                 $route_base_url = null;
             }
             // Instanciate the route's controller... but do it as a responder so it only instanciate's what is needed for that matched response. :)
             Router::with($route_base_url, function () use($route_namespace_url) {
                 Router::route(function ($request, $respond, $service) use($route_namespace_url) {
                     // Instanciate the route's controller
                     Router::app()->new_route_controller($route_namespace_url);
                 });
             });
             // Include our routes from namespaced, separate files
             Router::with($route_base_url, $route_path);
         }
     }
 }