CodeIgniter: global variables in a controller

前端 未结 5 1238
一整个雨季
一整个雨季 2020-12-24 13:56

I\'m a very newbie on CodeIgniter, and while I go on I run into problems that, in the procedural coding, were easy to fix

The current issue is: I have this controlle

5条回答
  •  感动是毒
    2020-12-24 14:36

    What you can do is make "class variables" that can be accessed from any method in the controller. In the constructor, you set these values.

    class Basic extends Controller {
        // "global" items
        var $data;
    
        function __construct(){
            parent::__construct(); // needed when adding a constructor to a controller
            $this->data = array(
                'title' => 'Page Title',
                'robots' => 'noindex,nofollow',
                'css' => $this->config->item('css')
            );
            // $this->data can be accessed from anywhere in the controller.
        }    
    
        function index(){
            $data = $this->data;
            $data['my_data'] = 'Some chunk of text';
            $this->load->view('basic_view', $data);
        }
    
        function form(){
            $data = $this->data;
            $data['my_other_data'] = 'Another chunk of text';
            $this->load->view('form_view', $data);
        }
    
    }
    

提交回复
热议问题