newBike
newBike

Reputation: 15002

How to add another controller file in CI (codeigniter)

the title seems stupid. but i met the problem, now i can just type http://ci.tao2tw.com, then routes to my index.php/entry function but when i type http://ci.tao2tw.com/order , i wanna routes to another controller order.php , however it doesn't work ! is anything wrong in my setting??

now , i can not run test function under order controller by http://ci.tao2tw.com/order/test . instead, i should type htpp://ci.tao2tw.com/index.php/order/test i can not figure out??

thank you all in advance~ enter image description here enter image description here

in routes.php

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

in config.php

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

in controller/index.php (it works fine )

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);      

    }

}

in controller/order.php ( no work )

<?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 file

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

Upvotes: 1

Views: 11512

Answers (4)

newBike
newBike

Reputation: 15002

sorry, everyboy. i found the solution so stupid i am. i use apache 2 and hava a virtual host with file at path : "/etc/apache2/sites-enabled/ci.tao2tw.com"

i have to change AllowOverride None to AllowOverride All

Upvotes: 2

MaNik
MaNik

Reputation: 121

I think you have problem with the .htaccess file. Is your application located in a subfolder?

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>

Upvotes: 0

Fabrice Tro&#239;lo
Fabrice Tro&#239;lo

Reputation: 184

Every controller must be called in CI after your index.php file.

So if you don't use a .htaccess file, your link to the order controller is like :

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

The .htaccess allows to skip the index.php file

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

You can so call your controller with :

http://ci.tao2tw.com/order

Then, if you want to call a method in your order controller your url is :

Upvotes: 4

Zathrus Writer
Zathrus Writer

Reputation: 4331

yeah, you need to route it like this:

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

the reason is that until you route the url to a model's method, it cannot know what function you want to perform

now, it will call the order() method of your Order module

Upvotes: 0

Related Questions