所以,我想用PHP学习MVC并做了一个简单的项目。CodeIgniter不会重定向到控制器,Apache给404,基本控制器工作
我配置了XAMPP和重定向到我的项目目录的别名/ gambit,不知道它是否重要。
这是我的控制器users.php:
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->model('user');
$this->load->helper('url');
}
public function account()
{
$data = array();
if($this->session->userdata('isUserLoggedIn'))
{
$data['user'] = $this->user->getRows(array('id'=>$this->session->userdata('userId')));
$this->load->view('account',$data);
}
else
{
redirect('login');
}
}
public function login()
{
$data=array();
if($this->session->userdata('success_msg'))
{
$data['success_msg'] = $this->session->userdata('success_msg');
$this->session->unset_userdata('successs_msg');
}
if($this->session->userdata('error_msg'))
{
$data['error_msg'] = $this->session->userdata('error_msg');
$this->session->unset_userdata('error_msg');
}
if($this->input->post('loginSubmit'))
{
$this->form_validation->set_rules('email','Email','required|valid_email');
$this->form_validation->set_rules('password','Password','required');
if($this->form_validation->run() == true)
{
$con['returnType'] = 'single';
$con['conditions'] = array(
'email' =>$this->input->post('email'),
'password' => md5($this->input->post('password')),
'status' => '1'
);
$checkLogin = $this->user->getRows($con);
if($checkLogin)
{
$this->session->set_userdata('isUserLoggedIn',TRUE);
$this->session->set_userdata('userId',$checkLogin['id']);
redirect('account');
}
else
{
$data['error_msg'] = 'Wrong E-mail or password, please try again.';
}
}
}
$this->load->view('login', $data);
}
public function registration()
{
$data = array();
$userData = array();
if($this->input->post('regisSubmit'))
{
$this->form_validation->set_rules('email','Email','required|valid_email|callback_email_check');
$this->form_validation->set_rules('password','password','required');
$this->form_validation->set_rules('conf_password','confirm password','required|matches[password]');
$userData = array(
'email' =>strip_tags($this->input->post('email')),
'password' => md5($this->input->post('password'))
);
if($this->form_validation->run() == true)
{
$insert = $this->user->insert($userData);
if($insert)
{
$this->session->set_userdata('success_msg','Registration successful! Please, log in');
redirect ('login');
}
else
{
$data['error_msg'] = 'An error occured. Please, try again later.';
}
}
}
$data['user'] = $userData;
$this->load->view('registration',$data);
}
public function logout()
{
$this->session->unset_userdata('isUserLoggedIn');
$this->session->unset_userdata('userId');
$this->session->sess_destroy();
$this->load->view('login',$data);
}
public function email_check($str)
{
$con['returnType'] = 'count';
$con['conditions'] = array('email'=>$str);
$checkEmail = $this->user->getRows($con);
if($checkEmail>0)
{
$this->form_validation->set_message('email_check','The given email already exists.');
return FALSE;
}
else
{
return TRUE;
}
}
}
功能注册()是有问题的,我希望它重定向到登录()控制器的功能,注册成功后,反而阿帕奇给我错误404对象无法找到。
我尝试“登录”,“/登录”“用户/登录”等无济于事
如果我设置登录为基础的控制器,它就像一个魅力。
我在XAMPP rewrite_mod启用,这是我的.htaccess文件
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
的config.php:
$config['index_page'] = '';
$config['uri_protocol'] = "REQUEST_URI"; //tried AUTO too
$config['base_url'] = 'http://localhost/gambit/';
项目结构:
提示:请在您的用户模型User_model.php同为类,仿佛有同一类控制器可现在又引起了一些错误, – user4419336
还打着你的控制器文件Users.php其中第一个字母只是大写https://www.codeigniter.com/user_guide/general/styleguide.html#file-naming – user4419336