Welcome Guest, Not a member yet? Register   Sign In
Controller organization question
#1

[eluser]Shpigford[/eluser]
So I've got an "admin" area for all things related to site control/configuration and within that there are a ton of different controllers. So I'm wondering what you'd suggest is the best way to organize things.

Say I've got a page for managing categories with the functionality to add, edit, and delete categories.

In /controllers would you suggest I have something like this:

/controllers
--/admin
----/managecategories.php

And then managecategories would have add(), edit(), and delete()

Or would I just have /controllers/admin.php and then have managecategories() and do add, edit, and delete within that?
#2

[eluser]xwero[/eluser]
It depends on the size of the code. If you have a lot of different sections it's best to split up your admin controllers but if your sections are limited you can put them in one controller.
#3

[eluser]ekeretex[/eluser]
Go with having a managecategories Controller (and the add, edit, methods in it).
If you have a lot of different admin functions, the admin.php could get messy with the second option.
#4

[eluser]dawnerd[/eluser]
[quote author="ekeretex" date="1201007802"]Go with having a managecategories Controller (and the add, edit, methods in it).
If you have a lot of different admin functions, the admin.php could get messy with the second option.[/quote]

I actually prefer to put everything into one controller for my admin page, but make the methods very small. Just calling something in my admin library and then showing the view.
#5

[eluser]tonanbarbarian[/eluser]
i tend, where possible, to have a controller for each model
MVC has not definately way you should do things but it is often implemented as follows

table names are plurals because they contain many records
i.e. users
model names are singular of table names because they generally refer to just a single record
i.e. user
controllers are plurals of the model names because they generally manage mutliple records
i.e. users
methods are simple action names for what actions they perform for a controller or model

so i susually have urls something like the follows
admin/users/index
admin/users/add
admin/users/edit
admin/users/delete

These are examples of admin urls of course
Sometimes I will use further subfolders if there are logical groupings of controllers and model.
i.e. user, group, organisation might be grouped under an account folder for example
admin/account/users/index
admin/account/groups/add
admin/account/organisations/edit
etc

Overall though it really does not matter what you do, except that if someone else may one day have to modify your code then try to stick to just one overall pattern
#6

[eluser]Shpigford[/eluser]
So tonanbarbarian, you have a separate model for, more or less, each table in the database? Do you organize them in any way the same as how you'd organize your controllers or views? Or is it really just basically a model for each database table?
#7

[eluser]Michael Wales[/eluser]
I name all of my controllers the plural of my tables - there are a few obscure controllers out there, that don't pertain to a table - but this is a general rule of thumb.

All of my models are the singular of the table - since they refer to the data for a single object (the data that defines a user, or a blog post, etc).

My views are all placed within subfolders with the same name as the controller that calls them (except for the _global folder, which is used for header, footer, nav, etc).

So, a user registration form would be something similar to:
Controller: users->signup()
Model: user
View: users/signup.php

Finally, if I don't like the way the URL looks by default, I use routing. This would make the registration url: domain.com/signup
Code:
$route['signup'] = 'users/signup';
#8

[eluser]tonanbarbarian[/eluser]
it is really just a model for each database table.
the only issue then is deciding which model to put methods that return data from multiple tables. in that case i look to the primary table of the query and put the code in that model, or some times i might create a seperate model for the joined tables

if the table is used for many many relationships, ie it contains the id fields from 2 or more tables to join them then i do not always create a model for it

i also create models for other data sources. for example if i am going to be processing cvs or xml data in a known format then i will create a model to read and write to that data source. that allows my controller to access the xml data in a similar fashion to if it was a database table, via the model

i do not put my models into subfolders as that is not supported, but since the table names are unique it has not been an issue yet, however it would be nice to be able to put them into sub folders if i wanted to access multiple databases

i do the same thing with views as michael does, i.e. putting them into sub folders with the same name as the controller so they can be easily found. occasionally 2 controllers may share a view but not often




Theme © iAndrew 2016 - Forum software by © MyBB