【问题标题】:Unable to route to static PHP page in CodeIgniter无法在 CodeIgniter 中路由到静态 PHP 页面
【发布时间】:2015-02-14 13:56:37
【问题描述】:

我是 CodeIgniter 的新手。在我的“views”文件夹中,我创建了 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 文件中写入任何内容。

我的项目主目录是“ED”

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

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

localhost/ED/register.php

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

【问题讨论】:

  • 你得到了什么? php错误还是404?
  • 我想导航到 localhost/ED/register,但不能这样做。我收到“找不到对象”错误。我可以导航到这个:localhost/ED/index.php/register

标签: codeigniter url-routing


【解决方案1】:

你必须默认拥有 index.php 因为根据CodeIgniter URLs

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

example.com/index.php/news/article/my_article

所以如果你想删除 index.php,你可以按照这个简单的步骤:

您可以使用 .htaccess 文件轻松删除此文件 简单的规则。这是此类文件的示例,使用“否定” 重定向除指定项目外的所有内容的方法:

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

在上面的例子中,任何 HTTP 除 index.php、images 和 robots.txt 之外的请求是 视为对 index.php 文件的请求。

只需将您的 .htaccess 文件(带有 mod 重写)放在您的主应用程序文件夹中,瞧,它就完成了。 我用我的 .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> 

【讨论】:

  • 感谢 Tweetie,这很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-03
  • 2015-07-07
  • 2019-06-05
  • 2011-10-24
  • 1970-01-01
相关资源
最近更新 更多