Welcome Guest, Not a member yet? Register   Sign In
How to do a CMS type system
#1

[eluser]JanDoToDo[/eluser]
Hey guys I was wondering if you could advise the best way to do a CMS type system.

I have a front controller and an admin controller both extended from MY_controller. My question is would you route everything to the front controller and then have everything in that controller for everypage and pass everything as a function and then function arguments or would you route depending on the page type to a different controller. E.g. if you had an accounts page would you setup a route to 'accounts' because it is a completely different function or would you just pass everything to front controller and have accounts as a function with arguments in that? If you routed it to different controllers what if you wanted everything to be modular or database based - if you routed using the router class you would have to add new routes everytime a new module was added?

Would you also direct to the page controller and then have EVERYTHING contained in there or would you detect the uri segment and then do a redirect to the correct controller?
#2

[eluser]Cro_Crx[/eluser]
CodeIgniter is a fairly open framework so you could do everything either of the ways your suggesting.

I'd probably suggest having a controller for each type of object rather than a front controller. So if you had 'pages' and 'accounts' for example each would have their own controller. That way you only have to create routes when you need a single function to load many pages, such as a function to display a single account 'view_account' or similar.

I'm assuming for your pages you'll want the URL to be www.domain.com/somepage . To achieve this you'll probably need to extend the Router class. Here's MY_Router class for the CMS system I use:

Code:
<?php

class MY_Router extends CI_Router
{

    function MY_Router()
    {
        parent::CI_Router();
    }

    function _validate_request($segments)
    {
        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {
            // Set the directory and remove it from the segment array
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);

            if (count($segments) > 0)
            {
                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    show_404($this->fetch_directory().$segments[0]);
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');

                // Does the default controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }

            }


            return $segments;
        }

        // We load the pages controller and have its _remap function handle which method is loaded
        array_unshift($segments, 'pages');

        return $segments;
        
    }

}

Hope that helps Smile
#3

[eluser]JanDoToDo[/eluser]
Cheers for your router extension! Anyone else have any best practices of what they do?
#4

[eluser]JanDoToDo[/eluser]
In particular, what if the class loads the same type of page, but just wants a different view for the body text for that page? Would you store an extra value in the DB for the content as like a "view type" and it could be which view you want to pull up for that page... and then that could be a selectable option when creating the content?




Theme © iAndrew 2016 - Forum software by © MyBB