Welcome Guest, Not a member yet? Register   Sign In
Best practices when coding a CMS in CI?
#1

[eluser]Starovich[/eluser]
Hello!

I am currently trying to make a front-end for a CMS system. (The admin panel uses old legacy code for now, this is only for the parts presented to the regular visitors.)

I started out easy by making a controller called "view" and implementing a function called "page", I would then call /view/page/<pagename> to visit a page. the "page" controller would fetch the content from a db, build any additional resources (such as a dynamic menu, for which I placed the build code in a method outside the class, but in the same file.) and send it to a view.

After that I began building additional CMS modules as methods inside the same controller. So I would have for example: "/view/marketplace" or "/view/calendar".

This quickly became unmanageable. I recently wrote a small library in CI and thought about how I perhaps could put the view code in a library instead of having everything in the same controller. Right now I have the following possible solutions:

1.) Make the "view" code into a library, this library would be loaded from any controllers that needed to utilise it, I could then make a marketplace.php controller and simply load the "view" library. Outputting a page would be done by the library, and not the actual controller the user accesses. I'm unsure how CI feels about this.

2.) As suggested by a friend, I could implement a superclass called "Viewable". This class would contain necessary methods and would extend CI's "Controller" class, just like a regular controller, only it would not show anything by itself. Any actual controllers utilising it would extend it. So something like: "class Marketplace extends Viewable" and then Marketplace would have access to all the page-building methods.

3.) Just doing what I'm doing and end up with a 4000 lines long "view" controller.

4.) Your magical solution, which will solve all my problems.

I am fairly new to CI, although I have been coding OOP in various languages.

Thank you for any assistance!
#2

[eluser]Starovich[/eluser]
Bump!
#3

[eluser]Jelmer[/eluser]
Take a look into extending the Controller class and put stuff like pages in a Page Controller, marketplace in its Marketplace Controller, etc...

You can search the forums for "MY_Controller" to see how others did it. The User Guide has some explanation on how to go about that at the bottom of the page Creating Libraries, the section is called "Extending Native Libraries".

That's a pretty good way to go about it when you've got 2 applications for your website: one for the visitors and 1 for the editors/admins. I personally don't do it that way because I've got the website in 1 application and need different functionality for the admin part and the public part. So I've created multiple libraries which contain the shared behavior, some are autoloaded for the entire website - others are only loaded for certain parts of the website.
#4

[eluser]Starovich[/eluser]
[quote author="Jelmer" date="1270417779"]Take a look into extending the Controller class and put stuff like pages in a Page Controller, marketplace in its Marketplace Controller, etc...

You can search the forums for "MY_Controller" to see how others did it. The User Guide has some explanation on how to go about that at the bottom of the page Creating Libraries, the section is called "Extending Native Libraries".

That's a pretty good way to go about it when you've got 2 applications for your website: one for the visitors and 1 for the editors/admins. I personally don't do it that way because I've got the website in 1 application and need different functionality for the admin part and the public part. So I've created multiple libraries which contain the shared behavior, some are autoloaded for the entire website - others are only loaded for certain parts of the website.[/quote]

After much time and effort, I implemented a custom MY_Controller which houses the common methods for showing pages. Thank you for your suggestions!




Theme © iAndrew 2016 - Forum software by © MyBB