如何在CI中添加另一个控制器文件(codeigniter)

时间:2012-08-25 09:57:07

标签: php codeigniter

标题似乎很愚蠢。但我遇到了这个问题, 现在我可以输入http://ci.tao2tw.com,然后路由到我的index.php / entry函数 但是当我输入http://ci.tao2tw.com/order时,我想路由到另一个控制器 order.php,但它不起作用! 我的设置有什么问题吗?

现在,我无法通过http://ci.tao2tw.com/order/test在订单控制器下运行测试功能。相反,我应该打字 HTPP://ci.tao2tw.com/的的index.php /订单/测试 我想不出来?

提前谢谢大家〜 enter image description here enter image description here

在routes.php中

$route['default_controller'] = "index/entry";
$route['order'] = "order"; 
$route['404_override'] = '';

在config.php中

$config['base_url'] = 'http://ci.tao2tw.com/';
$config['index_page'] = '';

在controller / index.php中(它工作正常)

class Index extends CI_Controller {

    public function __construct() // to remember use public
    {
        parent::__construct(); 
        $this->load->helper('url');
        //anchor(); 
    }
    public function entry() //just show index
    {
        $this->load->view('index_view',$data);      

    }

}

在controller / order.php(没有工作)

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Order extends CI_Controller {

    public function __construct() // to remember use public
    {
        parent::__construct(); 
        $this->load->helper('url');

    }
    public function fill($action) // 顯示填寫表單
    {
        echo "test";
    }
   //http://codeigniter.com/user_guide/libraries/uri.html
    public function order($action)
    {
        echo $action;
    }
}

?>

.htaccess文件

RewriteEngine on
RewriteCond $1 !^(index\.php|css|flash|images|img|includes|js|language|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
~

4 个答案:

答案 0 :(得分:4)

必须在 index.php 文件之后在CI中调用每个控制器。

因此,如果您不使用.htaccess文件,您到订单控制器的链接就像:

http://ci.tao2tw.com/index.php/order/

.htaccess允许跳过index.php文件

 RewriteRule ^(.*)$ index.php/$1 [L]

您可以通过以下方式致电您的控制器:

http://ci.tao2tw.com/order

然后,如果您想在订单控制器中调用方法,则您的网址为:

答案 1 :(得分:2)

对不起,每个男孩。我找到了解决方案 我很蠢。 我使用apache 2和hava一个虚拟主机,文件位于路径:“/etc/apache2/sites-enabled/ci.tao2tw.com”

我必须更改AllowOverride None to AllowOverride All

答案 2 :(得分:0)

是的,你需要像这样路由它:

$route['order'] = "order/order"; 

原因是,在将url路由到模型的方法之前,它无法知道您要执行的功能

现在,它将调用Order模块的order()方法

答案 3 :(得分:0)

我认为.htaccess文件存在问题。 您的应用程序是否位于子文件夹中?

RewriteEngine on
RewriteBase /path/to/app  [add this if your application is under a sub folder]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

<IfModule mod_php5.c>
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_php5.c>
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>