dpp78272 2012-04-10 12:58
浏览 19
已采纳

用户注册和MVC

Rather a basic question, though i'm a bit confused with the architecture.
I am programming a website using Yii framework.
There is a SiteController and AdminController (built not by me).
All the site administration is in AdminController, and the user interaction in SiteController.
I know it is not correct as it is not model-based.

Now, I want to implement user registration. The user can signup and administrator can change his properties.
Is it better to build UserController or manage all the above in the existing controller (not breaking previous logic)?

  • 写回答

2条回答 默认 最新

  • duansha7453 2012-04-10 13:12
    关注

    By MVC standards its best to split controllers by function groups.

    Auth Controller

    handles authentication/registration/deletion. Sets session vars for logins which all other controllers check first thing.

    function register(){
    }
    function logout(){
    }
    function edit(){
    }
    function login(){
    }
    

    Admin Controller

    handles all administrative tasks

    User Controller

    handles all user tasks. Maybe editing profiles, or posting to a personal blog?

    Front Controller

    handles all public facing tasks, displaying non auth based pages etc.


    Unrelated, CodeIgniter Example

    I use this methodology in my CI projects. This information will vary from Yii framework but it gives you a glimpse of a proper MVC setup.

    I have core controllers such as:

    application/core/FRONT_Controller.php this controller extends CI_Controller and handles all the setup of my pub facing pages. All of my public facing controllers extend FRONT_Controller instead of CI_Controller.

    application/core/USER_Controller.php this controller handles all of my users authentication.

    application/core/ADMIN_Controller.php this controller does preliminary checks, again all of my admin based controllers extend this controller so the authentication fires off right away... IE:

    if (!$this->tank_auth->is_logged_in()){
            redirect(site_url('/user/login/'));
        }else{
            $this->data['user'] = array(
                'user_id' => $this->tank_auth->get_user_id(),
                'username' => $this->tank_auth->get_username(),
                'emailAddress' => $this->tank_auth->get_email(),
                'firstname' => $this->tank_auth->get_firstname(),
                'lastname' => $this->tank_auth->get_lastname(),
                'userlevel' => $this->tank_auth->get_userlevel(),
                'avatar' => $this->tank_auth->get_avatar(),
                'last_login' => $this->tank_auth->get_last_login()
            );
        }
    

    application/controllers/blog.php:

    class Categories extends ADMIN_Controller{
    
    function __construct(){
        parent::__construct();
    
    } #end constructor function
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系