CodeIgniter子文件夹和URI路由

时间:2010-12-30 15:51:16

标签: php .htaccess codeigniter routing

我已经阅读了关于URI路由和视图的手册,而且没有点击我的内容。

在我的views文件夹中,我有一个名为products的子文件夹。在有一个名为product_view的文件中。在我的控制器中,我有:

function index() {
            $data['title'] = 'Product Overview';
            $data['main_content'] = 'products/product_view';
            $this->load->view('templates/main.php', $data);
        } 

模板加载标题视图,页脚视图和导航视图,以及视图作为主要内容变量。

在我的URI路由中,我有:

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
|   example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
|   http://codeigniter.com/user_guide/general/routing.html
|
| -------------------------------------------------------------------------
| RESERVED ROUTES
| -------------------------------------------------------------------------
|
| There are two reserved routes:
|
|   $route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
|   $route['scaffolding_trigger'] = 'scaffolding';
|
| This route lets you set a "secret" word that will trigger the
| scaffolding feature for added security. Note: Scaffolding must be
| enabled in the controller in which you intend to use it.   The reserved 
| routes must come before any wildcard or regular expression routes.
|
*/

$route['default_controller'] = "index_controller";
$route['laser-configurator'] = "configurator";
$route['news-and-events'] = "news";
$route['products/product-overview'] = "products/product_view";
$route['scaffolding_trigger'] = "";


/* End of file routes.php */
/* Location: ./system/application/config/routes.php */

当我尝试访问domain.com/products/product-overview时,会出现404错误。我需要用我的.htaccess做点什么吗?如果是这样,什么?这是我的.htaccess:

Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA] 

我很感激一些具体的帮助,因为文档并没有具体说明如何解决这个问题。我在论坛上做了一些搜索,但没有看到任何内容,但是我一直在寻找这个帖子。

2 个答案:

答案 0 :(得分:2)

在CI中,URI由路由器的名称和控制器中方法的名称路由。

如果控制器名为“products.php”,则相应视图的功能称为“index”,该页面的正确URI为“/ products”。

所以你的代码应该是

$route['products/product-overview'] = 'products/index'; 

答案 1 :(得分:1)

要为控制器使用多级目录结构,请使用this post中谈到的路由器扩展,我偶尔会使用它,并且可以保证它正常工作。