ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Dynamic Routes

February 27, 2011 9:55am

Subscribe [5]
  • #1 / Feb 27, 2011 9:55am

    darrentaytay

    100 posts

    Hi guys, I’m working on a simple Content Management System and was just wondering what the best way to handle routing of new pages created via the CMS would be?

    I want my CMS to support pages having children, so an example would be;

    the-team/office-team/fred-jones

    I’m just not entirely sure how I can tell Codeigniter where that page is routed should the user add it themselves via the CMS.

    Obviously if it was static I can just add a route but in this case it’s a bit more difficult. Any guidelines/advice would be much appreciated.

  • #2 / Feb 27, 2011 10:30am

    AoiKage

    11 posts

    I’m not sure if I understood your post correctly or not, but if the problem is just declaring dynamic routes you can actually add them using both wildcards and regular expressions.

    Something like:

    $route['something/(:any)/(:num)'] = 'home/something/$1/$2';
  • #3 / Feb 27, 2011 11:35am

    JoostV

    527 posts

    If you want to set routing for your pages based on slugs you could set the default controller to be the controller that handles your CMS pages and add custom routes for all other stuff.

    // If there is no match for the other routes then this must be a CMS page
    $route['default_controller'] = 'page';
    
    // Custom routes for anything that is not a CMS page slug
    $route['admin/(:any)'] = "admin/$1";
    $route['blog/(:any)'] = "blog/$1";
    // Etc…
  • #4 / Feb 27, 2011 12:12pm

    darrentaytay

    100 posts

    I’m thinking if the User in the CMS adds a page called “about-us”

    I COULD hardcode it as a route:

    $route['about-us'] = "pages/index/about-us";

    But obviously that’s terrible.

    And then if they add more Child pages and end up with a structure such as:

    About Us
    - The Team
    —John
    —Amy
    —Frank
    —- Franks Involvement

    How would I route it if someone were to go to about-us/the-team/frank?

  • #5 / Feb 27, 2011 12:27pm

    AoiKage

    11 posts

    Is there a limit in the number of child pages for every page?

  • #6 / Feb 27, 2011 12:41pm

    darrentaytay

    100 posts

    Well, no but I can imagine it going deeper than 3/4 levels for my clients.

  • #7 / Feb 27, 2011 2:23pm

    JoostV

    527 posts

    Store your pages in an mptt table and you can use any slug to retrieve the page, no matter how deep.

    mptt is modified tree order traversal. It’s a bitch to get it right, but once you do it’s heaven on earth. I use it for nested pages, categories, etc.

    It enables me to retrieve a nested tree of pages of unlimited depth in just two queries.
    Also, retrieving a page with a slug like about-us/the-team/frank is not a problem.

  • #8 / Feb 27, 2011 2:49pm

    InsiteFX

    6819 posts

    Create a Page Controller!

    Then create a route for your Page Controller.

    InsiteFX

  • #9 / Feb 27, 2011 5:44pm

    Boris Strahija

    129 posts

    This is how I do it in my content system:

    $route['default_controller']     = 'front';
    
    // Admin routes
    $route[BACKEND.'/login']                = 'auth/auth_admin/login';
    $route[BACKEND.'/logout']               = 'auth/auth_admin/logout';
    $route[BACKEND.'/([a-zA-Z_-]+)/(:any)'] = '$1/$1_admin/$2';
    $route[BACKEND.'/([a-zA-Z_-]+)']        = '$1/$1_admin/index';
    $route[BACKEND]                         = 'dashboard/dashboard_admin';
    
    $route['404_override']             = 'front';

    The front controller handles all the public requests, and just compare the slugs to the content entries in your DB.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases