Welcome Guest, Not a member yet? Register   Sign In
Routing DB pages
#10

(This post was last modified: 10-05-2015, 11:12 AM by weblogics.)

What I've been using in CI2 which would work in CI3 if a routing bug wasn't fixed is creating a cached version of the routes stored in the DB.

I'm using Jamie Rumbelow's MY_Model and using the triggers to run a save method against my database everytime a page is added/updated or removed to update the routes accordingly.

PHP Code:
/**
     * Get Slug By ID
     *
     * Returns a recursive slug based on a page ID
     *
     * @param $id
     * @return bool|string
     */
 
   public function get_slug_by_id($id)
 
   {
 
       $route $this->get($id);

 
       if( ! $route ) return FALSE;

 
       if$route->parent_id != )
 
           $prefix $this->get_slug_by_id$route->parent_id ). "/" $route->slug;
 
       else
            $prefix 
$route->slug;

 
       return $prefix;
 
   }

/**
     * Cache
     *
     * Rebuilds the routes cache file located in "application/cache/routes.php"
     *
     * @return bool
     */
public function cache()
 
   {
 
       $pages      $this->with('template')->get_many_by(array('status' => 'published'));

 
       if( ! empty( $pages) )
 
       {
 
           foreach$pages as $page )
 
           {
 
               $controller       $page->template->name;
 
               $method           ( ! $page->template->method ) ? 'index' $page->template->method;
 
               $id               $page->id;

 
               $controller       $controller DIRECTORY_SEPARATOR $method DIRECTORY_SEPARATOR $id;
 
               $route            $this->get_slug_by_id($id);

 
               //  Make sure index page is always set as the default controller
 
               if$route === 'index'    $route    'default_controller';
 
               if$route    $data[] = '$route["' $route '"] = "' $controller '";';
 
           }

 
           $output implode("\n"$data);

 
           $this->load->helper('file');

 
           $file    write_file(APPPATH "cache/routes.php""<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n // THIS FILE IS AUTOMATICALLY GENERATED BY THE CMS, DO NOT TRY TO EDIT THIS FILE AS IT WILL BE OVERWRITTEN!!\n\r" $output);

 
           if$file )
 
               log_message('debug''Page routing cache written successfully');
 
           else
                log_message
('error'"Page routing cache could't be written, check " APPPATH "cache/routes.php write permissions");

 
           return $file;
 
       }
 
   

The cache method is then triggered by the following triggers:

PHP Code:
   public $after_create      array('cache');
 
   public $after_update      array('cache');
 
   public $after_delete      array('cache'); 

This then spites out a file called routes.php in your cache directory (don't forget write permissions!), then in your config/routes.php add the following code:

PHP Code:
//  Cached Routes Generated from database
if(file_exists(APPPATH "cache/routes.php"))   include_once APPPATH "cache/routes.php"

The Generated routes file looks like this:

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
// THIS FILE IS AUTOMATICALLY GENERATED BY THE CMS, DO NOT TRY TO EDIT THIS FILE AS IT WILL BE OVERWRITTEN!!

$route["default_controller"] = "cms/index/1";
$route["about"] = "cms/index/2";
$route["products"] = "cms/index/3";
$route["products/ford"] = "cms/index/4";
$route["products/ford/fiesta-mk6-acr-rs-wide-arch-body-kit"] = "cms/index/5";
$route["products/ford/fiesta-mk6-acr-rs-spoiler"] = "cms/index/6"

This method means that you aren't hitting the database on every single page request, but only performing the grunt work when an admin updates the pages table (or where ever your content is stored).

The issue with this in CI3 is that the default_controller route has now been modified (or fixed in their view) to only accept a controller/method and due to this the default_controller route in the cache/routes.php file no longer runs, but the rest of the functionality works. If someone could resolve this side of it, this would be an ideal solution since its scalable and fits the existing architecture of CI.

Just to clarify this method works, but brakes only on the default route.

Assumptions
  • Your content / pages table has a field with the slug (uri) in.
  • Your content / pages table has a parent_id field in (this is so the get_slug_by_id($id) method can build the full slug (uri) route.
  • Your content / pages table has a field with the template_id in (see table below).

Other things you may notice

The cache method has a template method in, each page has a template and the template table consists of:
  • Id
  • Name
  • Controller
  • Method (can be null, cache will default this to index)

This way you can map a page to a particular template, I have extended this to go further for modules. If anyone's interested then I'm happy to share, but I just wanted to keep this example as lightweight as possible to try and give / get some input.
Reply


Messages In This Thread
Routing DB pages - by Martin7483 - 09-28-2015, 02:39 AM
RE: Routing DB pages - by slax0r - 09-28-2015, 04:47 AM
RE: Routing DB pages - by Martin7483 - 09-28-2015, 04:58 AM
RE: Routing DB pages - by slax0r - 09-28-2015, 05:03 AM
RE: Routing DB pages - by Martin7483 - 09-28-2015, 05:12 AM
RE: Routing DB pages - by slax0r - 09-28-2015, 05:21 AM
RE: Routing DB pages - by Martin7483 - 09-28-2015, 05:31 AM
RE: Routing DB pages - by slax0r - 09-28-2015, 05:46 AM
RE: Routing DB pages - by Martin7483 - 09-30-2015, 07:48 AM
RE: Routing DB pages - by weblogics - 10-05-2015, 11:07 AM
RE: Routing DB pages - by Martin7483 - 10-08-2015, 07:54 AM
RE: Routing DB pages - by weblogics - 10-09-2015, 05:11 AM
RE: Routing DB pages - by mwhitney - 10-09-2015, 06:33 AM
RE: Routing DB pages - by Martin7483 - 10-15-2015, 01:02 AM
RE: Routing DB pages - by Martin7483 - 10-15-2015, 05:03 AM



Theme © iAndrew 2016 - Forum software by © MyBB