CodeIgniter:在控制器内加载控制器 [英] CodeIgniter: Load controller within controller

查看:209
本文介绍了CodeIgniter:在控制器内加载控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 home 控制器,其中有一个索引操作,显示一组特色产品。但是,产品通过产品控制器进行管理,包括专有模型和视图。

I have a home controller with an index action that displays a set of featured products. However, the products are managed through a product controller including a proprietary model and views.

如何访问<在 home 控制器中索引操作中的实例产品将无法工作,因为类未在运行时加载,并且CodeIgniter不提供动态加载控制器的方法。将产品类放入库文件并不真正有效。

How do I access product information from within the index action in the home controller? Instancing product won't work as the class isn't loaded at runtime and CodeIgniter doesn't provide a way to dynamically load controllers. Putting the product class into a library file doesn't really work, either.

插入索引视图中的产品视图(填充有由产品控制器处理的数据)。我正在运行CodeIgniter 2.0.2。

To be precise, I need the product views (filled with data processed by the product controller) inserted in the index view. I'm running CodeIgniter 2.0.2.

推荐答案

如果你有兴趣,有一个成熟的包可以添加到您的Codeigniter项目,将处理这个:

If you're interested, there's a well-established package out there that you can add to your Codeigniter project that will handle this:

https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/


模块化扩展使CodeIgniter PHP框架模块化。模块是独立组件的组,通常是模型,控制器和视图,安排在应用程序模块子目录中,可以放入其他CodeIgniter应用程序。

Modular Extensions makes the CodeIgniter PHP framework modular. Modules are groups of independent components, typically model, controller and view, arranged in an application modules sub-directory, that can be dropped into other CodeIgniter applications.

好吧,所以很大的变化是,现在你将使用一个模块化结构 - 但对我来说这是可取的。我现在使用CI大约3年了,不能想象没有模块化扩展的生活。

OK, so the big change is that now you'd be using a modular structure - but to me this is desirable. I have used CI for about 3 years now, and can't imagine life without Modular Extensions.

现在,这里的部分处理直接调用控制器渲染视图分:

Now, here's the part that deals with directly calling controllers for rendering view partials:

// Using a Module as a view partial from within a view is as easy as writing:
<?php echo modules::run('module/controller/method', $param1, $params2); ?>

这就是它的全部。我通常使用此来加载一些小部件,例如:

That's all there is to it. I typically use this for loading little "widgets" like:


  • 活动日历

  • 新闻文章

  • 电子报注册表

  • 投票

  • Event calendars
  • List of latest news articles
  • Newsletter signup forms
  • Polls

通常我为每个模块构建一个窗口小部件控制器,并仅用于此目的。

Typically I build a "widget" controller for each module and use it only for this purpose.

当我开始使用Codeigniter时,您的问题也是我的第一个问题。我希望这可以帮助你,即使它可能有点比你想要的更多。我已经使用MX从来没有回头。

Your question was also one of my first questions when I started with Codeigniter. I hope this helps you out, even though it may be a bit more than you were looking for. I've been using MX ever since and haven't looked back.

请务必阅读文档,并在Codeigniter论坛上查看关于此包的大量信息。享受!

Make sure to read the docs and check out the multitude of information regarding this package on the Codeigniter forums. Enjoy!

这篇关于CodeIgniter:在控制器内加载控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆