CodeIgniter Forums
Divide controllers in folders, good practice? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Divide controllers in folders, good practice? (/showthread.php?tid=36734)



Divide controllers in folders, good practice? - El Forum - 12-13-2010

[eluser]sohei[/eluser]
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:
-blog
-portfolio
-forum

And put into these folders something like that:
-blog/list_posts.php
-blog/add_post.php
-blog/add_comment.php
-portfolio/list_works.php
-portfolio/add_works.php
-forum/list_subjects.php
-forum/add_topic.php
-forum/add_post.php
-...

Hope that's clear enough.
soh8


Divide controllers in folders, good practice? - El Forum - 12-13-2010

[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.


Divide controllers in folders, good practice? - El Forum - 12-13-2010

[eluser]LuckyFella73[/eluser]
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:
controller:
- "blog.php"

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

and the same with you other controllers.


Divide controllers in folders, good practice? - El Forum - 12-13-2010

[eluser]sohei[/eluser]
Ok,
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?

Thanks!


Divide controllers in folders, good practice? - El Forum - 12-13-2010

[eluser]LuckyFella73[/eluser]
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.


Divide controllers in folders, good practice? - El Forum - 12-13-2010

[eluser]sohei[/eluser]
Thanks, it's more clear now.

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


Divide controllers in folders, good practice? - El Forum - 12-13-2010

[eluser]LuckyFella73[/eluser]
rules.php he he ... yes I meant routes.php


Divide controllers in folders, good practice? - El Forum - 12-13-2010

[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”.

-admin/
--clubs.php
--events.php
-clubs.php
-events.php
-news.php

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


Divide controllers in folders, good practice? - El Forum - 12-13-2010

[eluser]sohei[/eluser]
Ok ok I see, thank you very much for the example!