CodeIgniter Forums
Good coding practice ??? - 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: Good coding practice ??? (/showthread.php?tid=9880)



Good coding practice ??? - El Forum - 07-11-2008

[eluser]Bramme[/eluser]
Hey all,

I'm working on my second CI site, but it's rather big. It has a news and blog section, but both rely on the same database table (the only difference is the type: blog or news).

Now, at the moment, I use just one controller for my frontend, and I use routing to just cut off the class name.
My controller sorta looks like this
Code:
function index
    $this->home()

function home
    do stuff

function news
    $this->_news('news')

function blog
    $this->_news('blog')

function _news($type)
    $this->db->where($type)
Now I think my news function is doing waaay too much:
- if no id is set, display all posts with pagination
- if an id is set, show the single post
- show the comments under a single post
- process comment posting

This makes for a rather lenghty function, it's about 200 lines (haven't put anything in models yet, so that'll shorten it a little) but I was wondering:

Is there a better approach to all of this? I know MVC doesn't necessarily dictate how to do this, but I'm just looking for some pointers from you old rots.
Should I create a separate controller for my news, or maybe a separate function for single items? Should I do the comment processing in another function?


Good coding practice ??? - El Forum - 07-11-2008

[eluser]Yash[/eluser]
Use this logic
Code:
function home($func,$id)
    {
        
        if(!isset($func)&&!isset($id))
        {
            
            
        }
        
        
        if(isset($func)&& !isset($id))
        {
        
            
        
        }
}



Good coding practice ??? - El Forum - 07-11-2008

[eluser]xwero[/eluser]
I would spit the database table in blog and news and then you also can add fields one section requires and the other one doesn't. For instance adding a traceback id to the blog table.

Create 3 controllers; site (landing page, comments), news and blog. The reason why i put the comment function another controller is because the comments data is the same for both sections.


Good coding practice ??? - El Forum - 07-11-2008

[eluser]Chicken's Egg[/eluser]
[quote author="xwero" date="1215792168"]I would spit the database table in blog and news and then you also can add fields one section requires and the other one doesn't. For instance adding a traceback id to the blog table.[/quote]
I would do that too, but suggest to split the content of the news- and blog-postings from the meta information. So, one table for pages (using the nesteds sets pattern). One table for the content of the blog-postings and one table for the content of the news postings.

Advantage:
- It becomes easy to create a menu or a sitemap of your website, as all pages can be found in one table.

Disadvantage:
- I haven't got a clue how to create the urls in the menu to manages this all. It's something I'm struggling with a couple of days now.