3

我正在构建一个基于 CodeIgniter 的 CMS。它将“视图”及其数据存储在数据库中,并在需要时收集适当的数据。您可能已经猜到了——我无法为每个页面生成物理控制器和匹配视图。

我认为路由会派上用场,因为我不想使用在 URL 中可见的控制器。解释得不好:我正在寻找一种方法,将所有最终不在物理现有控制器上的请求重新分配给自定义控制器 - 而不会出现在 URL 中。这个控制器当然会处理 404 错误等。

坏:.com/handler/actual-view/)好:((.com/actual-view/)不存在实际视图控制器,否则将显示)

我添加了404_override一条指向handler/. 现在,我只是在寻找一种方法来找出请求的视图(即在.com/actual-view实际视图中是我正在寻找的)。

我试过了

$route['404_override/(:any)'] = 'handler/$1';

和类似的,这将完全删除 404 覆盖。

4

3 回答 3

0

您最好扩展基本路由器或控制器。

通过这样做,您可以让您的应用程序变得灵活,并且仍然符合 CI 的工作方式。

于 2012-04-16T18:12:58.153 回答
0

您需要在 route.php 配置文件中定义所有有效路由,然后在最后一行,

$routes["(:any)"] = "specific controller path";

如果我应该举个例子:

$route['u/(:any)/account'] = "user_profile/account/$1";
$route['u/(:any)/settings'] = "user_profile/settings/$1";
$route['u/(:any)/messages'] = "user_profile/messages/$1";
$route['u/(:any)'] = "user_profile/index/$1";

如此处所见,在前三个无法捕捉到它之后,我将所有 url 转移到用户个人资料。

于 2012-04-16T18:47:20.663 回答
0

在 CodeIgniters 很棒的论坛和 StackOverflow 的可爱成员的一些指导下,我的解决方案变成了将所有 404 错误路由到我的自定义控制器,在那里我确保它是一个真正的 404(没有视图或控制器)。稍后在控制器中,我从我的数据库 URI 字符串中收集我需要的其余信息:

//Route
$route['404_override'] = 'start/handler';

//Controller
function handler($path = false) {

   //Gather the URI from the URL-helper
   $uri_string = uri_string();

   //Ensure we only get the desired view and not its arguments
   if(stripos($uri_string, "/") !== false) {
      //Split and gather the first piece
      $pieces = explode("/", $uri_string);
      $desired_view = $pieces[0];
   } else {
      $desired_view = $uri_string;
   }

   //Check if there's any view under this alias
   if($this->site->is_custom_view($desired_view)) {

      //There is: ensure that the view has something to show
      if(!$this->site->view_has_data($desired_view)) {
         //No data to show, throw an error message
         show_custom_error('no_view_data');
      } else {
         //Found the views data: show it
      }

   } else {
      //No view to show, lets go with 404
      show_custom_404();
   }
}
于 2012-04-16T20:54:24.253 回答