简体   繁体   English

CodeIgniter-所有页面上的购物车

[英]CodeIgniter - Shopping Cart on all pages

I have to put a shopping cart on several pages on my ecommerce website (CodeIgniter, MVC Paradigm). 我必须在我的电子商务网站(CodeIgniter,MVC Paradigm)的几个页面上放入购物车。

I don't really want to call the model behind this widget in all my controllers. 我真的不想在所有控制器中都调用此小部件背后的模型。

I know that a solution may be calling the model in the autoloader, but, sometimes, i don't want to display the shopping cart. 我知道一个解决方案可能是在自动装带器中调用模型,但是有时我不想显示购物车。

So, the solution may be calling the model in the related view. 因此,解决方案可能是在相关视图中调用模型。 But, since it respect (i think) the MVC paradigm, i'm afraid that CodeIgniter is not really designed like that. 但是,因为它尊重(我认为)MVC范例,所以我担心CodeIgniter并非真的如此设计。

What is the best solution, for keeping a clear, simple and maintenable architecture ? 保持清晰,简单和可维护的体系结构的最佳解决方案是什么?

Thanks ! 谢谢 !

You can create a own controller and extend the CI controller and then load the model based upon the conditions. 您可以创建自己的控制器并扩展CI控制器,然后根据条件加载模型。

Here is the reference : http://ellislab.com/codeigniter/user-guide/general/core_classes.html 这是参考: http : //ellislab.com/codeigniter/user-guide/general/core_classes.html

通过ajax请求调用cartData怎么样?

Hook: you can try to create a Hook that reads/calculates shopping card info and set it to run 'post_controller' . 挂钩:您可以尝试创建一个读取/计算购物卡信息的挂钩,并将其设置为运行'post_controller' This way your hook will run after every controller, setting the cart information on session or a flash var to be accessible from your view. 这样,您的挂钩将在每个控制器之后运行,从而可以从您的视图访问会话或Flash变量上的购物车信息。

more info here: http://ellislab.com/codeigniter/user-guide/general/hooks.html 此处提供更多信息: http : //ellislab.com/codeigniter/user-guide/general/hooks.html

alternatively you can call helpers from your View, this is not recommended but it will work. 或者,您也可以从View调用帮助程序,不建议这样做,但是可以使用。

Place the query code for the cart data in a cart_helper.php and call it from your view as your would call any other function. 将购物车数据的查询代码放在cart_helper.php并像调用其他任何函数一样从您的视图中调用它。

Remember that your helper must be loaded at the time, so either autoload it or set a load on your controller __construct() 请记住,您的帮助程序必须同时加载,因此请自动加载它或在控制器__construct()上设置加载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM