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


MVC Approach, is this still going the right way?

大约一周前,我开始尝试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方式,还是我做错了什么?

谢谢!

我在CodeIgniter中创建了一些关键的高可用性系统,发现它非常强大,非常灵活,适合我的特定项目。它没有像ZF这样的其他"企业"框架所带来的沉重负担(并不是说ZF没有自己的优势)。

正如zeusakm所说,你的控制器并没有那么大。然而,这也取决于你站在fat controller-lean model/lean controller-fat model辩论的哪一边(以及其他无数的变体/风格)。就我个人而言,我更喜欢保持我的控制器尽可能瘦。如果我觉得我的控制器做了太多的事情,并且变得臃肿,有时我会将其中一些功能转移到助手(而不是模型)中,主要是因为我喜欢让我的模型反映业务对象。此外,当其中一些任务可以耦合到一个更完善的实体中时,有时我会将它们合并到库(我自己或第三方)中

我想主要的想法是,MVC没有silver-bullet的正确方法——对一个项目有效的方法对另一个项目可能不是一个好主意。有很多因素需要权衡。归根结底,如果你的代码是easily maintainable,如果一个人可以按照read and understand的方式进行布局,而不会有太多麻烦,如果拥有different roles/responsiblites的人,比如程序员/前端HTML设计师/后端数据库程序员,都可以轻松地使用该框架协同工作,而不会(太多)相互干扰,那么是的,我认为你的MVC正在发挥作用。

还要记住,URLS如何映射到MVC只是系统的一个方面。这可以通过多种不同的方式来处理,从映射到详细模型的mock控制器,htaccess重写,有时甚至MVC路由器也可以帮助您配置URL的解析方式。。

您正在做完全正确的事情,但这个文件还不算大,我怀疑它会是大的。我已经在CodeIgniter上编程了-很棒的轻量级FW,还有一个很酷的MVC模型/模式解决方案。所以继续前进吧。

另一件事是,当你的文件/控制器没有完成1000-1500行代码时,不要用这个问题困扰你自己。

MVC实际上并不是您的控制器所包含的,而是模型(DB查询、Herlper函数)、视图控制器