CodeIgniter路由规则帮助需要,CI 2.0版


CodeIgniter routing rules help needed, CI version 2.0

我想要以下行为:

  1. http://www.example.com/应访问:http://example.com/welcome

  2. http://www.example.com/controllerName/functionName应该去
  3. 如果controllerName不存在,应该转到:specialController和specialFunction,其中functionaName为参数

。http://www.example.com/greatProducts应该变成http://www.example.com/specialController/specialFunction/greatProducts如果greatProducts控制器不存在,(它可以是任何字符串,不只是greatProduct,情况相同,即该名称的控制器不存在)

希望更好地使用路由规则实现这一点,对我来说,通过编辑库来改变URI段似乎不是一个好的选择。

如此:

我试着在core/Routes.php中编辑Routes.php,并添加类似

的内容
`$segments = array("specialController","specialFunction",$segments[0]);
        return $segments;`

如果发现控制器不存在

试试这个(尚未测试,但应该可以工作):

 $route['greatPtroduct/(:any)'] = "specialController/specialFunction/$1";

控制器:

class specialController extends CI_Controller {
  function specialFunction($method)
  {
     if(method_exists($this,$method))
     {
       $this->$method;
     }
     else
     {
      show_404;
     }
  }

更新:

更棘手。像

这样的路由
$route['(:any)/(:any)'] = "specialController/specialFunction/$1";

可以工作,但它会捕获任何控制器,所以如果你的应用程序中有其他控制器,你需要将它们列入白名单,以避免被捕获。例:

$route['contact'] = "contact";
$route['about'] = "about";
// and so on
$route['(:any)/(:any)'] = "specialController/specialFunction/$1";