Kohana 在另一个控制器中实例化一个控制器函数
我想在另一个控制器中实例化一个 kohana 控制器函数,该函数从购物篮中获取内容。
换句话说,在我拥有的一个控制器(在basket.php中)中
public function action_index()
{
$basket = $this->basket;
$contents = $basket->contents->find_all();
$this->view->basket = $basket;
$this->view->contents = $contents;
}
,我想在其他控制器sale.php中调用此函数,因为我希望产品已经存在于购物篮中,并在列表中以某种方式进行标记。 我想在控制器 sale.php 中调用此函数,其中实际列出了产品。
我在 sale.php 中有
公共函数 action_browse($id, $category_id = NULL) {
$sale = Model::factory('sale')->active()->find($id);
$basket_content = $this->user->get_basket($sale);
if ( ! $sale->loaded())
{
throw new Kohana_Request_Exception('Sale not found.');
}
if (isset($category_id))
{
$category = $sale->categories->find($category_id);
if ( ! $category->loaded())
{
throw new Kohana_Request_Exception('Category not found.');
}
$products = $sale->products->category($category_id)->find_all();
$this->view->category = $category;
}
else
{
$products = $sale->products->find_all();
}
谢谢!
i want to instantiate a kohana controller function that gets the contents from a shopping basket, in another controller.
in other words, in one controller i have (in basket.php)
public function action_index()
{
$basket = $this->basket;
$contents = $basket->contents->find_all();
$this->view->basket = $basket;
$this->view->contents = $contents;
}
and i want to call this function in other controller, sale.php because i want that the products already existent in the basket, to be marked somehow in the listing.
i want to call this function in the controller sale.php, where the products are actually listed.
i have in sale.php
public function action_browse($id, $category_id = NULL)
{
$sale = Model::factory('sale')->active()->find($id);
$basket_content = $this->user->get_basket($sale);
if ( ! $sale->loaded())
{
throw new Kohana_Request_Exception('Sale not found.');
}
if (isset($category_id))
{
$category = $sale->categories->find($category_id);
if ( ! $category->loaded())
{
throw new Kohana_Request_Exception('Category not found.');
}
$products = $sale->products->category($category_id)->find_all();
$this->view->category = $category;
}
else
{
$products = $sale->products->find_all();
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不建议像这样将控制器相互耦合。这样你的应用程序就会变得非常束缚,并且会变得一团糟。
尝试尽可能地解耦事物。例如,您可以通过将逻辑放入控制器可以获取它的模型中来做到这一点。
在理想情况下,您的控制器根本不会处理那么多数据,但它主要会尝试根据您的视图进行建模。
当前的首选方法是使用视图类,将数据从模型传递到视图。这样,视图将主要独立存在,并且与控制器几乎没有连接,这使得重用视图变得更容易。
想到的 Kohana-3 的另一个功能是使用 Request 类来执行额外的操作内部请求,这将允许您以解耦的方式重用控制器的输出。
您可以这样使用它:
其中响应是执行请求的渲染输出,您可以在视图中输出。此方法适用于在每个页面上呈现的购物篮。
It's not advised to couple controllers to each other like this. This way you application would be very tied, and it would become a mess.
Try to decouple things as much as you can. You can do this for example by putting the logic in model where the controllers can fetch it.
In an ideal situation, you controller wouldn't be handling that much data at all, but it would mainly trying you models to your views.
The current preferred method would be to use view classes which pass the data from your models to your views. This way, a view would stand mostly on it's own, and has very little connection to the controller, which makes it easier to reuse your views.
Another feature of Kohana-3 that would come to mind is to use the Request class to execute an additional internal request, which would allow you to reuse the output of a controller in a decoupled way.
You would use it like this:
Where response is that rendered output from the executed request, which you can output in your view. This method would be appropriate for a basket which gets rendered on every page.