简体   繁体   English

通过AJAX调用控制器的方法之一时,CodeIgniter是否调用控制器的构造函数?

[英]Does CodeIgniter call a controller's constructor when calling one of its methods via AJAX?

I need to know whether I should re-call my helper redirectIfNotLoggedIn() inside ajaxFunction, since it's already in the constructor: 我需要知道是否应该在ajaxFunction中重新调用我的助手redirectIfNotLoggedIn() ,因为它已经在构造函数中:

class Group extends Controller {
    function Group() {  
        parent::Controller();
        redirectIfNotLoggedIn();
    }
    function ajaxFunction() {
        //I am called via AJAX
        //Do I need to call redirectIfNotLoggedIn() again?
        //Or is the constructor called whenever I access this function via AJAX?
    }
}

Any help would be appreciated :) 任何帮助,将不胜感激 :)

An Ajax request is nothing more than an HTTP request to your CodeIgniter application, except that it's sent by JavaScript. Ajax请求只不过是对您的CodeIgniter应用程序的HTTP请求,只是它是由JavaScript发送的。 So your controller will still be instantiated and run as usual. 因此,您的控制器仍将被实例化并照常运行。

Your controller's constructor will be invoked and redirectIfNotLoggedIn() called, so you don't need to call it again. 将调用控制器的构造函数并调用redirectIfNotLoggedIn() ,因此您无需再次调用它。

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

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