Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update router to find/verify existence of controller and, if not, mov…

…e on to the next router match
  • Loading branch information...
commit 43e5e6d42b65c81f1445e3dff11171e4af7328a5 1 parent c8c9d25
Joshua Rudd authored
Showing with 19 additions and 10 deletions.
  1. +12 −8 ControllerFront.php
  2. +7 −2 _example_app/config/routes.php
View
20 ControllerFront.php
@@ -62,18 +62,19 @@ public static function getUri()
return array('string'=>self::$requestUri,'array'=>self::$requestA);
}
- public static function getRoute($routes=array())
+ public static function getRoute($custom_routes=array())
{
- // Default routes
+ // Default routes (use these if they are not overridden in the router.php file)
+ $default_routes = array();
// Default homepage (index)
- if (!isset($routes['default_home'])) $routes['default_home'] = array('uri' => "/^\/$/");
+ if (!isset($custom_routes['default_home'])) $default_routes['default_home'] = array('uri' => "/^\/$/");
// Default controller
- if (!isset($routes['default_controller'])) $routes['default_controller'] = array('uri' => "/^\/(?<controller>[a-z0-9_-]+)$/i");
+ if (!isset($custom_routes['default_controller'])) $default_routes['default_controller'] = array('uri' => "/^\/(?<controller>[a-z0-9_-]+)$/i");
// Default controller+action
- if (!isset($routes['default_controller_action'])) $routes['default_controller_action'] = array('uri' => "/^\/(?<controller>[a-z0-9_-]+)\/(?<action>[a-z0-9_-]+)$/i");
+ if (!isset($custom_routes['default_controller_action'])) $default_routes['default_controller_action'] = array('uri' => "/^\/(?<controller>[a-z0-9_-]+)\/(?<action>[a-z0-9_-]+)$/i");
- // Prep variables
- $routes = array_reverse($routes);
+ // Combine default and custom routes
+ $routes = array_merge($default_routes,$custom_routes);
// Try to find a match
foreach ($routes as $route) {
@@ -90,7 +91,10 @@ public static function getRoute($routes=array())
if (!isset($regs['action'])) {
$regs['action'] = isset($route['action']) ? $route['action'] : DEFAULT_ACTION;
}
- return $regs;
+ // Only return a found route if the controller exists!
+ if (file_exists(CONTROLLERS . ucfirst($regs['controller']) . 'Controller.php')) {
+ return $regs;
+ }
}
}
}
View
9 _example_app/config/routes.php
@@ -1,23 +1,28 @@
<?php
+/* OVERRIDE DEFAULT ROUTES ----------------------------------------------- */
+
// Override default homepage (index)
//$routes['default_home'] = array('uri' => "/^\/$/");
+
// Override default controller
//$routes['default_controller'] = array('uri' => "/^\/(?<controller>[a-z0-9_-]+)$/i");
+
// Override default controller+action
//$routes['default_controller_action'] = array('uri' => "/^\/(?<controller>[a-z0-9_-]+)\/(?<action>[a-z0-9_-]+)$/i");
+
/* USER DEFINED ROUTES --------------------------------------------------- */
// The $routes[] array doesn't need a specific key defined, unless you are overriding the 'default_*' routes
// Sample additional arguments to specific controller/action
-//$routes[] = array('uri' => "/(?<controller>blog)\/(?<action>archive)\/(?<id>[0-9]+)/");
+//$routes[] = array('uri' => "/^\/(?<controller>blog)\/(?<action>archive)\/(?<id>[0-9]+)$/i");
// Sample controller override
/*
$routes[] = array(
- 'uri' => "/(blog)\/(?<action>archive)\/(?<id>[0-9]+)/",
+ 'uri' => "/^\/(blog)\/(?<action>archive)\/(?<id>[0-9]+)$/i",
'controller' => 'actual_blog_controller'
);
*/
Please sign in to comment.
Something went wrong with that request. Please try again.