Codeigniter - 路由到控制器(如果存在),如果不使用dafault [英] Codeigniter - routing to the controller if it exists, if not use dafault
问题描述
所以我已经设置了这样的路线:
So I have set up my routes like so:
$route[':any'] = "main";
$route['products/(:any)'] = "products/product/$1";
例如 www.mysite.com/something 其中我处理某物。
我用类似的方式处理产品。
For example www.mysite.com/something goes to main controller, where I deal with "something". With products I deal in similar way.
但是你可以看到,我必须写下所有其他控制器的方法,我希望用于代替主控制器。是否有自动检测控制器是否存在使用控制器而非默认main的方法?
But as you can see after that I have to write down all the other controllers with methods that I wish to be used instead of main controller. Is there a way to make it automatically detect if the controller exists use the controller and not the default "main"?
$route['products'] = "products";
$route['admin/user/login'] = "admin/user/login";
$route['admin/user/logout'] = "admin/user/logout";
$route['admin/migrations'] = "admin/migrations";
$route['admin/dashboard'] = "admin/dashboard";
推荐答案
发现它在某个博客上,但我使用下面的代码在我的routes.php,在你的情况下,我会把它放在 $ route [':any'] =main;
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";
}
}
他们,或有任何独特的路线,我把它们放在routes.php文件的顶部,在这段代码之上。
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.
这篇关于Codeigniter - 路由到控制器(如果存在),如果不使用dafault的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!