MVC - 我可以在类控制器中调用多个(或多个)控制器吗?

发布于 2024-10-11 14:47:16 字数 269 浏览 4 评论 0原文

对于用php编写的项目,我可以在类控制器中调用多个(或多个)控制器吗? http://img192.imageshack.us/img192/7538/mvc03.gif

问:我需要从另一个控制器调用一个操作...如果我确实喜欢上面的图片,我是否不道德?

谢谢, 维尼修斯。

For projects written in php, can I call more than one (or multiple) controller in class controller? Example in http://img192.imageshack.us/img192/7538/mvc03.gif

ASK: I need to call an action from another controller... And if I do like the picture above, I'm being out-ethics?

Thanks,
Vinicius.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爱你不解释 2024-10-18 14:47:16

我确信无论您使用哪种框架,您都可以做您想做的事情。如果由于某种原因您无法在本机上做到这一点,那么您可以根据需要扩展您的框架。

话虽如此,我个人不喜欢一个控制器调用另一个控制器的想法。如果仅从理论角度来看,它似乎在某种程度上打破了 MVC 范式。我可能会做的是构建一个包含所需功能的库类,然后让两个控制器将该类实例化为成员并调用所需的函数。

例如,使用 CodeIgniter:

libraries/MyLib.php:

class MyLib
{
    public function MyFunc()
    { /* do whatever */ }
}

controllers/ControllerA.php:

class ControllerA extends Controller
{
    public function index()
    { 
        $this->load->library('MyLib');
        $this->mylib->MyFunc();
    }
}

controllers/ControllerB:

class ControllerB extends Controller
{
    public function index()
    {
        $this->load->library('MyLib');
        $this->mylib->MyFunc();
    }
}

I'm sure that you can do what you want with whichever framework you're using. If you can't do it natively for whatever reason, then you can extend your framework as required.

Having said that, I personally don't like the idea of a controller calling another controller. It seems to somewhat break the MVC paradigm if only from a theoretical standpoint. What I might do instead is build a library class that contains the functionality required and then have both controllers instantiate that class as a member and call the functions required.

For example, using CodeIgniter:

libraries/MyLib.php:

class MyLib
{
    public function MyFunc()
    { /* do whatever */ }
}

controllers/ControllerA.php:

class ControllerA extends Controller
{
    public function index()
    { 
        $this->load->library('MyLib');
        $this->mylib->MyFunc();
    }
}

controllers/ControllerB:

class ControllerB extends Controller
{
    public function index()
    {
        $this->load->library('MyLib');
        $this->mylib->MyFunc();
    }
}
命比纸薄 2024-10-18 14:47:16

道德外?无论如何……回到现实。

是的,一个控制器可以调用另一个控制器的操作。例如,在 cakePHP 中,此功能是通过 requestAction 提供的(

// pass uri to request action and receive vars back
$ot3 = $this->requestAction('/stories/xenu');

如果您自己推出) ,如何实现它的细节将非常具体于您的框架。

out-ethics? Anywhose... back to reality.

Yes, a controller can call another controller's action. For instance, in cakePHP, this functionality is afforded via requestAction

// pass uri to request action and receive vars back
$ot3 = $this->requestAction('/stories/xenu');

If you're rolling your own, the details of how to implement it will be very specific to your framework.

余罪 2024-10-18 14:47:16

然后你需要修改框架,找到控制器发布的地方并在那里添加你的第二个控制器。

你正在使用什么框架?

then you need to modify framework, find place where controller is lounched and add there your second controller.

what framework you are using?

真心难拥有 2024-10-18 14:47:16

您可以按照您想要的方式进行操作。如果您不愿意,则不必使用 MVC。然而,在 MVC 中,您实际上一次应该只有一个控制器处于活动状态。您可能需要多个视图或模型,而不是另一个控制器。加载网站的菜单和页脚的页眉和页脚视图没有任何问题。

You can do it any way that you want. You don't have to use MVC if you don't want to. However, in MVC you really should only have one controller active at a time. You probably want multiple Views or Models, not another Controller. There is nothing at all wrong in loading, say, a header and footer view for the menu and footer of the site.

软甜啾 2024-10-18 14:47:16

如果您正在构建另一个控制器,然后感觉您需要访问先前控制器的功能才能访问其功能(因为它与特定/所需的模型一起工作),那么您为后者开发的模型可能需要重构。简单来说,你的目标模型可能做得太多了。打破它。

您试图通过调用已开发的控制器的方法来避免重复(DRY),但这样做会在两个控制器之间创建紧密耦合!如果借用控制器发生变化,则会对借用控制器产生影响。不好,琼斯博士。

If you are building another Controller, then feel that you need to access the functionality of a previous Controller to access its functionality (because it works with a specific / desired Model), then the Model you developed for the latter probably needs to be refactored. IN plain speak, your target Model may be doing too much. Break it up.

You are trying to avoid repeating yourself (DRY) by using calling the methods of a Controller that has already been developed, but in doing so your are creating TIGHT coupling between both controllers! If something changes in the borrowed controller, it will have an effect on the borrowing controller. Not good, Dr. Jones.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文