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.

Assigning dynamic urls to a single template (MVC-like routing)

July 19, 2011 7:20pm

Subscribe [3]
  • #1 / Jul 19, 2011 7:20pm

    onewed

    13 posts

    We have legacy urls that don’t fit the default template group/entry model that EE enforces, and/or urls that can be logically grouped to use common templates and logic. Example below.

    /wedding-dresses/designer/[designer name]/
    /bridal-shoes/designer/[designer name]/

    Both urls will show the same designer template, but the only way I can think of to make this work is to create two template groups, like the following, where each designer.html will have to load the same embedded template.

    + wedding-dresses.group
    |- designer.html  (loads shared _designer.html)
    + bridal-shoes.group
    |- designer.html (loads shared _designer.html)

    I’d just like a way to define a route like /[^/]+/designer/([^/]+)/ to execute a particular template. Is this possible? I don’t want to have to create very generic index global templates and use a large switch statement over path segments.

  • #2 / Jul 20, 2011 10:29am

    Boyink!

    5011 posts

    Sounds like a use-case for the Pages Module.

  • #3 / Jul 20, 2011 10:32am

    Kevin Smith

    4784 posts

    Hi onewed,

    I can definitely understand the predicament of making sure your URLs maintain their structure. EE has a certain way that it handles URLs, so that’s a paradigm you’ll need to either work within or circumvent to accomplish what you want. It sounds like your options here are implementing the solution you mentioned in your post (embedding the same template, _designer, in two parent templates, wedding-dresses/designer and bridal-shoes/designer), developing a custom solution using EE as a development platform, or rewriting the URLs using rewrites in .htaccess. Here’s the developers documentation that might help you develop a custom add-on to rewrite the URLs.

    Does that help?

  • #4 / Jul 20, 2011 12:13pm

    onewed

    13 posts

    Sounds like a use-case for the Pages Module.

    Please explain how. I’ve run the following test and couldn’t find a way to get the dynamic paths I need.

    1. I created a designer.group and a file designer.group/show.html
    2. I created a Page for a dummy Designer in my Designer Channel and assigned it the path /wedding-dresses/designer
    3. When accessing the url /wedding-dresses/designer I see the designer.group/show.html view, but when accessing the url /wedding-dresses/designer/foo (which I really want) I am shown the site’s default view (it essentially 404s on the view lookup and shows my homepage)

    I tried the same for creating a Page with the url /wedding-dresses/designer/ and it didn’t work either, for some reason rendered a view in my blog.group directory (totally unrelated view).

    How would you intend the Pages module to handle these routes?

  • #5 / Jul 20, 2011 12:22pm

    onewed

    13 posts

    implementing the solution you mentioned in your post (embedding the same template, _designer, in two parent templates, wedding-dresses/designer and bridal-shoes/designer)

    Not DRY (and therefore/additionally not dynamic), so it’s not a great solution.I’d end up having to create several groups with the designer.html template. Not good for maintenance. Also not good for performance.

    rewriting the URLs using rewrites in .htaccess

    I don’t think using .htaccess is a good idea for several reasons: increased developer complexity, not dynamic or tied to a database, difficult to debug, etc.

    Does that help?

    Unfortunately no. I’ve evaluated both option above, and would like to mention that we’ll have several other cases where we’ll want to maintain legacy urls that will require dynamic routing. We shouldn’t continually edit an .htaccess or add container templates that merely embed other templates.

    The biggest problem here is that CodeIgniter has dynamic routing baked in since it’s a good MVC framework. I just want to make use of that, so it’s likely I’ll have to write an add-on to re-route the urls in CI before EE executes its main controller.

  • #6 / Jul 20, 2011 3:36pm

    onewed

    13 posts

    I came up with a hacked-in solution for dynamic routes within an extension, using the CodeIgniter routing library. It likely won’t be forward compatible with future versions of EE. I could (and likely will) chop out the route parser and reduce this complexity.

    I can now have the following urls
    /wedding-dresses/designer/foo
    /bridal-shoes/designer/bar

    Both will turn into /designer/show/[foo|bar] and render template group & template designer.group/show.html with ‘foo’ and ‘bar’ available as the last segment. This code will give me the flexibility to handle other various legacy routes and render content within template groups and templates as desired.

    function sessions_start()
       {
          $rsegments = $this->EE->uri->rsegments;
    
          require('OW_Router.php');
          $router = new OW_Router();
          @include('routes.php');
          $router->routes = $route;
    
          $router->_parse_routes();
    
          $new_segments = array();
          foreach($router->uri->rsegments as $idx => $seg)
             $new_segments[$idx+1] = $seg;
    
          $this->EE->uri->segments = $new_segments;
          $this->EE->uri->rsegments = $rsegments;
       }

    In my opinion codeigniter/system/core/Router.php needs a rewrite.

  • #7 / Jul 20, 2011 6:01pm

    Brandon Jones

    5500 posts

    Hi onewed,

    Thanks for sharing that. This is a bit outside of the scope of what EE is designed to do, but there’s great info here. Moving over to Community Help for further discussion.

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

ExpressionEngine News!

#eecms, #events, #releases