Naming conventions for controllers |
[eluser]xtremer360[/eluser]
I was looking at this new post on the forum and it got me thinking. http://ellislab.com/forums/viewthread/237085/ What is the best practice when naming functions that are the same across most controllers. For example lets say I want to name a function that shows all of my users. I would call it list_users(). This function would be responsible for running the get_all function to retrieve all of the users from the database. Another might be the delete_user which would be responsible for running the model function of deleting the specific user. Any ideas on this?
[eluser]greedyman[/eluser]
Function's name is relative with controller's name, example: shopping controller has add_to_cart, remove_from_cart, clear_cart, ... function. The same with models.
[eluser]ivantcholakov[/eluser]
@xtremer360 Have an idea of a nice link system first. Examples: Managing news, a single controller: http://mysite.com/admin/news - show the list of news (index method) http://mysite.com/admin/news/add http://mysite.com/admin/news/edit/1 http://mysite.com/admin/news/delete/1 http://mysite.com/admin/news/down/1 - reordering http://mysite.com/admin/news/up/2 - reordering Managing products - another way with two controllers, named in singular/plural: http://mysite.com/admin/products - show the list of products (index method) http://mysite.com/admin/product - add a product http://mysite.com/admin/product/1 - edit a product id=1 http://mysite.com/admin/products/delete/1 http://mysite.com/admin/products/down/1 http://mysite.com/admin/products/up/2 Managing products with categorization: http://mysite.com/admin/products - show the tree of categories (index method) http://mysite.com/admin/products?category=1 - show the list of products within the category 1 (the same index method, but loads an another view) http://mysite.com/admin/product?category=1 - add a product within the category 1 http://mysite.com/admin/product/1 - edit a product id=1 http://mysite.com/admin/products/delete/1 http://mysite.com/admin/products/down/1?category=1 http://mysite.com/admin/products/up/2?category=1 Adding pagination (the page number could be as a segment or as a get-parameter): http://mysite.com/admin/products/page/2 (index method, without categorization implemented) http://mysite.com/admin/products?category=1&page=2 (index method) The public side: http://mysite.com/category/1 - show the list of products within the category 1 (index method) http://mysite.com/category/1?page=2 - the same with pagination (index method) Later, on top of this link structure you may add slug support: http://mysite.com/bracelets (a preferable alternative of http://mysite.com/category/1 if the unique slug has been entered/generated) http://mysite.com/bracelets?page=2 - the same with pagination Show a product: http://mysite.com/product/1 (index method) http://mysite.com/bracelet-model-708 (this link is generated as an alternative to http://mysite.com/product/1 when the unique slug has been entered/generated) In the public side use helper functions for link generation, so you be able to switch easily to permalinks. To stress again, the common rule is: First invent your nice (and SEO-friendly in the publuc side) system of links, and then try to implement a system of (subdirectories), controllers, and methods. Some additional entries might be needed in application/config/routes.php. An additional suggestion: keep the controllers as skinny as it is possible, so the logic to be obvious. It is a long and time-consuming story. Examine the structure of PyroCMS (I did so) and you will see in details how to implement a similar link-system. |
Welcome Guest, Not a member yet? Register Sign In |