code refactor help

#1
[eluser]sasori[/eluser]
hi, it seem like I am repeating this lines of code all the time in
each of my functions inside the controller

Code:
$data['base'] = $this->config->item('base_url');
$data['css'] = $this->config->item('css');
$data['title'] = "the title here";

is there a way wherein one can actually put this on a separate file and just call the file in the controller ? if so, how ?

#2
[eluser]theprodigy[/eluser]
well, the base_url can be called by
Code:
base_url()

as for the other items, you could put them into a file, and then just include it like you would with any php application
Code:
include('path/to/file');
but you would need to include it in every function in your controller, unless you include it in the constructor, and set class level variables.

#3
[eluser]sasori[/eluser]
I think I have read a book where I saw the author actually ripped off all those lines and placed them
in a separated file and save them inside the config folder, i just can't remember how he called the file
do you sir(s) have any idea how to do it ? ( actually can't remember the book ). I asked it here for a reminder for me hehe

#4
[eluser]Colin Williams[/eluser]
When you create a controller in CI, you extend the base class Controller, inheriting all of it's methods and properties. So, if you want something to happen in every controller you create, add it to the class every Controller extends. Two ways to do this. One, copy system/libraries/Controller.php to application/libraries/Controller.php and put your "global" code in the constructor. Two, and this one might be more future-proof, create a file MY_Controller.php in application/libraries/. In that file, create a class MY_Controller that extends Controller. Then, all of your controllers will extend MY_Controller.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.