本文介绍了Yii Design - 如何拆分视图逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVC 对我来说是一个相当新的概念,我刚刚发现自己在做一些我不太确定会被认为是好的做法"的事情.

MVC is a fairly new concept for me and I have just found myself doing something I am not quite sure would be considered 'a good practice'.

我有一个控制器 User(从 CUserController 扩展)处理所有与用户相关的操作.在这个控制器中,我有 actionLogin 处理登录请求.现在在登录过程中我需要根据情况显示3个不同的页面:

I have a controller User (extending from CUserController) that deals with all user relate actions. In this controller I have actionLogin that deals with login requests. Now in the process of login I need to display 3 different pages according to situation:

  • 登录表单(输入用户名/密码).
  • 错误消息(如果用户详细信息匹配但帐户被标记为已阻止).
  • 登录成功页面(如果有任何新消息与该帐户相关联).

到目前为止,我是这样处理的:

So far I have deal with it like that:

public function actionLogin(){
    ... perform some operations ...
    ... determine which page to display and set $scenario accordingly ...
    $this->render('login',array(
        'scenario'=>$scenario
    ));
}

然后在login.php(视图文件)中:

And then in login.php (the view file):

... perform some common operations ...
switch($scenario){
    case "login":
        ...display login...
        break;
    case "error":
        ... display error ...
        break;
    .. etc. ...
}

现在这使我的视图文件实际上显示了完全不同的页面(尽管密切相关).

Now this makes my view file show in fact entirely different pages (although closely related).

将我的视图拆分成不同的页面会更好吗(例如 login-form.phplogin-error.phplogin-success.php) 并根据情况在 actionLogin 中呈现不同的内容,或者我目前正在做的处理这个问题的好方法是什么?

Would it be better to split my view into different pages (e.g. login-form.php,login-error.php,login-success.php) and render different ones in actionLogin according to the situation or is what I currently do a good way of dealing with this?

推荐答案

最好的选择是尝试修复 Yii 对 MVC 设计模式的解释,我喜欢称之为 ORM-Template-Adapter 模式.

The best option would be to try to fix the Yii interpretation of MVC design pattern, what I like to call ORM-Template-Adapter pattern.

您的问题源于这样一个事实,即在适当的 MVC 启发式结构中,视图应该是对象,负责所有表示逻辑并且每个都能够管理多个模板.

Your issue stems from the fact, that, in proper MVC-inspired structure, the Views should be objects, responsible for all presentation logic and each capable of managing multiple templates.

Yii 实际努力实现的 MVC 变体是 MVP:model-view-presenter.显着的区别在于 MVP 中的 View 是被动的(这不是哑模板"的同义词).你应该坚持下去.

The MVC variant which Yii actually strives to implement is MVP: model-view-presenter. The significant difference is that View in MVP is passive (which is not synonymous to "dumb template"). And you should stick to it.

我建议创建一个类来处理身份验证的表示.并且有 4 个模板:

I would recommend to create a class, that handles the representation of authentication. And have 4 templates:

  • layout - 基本的东西,不会改变的 html 部分
  • 登录 - 表单
  • 错误 - 好吧..猜猜它做了什么
  • 成功

然后你的新的和改进的视图将根据来自 User presenter 的输入来决定要组合哪些模板.

And then your new and improved view will decide, based on input from User presenter, which templates to combine.

...或者你将更多的呈现逻辑冷推到你的User控制器"上.

... or you cold push more presentation logic onto your User "controller".

这篇关于Yii Design - 如何拆分视图逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 21:19