大约一周前,我开始尝试使用 CodeIngiter,因为我想学习 OOP。我以为我走在正确的轨道上,但现在我开始怀疑了。原因是,我有一个成员控制器,它正在变成一个很大的文件。这是因为我希望我的网址像成员/登录、成员/注册等。
这是我的控制器:
<?php
class Members extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('users');
}
public function index()
{
}
public function register()
{
$this->load->helper(array(
'form',
'recaptcha'
));
$this->load->library('form_validation');
$data['title'] = "Register a free account - Become an Author";
$data['titlesucces'] = "Thanks for registering";
$this->form_validation->set_rules('fname', 'First name', 'required');
$this->form_validation->set_rules('lname', 'Last name', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passwordconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Emailaddress', 'required|is_unique[users.email]|valid_email');
$this->form_validation->set_rules('recaptcha_challenge_field', 'Captcha', 'required|recaptcha_matches');
if (!$this->form_validation->run()) {
$this->load->view('header', $data);
$this->load->view('register', $data);
} else {
$this->users->register_new_member();
$this->load->view('register_succes', $data);
}
}
public function login()
{
$data['title'] = "Login";
$data['fail'] = "";
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Emailaddres', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if (!$this->form_validation->run()) {
$this->load->view('login', $data);
} else {
if ($this->users->do_login($this->input->post('email'), $this->input->post('password'))) {
$this->load->view('login', $data);
} else {
$data['fail'] = "Emailaddress or password is incorrect";
$this->load->view('login', $data);
}
}
}
public function logout()
{
$this->session->sess_destroy();
redirect('/members/login/', 'refresh');
}
public function addarticle()
{
if ($this->users->logged_in()) {
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'Title', 'required|max_length[200]|min_length[10]');
$this->form_validation->set_rules('intro', 'Intro', 'required|min_length[40]|max_length[50]');
$this->form_validation->set_rules('cat', 'Category', 'required');
$this->form_validation->set_rules('body', 'Article', 'required|min_length[3000]|link_check');
$this->load->model('categories');
$data['title'] = "Add a new article";
$data['cats'] = $this->categories->get_all_categories();
if (!$this->form_validation->run()) {
$this->load->view('addarticle', $data);
} else {
$this->load->model('articles');
$this->articles->add_new_article();
$this->load->view('welcome');
}
} else {
redirect('/members/login/', 'refresh');
}
}
}
?>
正如您所看到的,它已经是一个相当大的文件,但它只会变得更大。现在我对你们的问题是:这仍然是正确的 MVC 方式还是我做错了什么?
谢谢!