Welcome Guest, Not a member yet? Register   Sign In
Advice Needed - Using Libraries instead of Controllers to deliver pages
#1

[eluser]haydenp[/eluser]
In a current project I have to deal with database-driven pages and library-driven pages. I would like advice on whether or not the way in which I deliver the library-driven pages could be done any other "better/more efficient" way?

Areas of focus: The way in which the class properties are being handled. The way in which DIRECTORY LIBRARY (A) relies on DIRECTORY LIBRARY (B) ... and any other areas where improvements can be made.

Database driven pages

All the markup for the page's body comes from the databse

Library driven pages

All the markup for the page's body is generated by a library (these libraries may depend on other libraries).

To cater for the above scenario I have set up a single base controller 'application/controllers/page.php' and route all requests through the 'page/index' method. This allows me to determin if the current page is a database or library driven page based on what is in the url. I am then able to set the class properties and deliver the correct type of page.

For example:

mysite.com/index.php/terms
This would result in a database-driven page being delivered

mysite.com/index.php/directory
This would result in a library-driven page being delivered ie. the directory library would be loaded and the directory_home() method executed

mysite.com/index.php/directory/categories

This would result in a library-driven page being delivered ie. the directory library would be loaded and the directory_categories() method executed

I currently have it all working but would like advice on whether or not the way in which I deliver the library-driven pages could be done any other "better/more efficient" way?

Sample Code:

CONTROLLER

application/controllers/page.php

Code:
class Page extends Controller
{
    // A bunch of class properties here
    public $page_properties; // array
    etc...
    
    public function __Construct()
    {
        parent::Controller();
    }
    
    public function index()
    {
        $this->_set_page_properties();
        
        if( ... )
        {
            // deliver database driven page here by pulling the
            // details from the database and loading the views etc
            $this->load->view('_header', $data);
            $this->load->view('_body', $data);
            $this->load->view('_footer', $data);
        }
        elseif( ... )
        {
            // deliver library driven page here by using the libraries to build the page body
            // based on what is in the url etc. The views are then loaded and the page displayed

            $this->load->library('directory/directory_pages');
            $data['page_body'] = $this->directory_pages->get_page_body( $this->page_properties['page_type'] );
            etc...
            
            $this->load->view('_header', $data);
            $this->load->view('_body', $data);
            $this->load->view('_footer', $data);          
        }
        else
        {
            show_404();
        }
    }
    
    private function _set_page_properties()
    {
        // Run some check to see if we are dealing with a database driven page
        // or library driven page and set the class properties accordingly
    }
}

DIRECTORY LIBRARY (A)

application/libraries/directory/directory_pages.php

Code:
class directory_pages
{
    // A bunch of class properties here
    public $CI;
    public $directory_category_name; // str
    etc...
    
    public function __Construct()
    {
        $this->CI =& get_instance();
        
        $this->load->library('directory/directory_methods');
        
        $this->_set_directory_properties()
    }
    
    public function get_page_body($page_type)
    {
        ...    
    }
    
    public function directory_home()
    {
        ...    
    }
    
    public function directory_categories()
    {
        ...    
    }
    
    public function directory_detail()
    {
        ...    
    }
        
    private function _set_directory_properties()
    {
        ...
    }
}

DIRECTORY LIBRARY (B)

application/libraries/directory/directory_methods.php

Code:
class directory_methods
{
    // A bunch of class properties here
    public $CI;
    
    public function __Construct()
    {
        $this->CI =& get_instance();
    }
    
    public function make_bold()
    {
        return '<strong>' . this->CI->directory_pages->directory_category_name . '</strong>';    
    }
}
#2

[eluser]stuffradio[/eluser]
I don't even see any views being used, you're breaking the MVC pattern and using something like MLC (Model Library Controller).

You can keep your Page controller, and perhaps you should also use routes. That way you could go

http://yoursite.com/Page/home

In your controllers, it would go to the routes, and take the second URI segment. This would be something like Page/index/$1 in your route. Then in the controller, you can check if this is a page that you have defined as needing the database, if yes then do your database stuff, if no then use libraries or however you're doing it now.
#3

[eluser]haydenp[/eluser]
Thanks for the reply stuffradio

To keep the lines of code to a minimum I left out view requests etc. The code provided is simply sample code use to try get my point across without overly complicating things. My attempt to keep the post less complex may have backfired?

I have edited the sample code to show where the view are being loaded etc. see my controller 'page/index'




Theme © iAndrew 2016 - Forum software by © MyBB