【发布时间】:2017-06-18 18:59:50
【问题描述】:
在过去,我使用了一个专有框架,该框架有点遵循类似于 View Controller 的 Objective-C 方案。
基本上,我能够在一个控制器中实例化另一个控制器并将一些值传递给它,例如产品数组,然后将控制器的引用注入到我的视图中,并在我想要的任何时候通过发出:$controllerReference->render();
这在许多情况下可能很有用,例如。如果我有一个负责渲染目录的控制器,我只需将一个包含我想查看的所有项目的数组传递给它,它会进行分页并自行显示项目......
例子:
在\UI\Homepage\Controller.php(负责主页的控制器):
// Instantiate a ProductDisplay Controller
$controllRef = new \UI\ProductDisplay\Controller();
$controllRef->setProducts( ... ); // Inject the product array
// Load the current view for this controller and pass it a reference for the ProductDisplay controller
$this->loadSelfView(["controllRef" => $controllRef]);
在\UI\Homepage\View.php(之前加载的视图):
// some html of the view
$controllRef->render(); // Render the ProductDisplay view here!
// some other html
这个功能应该如何在 Laravel 中实现?从我一直在阅读的内容来看,Laravel 试图避免这种行为,为什么?有什么解决办法?
谢谢。
【问题讨论】:
-
旁注:这也可以在带有 View Cells book.cakephp.org/3.0/en/views/cells.html 的 CakePHP 中完成。 Laravel 中有类似的东西吗?