Monday, 10 February 2014

Codeigniter - routing to the controller if it exists, if not use dafault

I can't take any credit for it as I found it on a blog somewhere, but I use the following code in my routes.php, in your case I would place it above the $route[':any'] = "main";
$controller_dir = opendir(APPPATH."controllers");

while (($file = readdir($controller_dir)) !== false) {

    if (substr($file, -4) == ".php" ) {

        $route[substr($file, 0, -4)."(.*)"] = substr($file, 0, -4)."$1";

    } elseif (substr($file, -5) == ".php/") {

        $route[substr($file, 0, -5)."(.*)"] = substr($file, 0, -5)."$1";

    }
}
Should I need to override any of them, or have any unique routes, I place those at the top of the routes.php file, above this code.

No comments:

Post a Comment