Best Practice Regarding Controllers / Site Structure - 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: Best Practice Regarding Controllers / Site Structure (/showthread.php?tid=23875) |
Best Practice Regarding Controllers / Site Structure - El Forum - 10-24-2009 [eluser]JamesTaylor[/eluser] OK, so i have almost completed building my 1st site using codeigniter and an MVC structure. The site is only a small one for a local tooling company and i have used it as a live project in order to learn CodeIgniter. The site's structure is broken down as follows: Home Page - 1 page About Us - 1 page New Tooling - 6 sub pages Indexable Tooling Milling & Drilling Work Holding Spindle Tooling Threading Tools Inspection & Metrology Used Tools - 1 page Offers - 1 page Contact Us - 1 page What is considered the best practice for use of controllers in the above the example? I have developed the site using a single controller called 'structure' and defined each page within it. I did it this way as my site is only small it was easy to have all the controller functions in place. However i am now thinking that perhaps the appropriate way would be to have a controller for each main section i.e Home / AboutUs / New Tooling / Used Tooling / Offers / Contact Us If i did as outlined above how would i deal with the sub pages of the New Tooling section? would i include these as sub functions in the New Tools controller or should each sub page have its own controller? Thanks guys, looking forward to your advice! James Best Practice Regarding Controllers / Site Structure - El Forum - 10-24-2009 [eluser]jedd[/eluser] Hi James, If the controller is under a thousand lines, I'd stick with the single controller approach - especially if you don't anticipate the codebase growing. As to the mythical best practice here - the mantra you need to repeat, as you doze off each night, is 'one controller per resource'. Of course this line can be fuzzy. For static stuff (home, about us, t&c's etc) I think a lot of people tend to put those pages under one controller. Of course, much depends on how that data is stored and updated (in- or outside of the application proper). If used tools runs a different method and/or is substantially different from the new tools / products page, I'd possibly split that up. Similarly the offers page (a kind of forum?). The sub-tools stuff sounds like something you'd keep within the same controller - it's just slightly different takes on similar sets of data, isn't it? Best Practice Regarding Controllers / Site Structure - El Forum - 10-24-2009 [eluser]Udi[/eluser] I would make "page" controller, that loads content from the DB. I don't think creating methods for each page is good idea even if its a small amount of pages. Create new table called pages: ID, ParentID - New Tooling for example, Title Content UrlTitle - for cool urls like example.com/page/Inspection_and_Metrology, The UrlTitle is the identifier of the page in your site, not the ID. Write a model that retrieves you content of page by the UrlTitle segment... and you're good to go. Best Practice Regarding Controllers / Site Structure - El Forum - 10-24-2009 [eluser]JamesTaylor[/eluser] In this instance the site doesn't actually use any methods, i was planning on the use of a database so i could have a go at implementing that side of things with CodeIgniter but it ended up the client didn't want to display enough info for it to be worth while. I have a couple of other things in the pipeline that will allow me to play on that front... ...Udi your post has just come as i am replying to Jedd's post - are you suggesting i place all the content of each page, even if it is static, into a database? What i have done in respect to my controller called structure which has everything incorporated into it for the entire site is set out below. Code: class Structure extends Controller { Part of my reason for asking is with one eye on the end urls that are displayed in the address bar (for SEO purposes as well) - i'm looking into htaccess files and mod rewrite but again its something i'm not familiar with at the moment. All url currently show: /RDBarrett/index.php/structure/... i have implemented a simple htaccess file with code copied from another post on here to make it /RDBarrett/structure/... but i still want to get rid of the '/structure' part as it isn't friendly! If i had a controller for each main section it would create good urls (easily with current htaccess file) for users and SEO such as: /RDBarrett/About_Us /RDBarrett/New_Tooling /RDBarrett/New_Tooling/Milling_Drilling - the Sub pages of New Tooling could be incorporated into that controller. I appreciate i am asking about two issues that can be dealt with separately but at the moment i am seeing them as being inter-twined! as a noob i'm just trying to get my thoughts together Best Practice Regarding Controllers / Site Structure - El Forum - 10-24-2009 [eluser]Udi[/eluser] For the URL part -> use route array, in routes.php. Now, I suggested to use DataBase because it makes a lot more sense, even in small sites. If those are static pages, then cache them for 24 hours, or even more. Best Practice Regarding Controllers / Site Structure - El Forum - 10-25-2009 [eluser]Omar Vazquez[/eluser] I usually setup one controller per resource. Any resources that have sub pages with children would be passed as method parameters. For example: Code: class Portfolio extends Controller{ This would yield urls like this: www.site.com/portfolio/digital/web www.site.com/portfolio/digital/kiosk where "portfolio" is our controller, "digital" is our method and "web" and "kiosk" are parameters that get passed into our digital method. See this entry in the user guide for more details on passing uri segments to your methods: Passing URI segments to your methods Hope that helps a bit. Omar Best Practice Regarding Controllers / Site Structure - El Forum - 10-29-2009 [eluser]JamesTaylor[/eluser] Thanks for the suggestions guys, been run off my feet so far this week but will be getting back to look at this as soon as i get chance! |