无法路由到CodeIgniter中的静态PHP页面

时间:2015-02-14 13:56:37

标签: codeigniter url-routing

我是CodeIgniter的新手。在我的观看中'文件夹,我创建了2个PHP页面 - home.php和register.php。我还在模板文件夹中创建了页眉和页脚PHP页面。

这是我的pages.php代码,它是控制器类:

<?php
class Pages extends CI_Controller
{
    public function view($page)
    {
        if(!file_exists(APPPATH.'/views/pages/'.$page.'.php'))
        {
            show_404();
        }
        $data['title'] = ucfirst($page);
        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);
    }
}

这是我的routes.php代码:

$route['register'] = "pages/view/register";
$route['default_controller'] = 'pages/view/home';

我还没有在.htaccess文件中写任何内容。

我的项目主目录是&#39; ED&#39;

我可以使用以下网址导航到主页:http://localhost/ED

但是,我无法使用以下网址导航到注册页面: 本地主机/ ED /注册

本地主机/ ED / register.php

请帮助我如何实现这一目标。

1 个答案:

答案 0 :(得分:1)

默认情况下,您必须拥有 index.php ,因为根据CodeIgniter URLs

  

默认情况下,index.php文件将包含在您的网址中:

     

example.com/的的index.php /消息/条/ my_article

因此,如果您要删除 index.php ,您可以按照以下简单步骤操作:

  

您可以使用.htaccess文件轻松删除此文件   简单的规则。这是一个这样的文件的例子,使用&#34;否定&#34;   除指定项目外,所有内容都被重定向的方法:

RewriteEngine on 
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
  

在上面的例子中,任何HTTP   index.php,images和robots.txt以外的请求是   作为index.php文件的请求处理。

只需将你的.htaccess文件(带有mod重写)放在主appplication文件夹中即可完成。 我用.htaccess做的就是这个:

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

    #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>