如何使用config.php中指定的base_url运行另一个控制器的函数


How to run function of another controller with base_url specified in config.php

我下面有一个控制器welcome,重定向到controllers/auth/login.php中另一个控制器的功能

function __construct() {
    parent::__construct();
    $this->load->helper('url');
    $this->load->library('tank_auth');
}
function index() {
    if (!$this->tank_auth->is_logged_in()) {
        redirect('/auth/login');
    } else {
        $data['user_id']    = $this->tank_auth->get_user_id();
        $data['username']   = $this->tank_auth->get_username();
        $this->load->view('welcome', $data);
    }
}

这里,config.php:

$config['base_url'] = '';
$config['index_page'] = 'index.php';

效果很好。但是当我在配置文件中指定base_url为:

$config['base_url'] = 'http://localhost/cilog/';
$config['index_page'] = '';

Object not found。为什么会这样?但是当我将index_page指定为index.php时,它再次工作。

我相信这是因为CodeIgniter处理它的URL的方式。我不确定为什么Codeigniter这样做,但他们包括index.php在那里的URL。

所以你的URL应该是http://localhost/cilog/index.php/auth/login

你可以重写你的.htaccess文件来删除index.php通过把这个放在:

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

并保持你的$config['base_url']设置为"http://localhost/cilog/"

同时指定$config['base_url']$config['index_page']

查看此处获取更多信息:http://ellislab.com/codeigniter/user-guide/general/urls.html(删除index.php文件)