Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Reverse lookup of routes
  • Just wondering if any thought has been given to this area. If I have a controller foo with method bar, then the URI for a page there would be http://www.example.com/foo/bar Now, I can create a route so that /foo/bar is aliased to /foob. The user visits /foob and they are given the output from /foo/bar Any controller that wishes to access that page would either use the full URI /foo/bar, with the risk that the same page now gets accessed at two different URIs, or we hard-code /foob into the controller with the risk that the routing could be changed at any time to restructure the pages. Ideally, it would be good if constructing the URI "foo/bar" performed a lookup into the routing list and realised that there is an alternative URI that accesses that page, and instead returns that, i.e. http://www.example.com/foob Maybe that has already been done, but I'm just not seeing it. Maybe there are other workarounds. I'm thinking along the lines of installing a third-party module, then giving it a new base route, and having all the URLs within that module set themselves to the new route automatically.
  • Harro Verton wrote on Thursday 8th of December 2011:
    I've been looking at this in the past, but it's quite complicated.

    I can see the regexes are a one-one street. It kind of makes leads me towards thinking about a reverse-routes script that is manually maintained, and allows rules to be added to modify URLs before they are used. If there is a hook in the URL generation class, then this could be provided by a module.
    Harro Verton wrote on Thursday 8th of December 2011:
    Also, currently module routes are only loaded when a module controller is requested from the URI. For this functionality you need all routes loaded.

    This is an area I haven't dug into yet - routes within modules. On an older CMS I used to use extensively (Xaraya) the first part of the URI was always enough to determine which module that URI was for. Each module could register all the URI starting paths they wanted, but each part could only go to one module. For example, "/news" could be registered by the "articles" module and that module would then own every path under "/news". If the CMS ever saw "/news/..." in a URI then it would know exactly where to go to find the routing rules (in both directions, e.g. "news", "func=view" and "articleid=123" would be picked up by the articles module and constructed as "/news/view/123", and a similar rule would be able to reverse that for the controllers). It was a bit clumsy in implementation, but kind of had the right idea.
  • In FuelPHP that is the same (unless you overrule it with a route), but you need the routes of your 'xyz' module loaded, to be able to use a get('test') that should return "/xyz/abc/def", defined in the module route config. Module routes allow you to keep all your routing related to module controllers inside your module, so a route like '/xyz/abc/' => '/xyz/def' doesn't have to be placed in your global routing config, and it isn't loaded if no request to the xyz module is made.
  • Ah, so the idea is to create custom names for each route in your module, use those custom names throughout the controllers and views, creating default routes for those names in your module routes file, then anyone using the module has the ability to override those routes in the global routes script (fuel/app/config/routes.php)? Can the names of the routes be name-spaced in some way, or should they just be prefixed with the module name, or something? In fact, it looks like it is all documented nicely here: http://docs.fuelphp.com/general/routing.html -- Jason
  • Yes, I added named routes and reverse routing to the docs a few weeks ago. But since we don't have a dev site at the moment, the new docs only came online after release of v1.1.
  • You can define a route like so
    '/foob' => array('foo/bar', 'name' => 'myroute'),
    

    You can then get this route using
    $route = Router::get('myroute');
    
    add feed it to for example html::anchor() in your views.
  • I can see how that can help, but it still kind of ties the contents of the routes config to the internals of a module (in the controllers and views), so you can't write a module to use named routes without knowing the routes are going to be set up, and you can't introduce you own named routes without modifying the module to use them. Unless a module can bring along its own set of default named routes that can be overridden by the application? I realise this is not an issue for small custom applications, as you would be developing all bits together anyway. I'm thinking more about further down the line when whole sites are put together with ready-made building blocks that need to be fairly loosely coupled. -- JJ
  • I've been looking at this in the past, but it's quite complicated. The issue with this is that you can have routes containing parameters, placeholders and even a regex. It is going to be tough and possibly time consuming to find your match. Also, currently module routes are only loaded when a module controller is requested from the URI. For this functionality you need all routes loaded.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion