15

我正在尝试在 CodeIgniter 框架上运行的网站上设置博客脚本。我想在不对现有网站代码进行任何重大代码更改的情况下执行此操作。我认为创建一个指向另一个控制器的子域将是最干净的方法。

我设置新Blog控制器的步骤涉及:

  1. 创建一个指向我的服务器 IP 地址的 A 记录。
  2. 将新规则添加到 CodeIgniter 的routes.php文件中。

这是我想出的:

switch ($_SERVER['HTTP_HOST']) {
    case 'blog.notedu.mp':
        $route['default_controller'] = "blog"; 
        $route['latest'] = "blog/latest";
        break;
    default:
        $route['default_controller'] = "main";
        break;
}

这应该指向我的blog.notedu.mp控制器。blog.notedu.mp/latestblog

现在问题来了……

访问blog.notedu.mpblog.notedu.mp/index.php/blog/latest工作正常,但是blog.notedu.mp/latest由于某种原因访问将我带到 404 页面...

我的 .htaccess 文件如下所示(从 url 中删除 index.php 的默认设置):

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

我的Blog控制器包含以下代码:

class Blog extends CI_Controller {

    public function _remap($method){
        echo "_remap function called.\n";
        echo "The method called was: ".$method;
    }

    public function index()
    {
        $this->load->helper('url');
        $this->load->helper('../../global/helpers/base');

        $this->load->view('blog');
    }

    public function latest(){
        echo "latest working";
    }

}

我在这里错过了什么或做错了什么?几天来我一直在寻找解决这个问题的方法:(

4

4 回答 4

4

经过4天的反复试验,我终于解决了这个问题!

原来这是一个 .htaccess 问题,以下规则修复了它:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

感谢所有阅读或回答此问题的人。

于 2014-02-05T19:35:48.360 回答
1

blog.domain.co/blog/latest 是否也显示 404?也许您还可以查看默认控制器的 _remap() 函数。 http://ellislab.com/codeigniter/user-guide/general/controllers.html#default

基本上,CodeIgniter 使用 URI 的第二段来确定控制器中的哪个函数被调用。您可以通过使用 _remap() 函数来覆盖此行为。

直接来自用户指南,

如果你的控制器包含一个名为 _remap() 的函数,无论你的 URI 包含什么,它都会被调用。它覆盖了 URI 确定调用哪个函数的正常行为,允许您定义自己的函数路由规则。

public function _remap($method)
    {
        if ($method == 'some_method')
        {
            $this->$method();
        }
        else
        {
            $this->default_method();
        }
    }

希望这可以帮助。

于 2014-02-02T20:09:38.107 回答
0

在apache的子域的配置文件中有一个“AllowOverride All”?

没有它“blog.notedu.mp/index.php/blog/latest”工作完美,但“blog.notedu.mp/latest”没有

于 2014-02-05T12:31:55.577 回答
-1
$route['latest'] = "index";

表示 URL将在控制器中http://blog.example.com/latest查找index()方法。index

你要

$route['latest'] = "blog/latest"; 

Codeigniter 用户指南在此处对路线有明确的解释

于 2014-02-05T02:23:36.340 回答