问题描述
我使用窗口小部件作为部分视图,它有自己的控制器(所以它自己的动作),它几乎放置在所有页面。我通过HMVC实现这个渲染,这只是伟大的。
I use the word widget as a partial view that have its own controller (so its own actions) and it is placed in almost all pages. I implement the rendering of this via HMVC, that is just great.
现在,问题是小部件本身执行动作。想想购物车小部件。该小部件放置在所有页面中,以便用户可以一直看到他/她的东西。和小部件有相关的动作,例如:RemoveItem,RefreshCart,ApplyDiscountCoupon等这样的动作应该触发一个按钮或链接,链接应该是类似的(在HMVC):
Now, the problem is that the widget itself execute actions. Think about a shopping cart widget. That widget is placed in all pages so the user can see his/her stuff all along. And the widget have actions that are related to it, for instance: RemoveItem, RefreshCart, ApplyDiscountCoupon, etc. Such actions should trigger by a button or link and the link should be something like (in HMVC):
<a href='<?site_url()?>/cart/cart/removeitem/the_item_id'>Remove this item</a>
好的。现在用户点击该链接,购物车模块和购物车控制器加载并执行操作,操作应如下所示:
Ok. Now the user clicks that link, the cart module and cart controller are loaded and the action is executed, the action should look something like:
function removeitem($itemid)
{
// remove the item from db
...
// "load the view" ???
}
正如你可以看到的,我的问题是如何加载视图在HMVC模块。事情是,如果我只加载购物车视图,它将只显示我的车,我不能只是重定向或加载主页面,因为它可以是任何主页面,即:购物车可以在任何主页面(选择产品,继续购买,查看产品详情,结算信息,结帐等)。 :/
As you can see, my question is how to load the view in a HMVC module. The thing is that if I only load the cart view, it will only show my cart, and I can’t just redirect or load the main page because it can be any main page, that is: the cart could be in any main page (select product, keep buying, see product details, billing info, checkout, etc). :/
另一件事:要求是我不能在这个项目中使用AJAX。
Another thing: a requirement is that I can’t use AJAX in this project.
你知道HMVC如何处理这个问题吗?
Do you know how HMVC handle this?
提前感谢。
推荐答案
好的。没有运气与社区。然而,我发现了一个解决方法。希望对某人有帮助。
Ok. No luck with the community. Nevertheless I found a workaround. Hope would be helpful to someone.
HMVC没有自然的解决方案。所以我决定使用这个解决方法:
HMVC doesn't have a natural solution to this. So I decided to use this workaround:
-
在每个主控制器(即没有窗口小部件,此会话中的当前网址(/ controllers / keep_buying.php)
Into each main controller (that is, no widget, no partial view) I grab the current url in session this way (/controllers/keep_buying.php):
class Keep_buying extends Controller
{
function Keep_buying()
{
parent::Controller();
$this->session->set_userdata('main_uri', uri_string());
}
...
}
在我的部分视图窗口小部件(HMVC模块视图)我有一个正常的链接到我的窗口小部件控制器(/ modules / cart / views / cart_show.php):
Then in my partial view widget (HMVC module view) I have a normal link to my widget controller (/modules/cart/views/cart_show.php):
<a class="button" href="cart/cart/additem">Add Item</a>
At the controller action (HMVC module controller action) I retrieve the current main page, do stuff and then redirect to that page, that implicitly will get into my widget flow (due to HMVC).
class Cart extends Controller
{
...
function additem()
{
$to_redirect = $this->session->userdata('main_uri');
// add-item work stuff...
redirect($to_redirect);
}
}
那就是。这不是理想的方法IMHO,但工作。
That is. Is not the ideal approach IMHO, but works.
这篇关于HMVC和动态小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!