Add $this->load->view to all functions? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: Add $this->load->view to all functions? (/showthread.php?tid=40958) |
Add $this->load->view to all functions? - El Forum - 04-24-2011 [eluser]Fredrik-s[/eluser] Hello, I'm totally new at using CodeIgniter and I have a question about the $this->load->view command. I have done a header and a footer, which I want to add to my code through the controller file. The code I am using is a auth library found here at codeigniter.com. I have read the documentation and what I understood is that I should only be in need to add the implementation of the header and footer in the index() function and after that all the pages which is going through this controller will show up with my header and footer. But this does'nt seems to work. If i add the code: $this->load->view('header'); $this->load->view('footer'); to all my functions it appears right with the header and footer implemented. But as it could be many different functions I'm wondering if I most add the header and footer code to all thoughts? Is'nt there a simple way that all the new functions I'm adding will have the header and footer in them per default? Code: //redirect if needed, otherwise display the user list Add $this->load->view to all functions? - El Forum - 04-24-2011 [eluser]davidbehler[/eluser] Possible solution would be to write your own function that you call instead of $this->load->view() in your controller. I would extend the CI_Controller class and put that function there: Create a file called MY_Controller in application/core with this content: Code: class MY_Controller extends CI_Controller { And now you only have to update your controllers to extend MY_Controller instead of CI_Controller and in your functions you would call $this->load_view('auth/login', $this->data); and remove the $this->load->view('header') and $this->load->view('footer') calls. Add $this->load->view to all functions? - El Forum - 04-24-2011 [eluser]Fredrik-s[/eluser] [quote author="waldmeister" date="1303655570"]Possible solution would be to write your own function that you call instead of $this->load->view() in your controller. I would extend the CI_Controller class and put that function there: Create a file called MY_Controller in application/core with this content: Code: class MY_Controller extends CI_Controller { And now you only have to update your controllers to extend MY_Controller instead of CI_Controller and in your functions you would call $this->load_view('auth/login', $this->data); and remove the $this->load->view('header') and $this->load->view('footer') calls.[/quote] Thanks for answer! I run in to a problem though. I have added the code you stated above into my /application/core -folder. Named the file MY_Controller.php. And in my auth.php file in the folder /application/controllers/ I have changed the following code on the top to extend the MY_Controller class: Code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); From this long I have come I got the following error message stating that it does'nt find the MY_Controller class Fatal error: Class 'MY_Controller' not found in /home/XXXXX/application/controllers/auth.php on line 8 Of course it can't find it because the file MY_Controller.php is in the core folder and not the controllers. How come this error shows up? Add $this->load->view to all functions? - El Forum - 04-24-2011 [eluser]davidbehler[/eluser] Are you using CI 2.X or 1.7? If you are using 1.7 then you have to put the MY_Controller file into application/library and have it extend Controller instead of CI_Controller. Add $this->load->view to all functions? - El Forum - 04-24-2011 [eluser]InsiteFX[/eluser] To use a MY_Controller in CI 2.0.+ from the core you need to include the following: Code: /* InsiteFX Add $this->load->view to all functions? - El Forum - 04-24-2011 [eluser]davidbehler[/eluser] Actually you don't. At least not that I'm aware of. I'm using CI 2.0 with MY_Controller in application/core and it works just fine without that function. Add $this->load->view to all functions? - El Forum - 04-24-2011 [eluser]Fredrik-s[/eluser] Thanks for the answers! I tried to add the code that InsiteFX told about in the file /application/config/config.php, added it to the bottom. I'm afraid that didn't help either. I'm using the latest version of CI, version 2.0.2. Any other suggestions on what could be wrong? Add $this->load->view to all functions? - El Forum - 04-24-2011 [eluser]InsiteFX[/eluser] You cannot do this Controller is a CI reserved word! Code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); InsiteFX Add $this->load->view to all functions? - El Forum - 04-24-2011 [eluser]Fredrik-s[/eluser] [quote author="InsiteFX" date="1303697189"]You cannot do this Controller is a CI reserved word! Code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); InsiteFX[/quote] Thanks for answer. I'm using this authentication library http://codeigniter.com/wiki/Ion_Auth_-_Lightweight_Auth_System/ Strange that it is coded with reserved name. I think I will change library. Add $this->load->view to all functions? - El Forum - 04-24-2011 [eluser]InsiteFX[/eluser] I would re-download Ion Auth, It sounds like you have an older version! InsiteFX |