Code Igniter 2 - 控制器/方法重写
如果用户未登录,我想覆盖现有控制器并调用身份验证/登录控制器,而不是保持浏览器 URI 不变。 (每个用户都必须登录才能使用该站点)
我尝试过预控制器挂钩,但还为时过早。此时 auth 库尚未实例化。 由于目标控制器已实例化,后控制器挂钩为时已晚。
编辑库的构造函数似乎也毫无意义,因为它已经创建了..所以我有点卡住了..
请问有什么想法吗? :)..
谢谢
If a user is not logged in I would like to override the existing Controller and call the auth/login one instead keeping the browser URI in tact. (Every user has to be logged in to use the site)
I have tried a pre controller hook, but its too early. The auth library has not been instantiated by this point.
The post controller hook is too late as the target controller has been instantiated.
Editing the constructor of the library seems pointless also, as it has already been created.. So i am a little stuck..
Any ideas please? :)..
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将覆盖默认控制器并在每个控制器中扩展。使用以下代码创建一个新文件:./app/core/MY_Controller.php:
然后,在每个控制器中使用此代码,登录用户应该可以访问该代码
I would override the default controller and extend in each controller. Create a new file: ./app/core/MY_Controller.php with the next code:
Then, use this code in each controller that you should be accessible by logged users
您是否考虑过将 URI 保存到 flashdata,将访问者重定向到另一个控制器(登录页面),然后将引荐来源网址放入隐藏的表单字段中?我正在使用 Codeigniter 对我的网站之一做同样的事情,并且效果很好。唯一的区别是在此过程中 URI 并不完整。
Have you considered just saving the URI to flashdata, redirecting the visitor to another controller (login page) and then just putting the referrer into a hidden form field? I'm doing the same thing with one of my sites using Codeigniter and it works fine. The only difference is that the URI is not intact during this process.
也许这个问题的答案对你来说是一样的? CodeIgniter 仅允许在登录时访问某些控制器
(如果是这样,请不要忘记将“function className()”替换为“function __construct()”,并将“parent::Controller();”替换为“父::__construct();”)
Perhaps the answer to this question will be the same for you ? CodeIgniter only allow access to certain controllers when logged in
(If so, don't forget to replace the "function className()" by "function __construct()" and "parent::Controller();" by "parent::__construct();")