大家好,我是codeigniter的新手。我已经在Ubuntu 14.04上安装了codeigniter,现在可以看到欢迎页面了。然而,我创建的其他控制器不工作。
我得到一条消息,页面未找到。我已经环顾四周,但找不到任何东西来解决我的问题。
有谁能帮忙吗?
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->library('email');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('login_model');
}
public function index($msg = NULL) {
$data['msg'] = $msg;
if ($this->session->userdata('validated') == false) {
$this->load->view('templates/header_login');
$this->load->view('login_view', $data);
$this->load->view('templates/footer');
}
else {
redirect('home', 'refresh');
}
}
Routes.php
$route['default_controller'] = 'login';
默认的编码器页面工作完全正常。但如果我把默认控制器改成我的那么我就会得到一条消息页面未找到
无论何时安装Codeigniter,
您可以通过http://sitename/index.php/controller/method/parameter访问任何控制器。使用。htaccess文件从url中删除index.php。
您可以使用http://sitename.com/index.php/login
访问您的登录页面示例。htaccess代码:
RewriteEngine on
RewriteCond $1 !^(index.php|images|robots.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
要访问自定义控制器,您所要做的就是在URL中使用index.php
。例如,如果您的控制器名称是User
,则使用此url http://localhost/your_project/index.php/user
如果你想在User
控制器中使用其他方法如create
则使用
http://localhost/your_project/index.php/user/create
但是如果你不喜欢在你的URL中看到index.php
,那么你可以在你的根目录下放置一个名为.htaccess
的文件,并在这个文件中添加以下内容
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Ref: More Details
现在你的URL看起来像http://localhost/your_project/user/create
Codeigniter是这样工作的
www.yourdomain.com/controller/function如果你在控制器中添加索引函数,你可以直接访问
www.yourdomain.com/controller/检查是否添加了索引函数
请在项目根目录中添加。htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /stock
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>