dow58115
2019-02-08 08:14
浏览 70
已采纳

如何在Codeigniter中读取Controller中的任何方法之前检查条件?

How to check a condition before reading all the methods in a controller. After the condition is checked, if it is TRUE, then only the methods in the controller have to be accessible. If the condition returns false then it should be redirected to another controller. How can I achieve this?

Thanks in Advance.

图片转代码服务由CSDN问答提供 功能建议

如何在读取控制器中的所有方法之前检查条件。 检查条件后,如果为TRUE,则只能访问控制器中的方法。 如果条件返回false,则应将其重定向到另一个控制器。 我怎样才能做到这一点?

先谢谢。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongmeirang4679 2019-02-08 09:19
    已采纳

    Controller automatically runs construct every time it is initialized, before it runs any other methods, so you can use it to set conditions required to access other controller methods.

    Example:

    class ExampleController extends CI_Controller {
    
       public function __construct () {
    
           // use construct method of CI_Controller(the parent)
           // don't foget this because it wont work without it!
           parent::__construct();
    
    
           if(true) {
               //do the thing you want
           } else {
              // use the url helper to redirect to another page/controller
              $this->load->helper('url');
              redirect('another-page');
           }
    
       }
    
       public function index() {
          //display index page...   
       }
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题