Codeigniter MVC controller architecture
I'm building a site using CodeIgniter that largely consists of static content (although there will be a relatively small CMS backend, and there's code to handle localization/internationalization based on the domain used to access it). Typically, in a situation like this, I'd use a Pages controller that i开发者_Python百科s in charge of rendering static content, but as there are a fair number of pages on the site (30+) it'd quickly end up containing lots of methods (assuming one per page).
Should I break my Pages controller into multiple controllers (that perhaps inherit from it) according to different sections of the site? Should I organize methods differently in the Pages controller? What's the best practice here?
Thanks!
Justin
I'd say split your controller into multiple. Not only it becomes easier to find whatever you need to work on in the file, probably there is a relation between the methods that "belong" to the same section. The end result is a more organized project,
Alternatively, why not have one single method that serves a static page, and work out which page to serve based on url ($id)? You'd have to play with CI routes a bit, in order to still have "site.com/pages/static_page_x" instead of "site.com/pages/view/static_page_x", but if you have a consistent way of accessing your static pages, it would save some typing, give you a way of adding new pages without touching the controller code, and might also come in handy if you want to apply to all the static pages something that you didn't think you'd need before.
Why dont you try to use a helper to detect your URL with the base_url() method and then process it. set a session variable to define the language in the controller...
精彩评论