doulu6234 2012-08-22 18:19
浏览 46
已采纳

MVC方法,这仍然是正确的方式吗?

About a week ago i started experimenting with CodeIngiter, because i want to learn OOP. I thought i was on the right track, but now i begin to doubt that. The reason is, i have a controller for Members, which is becoming quite a large file. This is because i want my urls to be like members/login , members/register etc.

Here is my controller:

<?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');
        }
    }

}
?>

As you can see its quite a big file already, but it will only get bigger. Now my question to you guys is: is this still the right MVC way or am i doing something wrong?

Thanks!

  • 写回答

2条回答 默认 最新

  • doupu5941 2012-08-22 18:28
    关注

    You are doing completelly right things, and this file isn't big anough yet, and I doubt it will be. I've programmed on CodeIgniter - great and light-weight FW, and there is a cool MVC model/pattern workarounds. So just keep going.

    Another thing while Your file/controller didn't achive 1000-1500 lines of code do not bother Your-self with this question.

    The MVC is not actually what Your controller contains, rather than whole bunch of things Models(DB Queries, Herlper Functions), Views(Templates/GUI) and Controller(Logic) - keep that in mind and do not worry about anything at this point.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?