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

时间:2012-08-22 18:19:24

标签: php codeigniter

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

谢谢!

2 个答案:

答案 0 :(得分:2)

我在CodeIgniter中创建了一些关键的高可用性系统,并且发现它非常强大且对我的特定项目非常灵活。它没有像ZF这样的其他“企业”框架附带的大包袱(不是说ZF没有自己的优势)。

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

我想主要的想法是MVC没有silver-bullet正确的方法 - 对一个项目有用的东西对另一个项目来说可能不是一个好主意。有很多因素可以衡量。如果你的代码是easily maintainable,那么一天结束时,如果一个人可以read and understand布局的方式没有太多麻烦,那么{{1}像程序员/前端HTML设计人员/后端数据库编码人员可以轻松地使用框架一起工作,而不会踩到(太多)彼此的脚趾,然后我会说你的MVC正在完成它的工作。

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

答案 1 :(得分:0)

你做的是完全正确的事情,而且这个文件还不够大,我怀疑它会是什么。我编写了 CodeIgniter - 强大而轻量级的FW,并且有一个很酷的MVC模型/模式解决方法。所以继续前进。

另一件事,你的文件/控制器没有实现 1000-1500 代码行,这个问题并没有打扰你自己。

MVC实际上并不是你的控制器包含的内容,而不是一大堆模型(数据库查询,Herlper函数),视图(模板/ GUI)和< strong>控制器(逻辑) - 记住这一点,此时不要担心任何事情。

相关问题