簡體   English   中英

CodeIgniter子文件夾和URI路由

[英]CodeIgniter subfolders and URI 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] 

我很感激一些具體的幫助,因為文檔並沒有具體說明如何解決這個問題。 我在論壇上做了一些搜索,但沒有看到任何內容,但是我一直在尋找這個帖子。

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

如果控制器名為“products.php”,則相應視圖的函數稱為“index”,該頁面的正確URI為“/ products”。

所以你的代碼應該是

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

要為您的控制器使用路由器擴展時談到了使用多級目錄結構這個職位 ,我已經用它的場合,並且可以保證它的工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM