0

There's a part of a code (query) that will be required in all controllers, they will be passed into views for display.

Can I know is there anyway to declare them in just a single file so that I can reference them directly from my view? Without declaring them in each controller's _construct.

I'm using codeigniter3, here's a sample code:

MainController.php

public function index(){
      $data['userCampaign'] = $this->Usermodel->getCampaign();
}
Karl Wong
  • 594
  • 2
  • 7
  • 23

3 Answers3

1

Create default controller in your project which extends CI_Controller and your all controller extends new controller and in __construct(); function of your new controller you can add this code.

Keval Rathi
  • 978
  • 6
  • 21
  • You mean create a default controller and then put the query in it's _constructor() function, then my MainController would extend this default controller? But I will need to use the code in each controllers too right? I'm quite confused... – Karl Wong Sep 16 '15 at 11:22
  • No, as you add this code to common controller `__construct();` function it will be available in all controller – Keval Rathi Sep 16 '15 at 11:30
  • @KarlWong have you test ? – Keval Rathi Sep 16 '15 at 11:40
  • I've found another solution which is creating a helper but your solution would work as well. – Karl Wong Sep 16 '15 at 12:02
0

Declare this function as protected in parent controller class.

Nitromoon
  • 378
  • 1
  • 2
  • 11
0

No. I don't know there is method like that.

You want call the function in __construct() or you have to declare function in controller and call it back. $this->check_session()

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • So there's no global file which I can autoload and then reference them without the controller? I mean I want a variable...which is obtained through a query. – Karl Wong Sep 16 '15 at 11:19
  • No. have to do it manually. And read this too [Where to place global functions in codeigniter](http://stackoverflow.com/questions/10831213/where-to-place-global-functions-in-codeigniter) – Abdulla Nilam Sep 16 '15 at 11:20
  • and read this too (CodeIgniter global function)[http://stackoverflow.com/questions/9237112/code-igniter-global-function] – Abdulla Nilam Sep 16 '15 at 11:21