• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Divide controllers in folders, good practice?

Hi all,

I was wondering if it was a good practice to divide controllers into folders.. For example, for a website containing a blog, a portfolio and a forum.

In the /controllers folders it good to create 3 folder named:

And put into these folders something like that:

Hope that's clear enough.

[eluser]Thomas Edwards[/eluser]
Ideally not. Each of the functions you’ve described as separate file should be functions within one file.

ie All the blog functions should be in blog.php. You can more easily share models and views this way.

When you have too many controllers you can save them into different
folders - that's no problem.

In your case I would recommend to set up a controller for every area
and add the needed methods.

For example:
- "blog.php"

containing the methods:
- "list_posts"
- "add_posts"
- "add_comment"

and the same with you other controllers.

So LuckyFella, for my example you recommend me to not use folders as Thomas said. But for a larger project, it's ok to divide file into different folder?


It can be "nicer" to group some controllers to not loose the overview maybe.
The disadvantage is that you will have to set up rules in rules.php.
Otherwise CI don't find your controllers.

But generally it's more or less a personal taste. On a site with "harcore-traffic"
it could be that routing turns out to slow the site down a bit but I can't say
how much. In most cases you don't need to seperate controllers into different folders.
It's more often that you need to seperate your view-files. But for doing that you
don't need to set up a route.

Thanks, it's more clear now.

PS: you meant "set up rules in routes.php", eh?

rules.php he he ... yes I meant routes.php

[eluser]Thomas Edwards[/eluser]
I can give you an example of a project I’m working on. It’s a webapp for students’ unions. I have events.php, clubs.php and news.php. Each one is quite complex, but they remain in one file. I have one folder called admin, and have an events.php and clubs.php in the admin folder. News is edited on the front-end, and so are basic tasks for events and clubs, but more advanced things (such as setting up membership for a club, or creating a ticket for an event) are handled by files in the admin folder. The files in the admin folder also require an extra level of security, you have to be logged in and made an “admin”.


There’s actually 48 controllers in total, but I’ve simplified it here.

Ok ok I see, thank you very much for the example!

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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