• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
break up large controller file in to separate files, using 'includes'

I have a fairly large controller file with several blocks of code of 50 – 100 lines each. Id like to break them up into separate files and then include them in the main controller file line the classic include ('includes/someCode.php') – how can this be done? They aren't views so i cant use $this->load->view ... using $this->load->file doesnt work either.

using the latter, after getting some errors saying it couldnt find the file, i finally got the path right but the code isnt being included, even with true or false as the second parameter.

how can i break up my controller file in to smaller chuncks?

Those blocks you speak of are that controller methods or functions you are using throughout your code?

If it are controller methods you best split it up in different controllers instead of including then. For example controller blog has methods landingpage, detail, add and update you can split up the controller in blogfrontend with the methods landingpage and detail and the controller blogadmin with the methods add and update. Using the routes configuration you can make the url cleaner.

If the functions are used throughout the application you can create a helper or a library. and load that to use the functions.

If the chuncks of code are only used in that particular controller you can abstract it a bit and create a hidden method, prefixed with an underscore, and use that.

I agree with xwero, I've barely touched the include function since switching to CI (only for the occasional addon library). But technically you could use includes the way you used to, it's still PHP

You could always try a Modular approach Wink

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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