每次在 CodeIgniter 中加载页面时运行一个函数
我以前只在 Asp.Net MVC 中制作过 Web 应用程序,您可以在 BaseController 上设置的“ActionFilter”中使用 OnResultExecuted 来在每次执行操作方法时运行一个方法(即基本上每次有人访问该中的任何页面时)应用)。
我如何在 CodeIgniter/PHP 中做同样的事情?
编辑:
我根据建议之一尝试使用 post_controller_constructor ,但这没有帮助:
$hook['post_controller_constructor'] = array(
'class' => 'PreController',
'function' => 'getIp',
'filename' => 'preController.php',
'filepath' => 'hooks'
);
我仍然得到 Undefined property: PreController::$input (我只是没有重命名所调用的类,如果目前仍称为 PreController)。
但事实仍然是我无权访问输入属性...显然我无权访问输入类,那么我该怎么做呢?我相信如果我在控制器中做了同样的事情那就没问题了,但是从钩子中呢?我该怎么做?
I have previously only made web applications in Asp.Net MVC, and there you can use OnResultExecuted in an "ActionFilter" set on the BaseController to run a method each time an action method is executed (i.e. basically each time anyone visits any page in the application).
How would I do the same thing in CodeIgniter/PHP?
EDIT:
I tried using post_controller_constructor instead, according to one of the suggestions, but that doesn't help:
$hook['post_controller_constructor'] = array(
'class' => 'PreController',
'function' => 'getIp',
'filename' => 'preController.php',
'filepath' => 'hooks'
);
I still get Undefined property: PreController::$input (I just haven't renamed the class called, that shouldn't matter if it is still called PreController for the moment).
But the fact remains I do not have access to the input property... Obviously I don't have access to the Input Class, so how do I do that? I belive if I had done the same thing in a Controller it would have been ok, but from a hook? How do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 CodeIgniter 挂钩: http://www.codeigniter.com/user_guide/general /hooks.html
您可以挂钩几个“事件”,请查看文档页面的底部。
回应评论:
重新阅读您的评论并编辑...似乎您假设您的钩子类是当前控制器 - 但事实并非如此。当前控制器是与 URI/路由映射匹配的任何控制器(例如,
site.com/users/view/1
将使用Users
控制器,而不是您的钩子处理程序PreController
类)。挂钩处理程序可以是一个不继承自 CI_Controller 的简单 PHP 类。在钩子处理程序中您需要做的第一件事是获取实际控制器,其中将包含输入引用和其他内容。
You can use CodeIgniter hooks for that: http://www.codeigniter.com/user_guide/general/hooks.html
There are several "events" you can hook into, check the bottom of the documentation page.
In response to the comment:
Re-read your comment and edit... it seems that you're assuming that your hook class is the current controller -- it isn't. The current controller is whatever matches the URI/route mapping (for example,
site.com/users/view/1
would be using theUsers
controller, and not your hook handlerPreController
class). The hook handler can be a simple PHP class not inheriting fromCI_Controller
.The first thing you need to do in the hook handler is to get hold of the actual controller, that will contain the Input reference and others.