在控制器之间传递值 [英] Passing values between controllers

查看:20
本文介绍了在控制器之间传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Yii.我有一个测试开发,其中包含许多表(员工、个人详细信息、地址).我对 MVC 的理解使我几乎将这些视为独立的行星,其中每个 (MVC) 组件在该世界中都扮演着明确定义的角色.

I'm learning Yii. I have a test development which contains a number of tables (employee, personalDetails, address). My understanding of MVC leads me to see these almost as individual planets, where each (MVC) component plays a clearly defined role within that world.

我有一个开始困扰我的问题,因为我现在想在这些世界之间传递数据和计算请求.我遇到了一些关于如何做到这一点的帖子,但它们看起来更像是黑客"而不是规定"的做法.我显然不想养成坏习惯.这种过程显然是任何开发的根本要求,因此想就此寻求一些指导.

I have a question that’s starting to bug me because I now want to pass requests for data and calculations between these worlds. I have come across a few postings on how to do this, but they appear more like "hacks" than "prescribed" practises. I’m obviously keen not to pick up bad habits. This kind of process is obviously a root requirement of any development so would like to ask for some guidance on this.

一个具体的例子是返回一个包含奖金 > 100,000 美元的员工的视图(例如,员工控制器要求personalDetails 控制器计算{goss pay + bonuses – tax} 并返回所有适当的实例,然后查找并返回相关员工).

A specific example would be to return a view of employees who have take home salaries > $100,000 including bonuses ( e.g. employee controller asks personalDetails controller to calculate {goss salary + bonuses – tax} and return all appropriate instances, it then looks up and returns the relevant employees).

那么我是在personalDetails中创建一个函数并从员工控制器内部调用它,或者这种事情应该放在扩展中......还是有另一种方法?

So do I create a function in personalDetails and call it from inside employee controller, or should this kind of thing go in an extension ... or is there another approach?

感谢您的指导

推荐答案

对于封装的自管理视图部件使用 widgets.对于上述情况,您可以使用可配置的 treshhold 创建小部件.

For encapsulated self managed view parts use widgets. For above case you could create widget with configurable treshhold.

如果您必须要求不同的控制器来计算某些东西,这是一种不好的做法.将此类计算放在模型中.模型可重用,视图可以重用,但是控制器应该只响应动作并将数据绑定到视图.

If you have to ask different controller to calculate something it is a bad practice. Place such calculations in model instead. Model is highly reusable, view can be reused, however controller should only respond to action and bind data to view.

胖模型瘦控制器,明智的观点.

这是一些草稿代码:

首先使用任何需要的计算创建模型:

First create model with any needed calculations:

class Employee extends CActiveRecord
{
    public function getTotalSalary()
    {
        // Do any calculations here
        // ...
        return $salary;
    }
}

然后你可以在控制器中重用它:

Then you can reuse it in controllers:

class FirstController extends CController
{
    public function actionPersonDetails()
    {
        $model = $this->_loadModel();

        // By assigning this you will have getTotalSalary() available in view
        $this->render('personDetails', ['model' => $model]);
    }
}

class SecondController extends CController
{
    public function actionViewSallary()
    {
        $model = $this->_loadModel();

        // Also here you will have getTotalSalary() available in view
        $this->render('viewSallary', ['model' => $model]);
    }
}

对于需要独立创建小部件的更复杂的场景:

And for more complex scenarios where you need something standalone create widget:

class EmployeesWidget extends CWidget
{
    public $minSalary = 0;

    private $_data = null;

    public function init()
    {
        $this->_data = new CActiveDataProvider(/* Criteria here with $this->minSalary as param */);
    }

    public function run()
    {
        $this->render('employeesWidget', ['data' => $this->_data]);
    }
}

然后您可以在任何视图中轻松使用它,甚至在其他小部件中:

Then you can easy use it in any view, even in other widgets:

$this->widget('path.to.EmployeesWidget', [
    'minSallary' => 10000
]);

这篇关于在控制器之间传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆