0

按照教程从Code Igniterindex.php中的 URL 中删除。

它现在适用于 my default_controller,但不适用于其他页面(或视图)。

我有一个控制器Pages,它有一种方法View($page = '主页'),根据传递的参数加载具有其他内容的页面。

如果我输入localhost/devURL - 我登陆我的主页,这是正确的。

如果我输入localhost/dev/aboutus- 我会收到404。仅当我键入时它才有效localhost/dev/pages/view/aboutus

我想要发生的是通过键入localhost/dev/aboutus它会显示AboutUs视图。

路由.php

$route['default_controller'] = "pages/view";
$route['dev/(:any)'] = "dev/pages/view/$1";
$route['404_override'] = '';

Pages.php (控制器)

<?php
    class Pages extends CI_Controller
    {
        public function view($page = 'home')
        {
            if (!file_exists('application/views/pages/' . $page . '.php'))
            {
                // Whoops, we don't have a page for that!
                show_404();
            }

            $data['title'] = ucfirst($page); // Capitalize the first letter

            $this->load->view('templates/header', $data);
            $this->load->view('pages/' . $page);
            $this->load->view('templates/footer');
        }
    }
?>

.htaccess 文件 (位于 /dev/ 文件夹中)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /dev/

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule> 
4

2 回答 2

1

这条路线$route['dev/(:any)'] = "dev/pages/view/$1";有点可疑,因为 CodeIgniter 路线不应包含项目名称(dev在您的情况下)。它们从控制器级别开始,而不是项目级别。

您编写的路由意味着如果用户键入http://localhost/dev/dev/something(是的,两个开发人员),他们将被路由到控制器dev.php并进入带有原型的函数:function('view', 'something'){}

为什么这么复杂?您应该将所有主页名称都放入控制器中。只有在特殊情况下才能触及路线。

创建一个包含内容aboutus.php的文件application/controllers

<?php
class Aboutus extends CI_Controller{
    function __construct(){
        parent::__construct();
    }

    function index(){
        "I'm in the about controller!";
        //$this->load->view("some_view") ;
    }
}

您可以使用它访问它http://localhost/dev/aboutus

于 2012-10-02T19:50:50.483 回答
0

如果您没有 $config['index.php'] = ''; 在您的 config.php 中将其留空

于 2012-10-02T19:00:19.970 回答