gpt4 book ai didi

php - CodeIgniter 子文件夹和 URI 路由

转载 作者:搜寻专家 更新时间:2023-10-31 20:54:02 25 4
gpt4 key购买 nike

我已经阅读了有关 URI 路由和 View 的手册,但有些地方并没有引起我的兴趣。

在我的 views 文件夹中,我有一个名为 products 的子文件夹。其中有一个名为 product_view 的文件。在我的 Controller 中,我有:

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

模板加载一个页眉 View 、一个页脚 View 和一个导航 View ,加上作为主要内容变量的 View 。

在我的 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 由您的 Controller 名称和 Controller 中的方法名称路由。

如果 Controller 名为“products.php”,相应 View 的函数称为“index”,该页面的正确 URI 为“/products”。

所以你的代码应该是

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

关于php - CodeIgniter 子文件夹和 URI 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4564025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com