Complex Url Routing?

Does anybody know of JUST a routing framework (no orm, or mvc crud) that can handle complex url to route mappings that work similar to .NET’s web routing code? Not just being able to specifiy a pattern with variables and literals, but include defaults and even value restrictions for the variables?

Maybe Symfony Routing? (it is a standalone component from the framework.)

The RouteCollection::add() method takes two arguments. The first is the name of the route. The second is a Route object, which expects a URL path and some array of custom variables in its constructor. This array of custom variables can be anything that’s significant to your application, and is returned when that route is matched.

I took a look and reviewed it. Maybe I missed something but am not sure how it’d work. I am used to asp.net mvc’s implementation. Frequently, I’ll have routes like the following (example only) recoded for how it would look in PHP land.


// example of routing to a sub area
// this would load and run Areas\\Admin\\Controllers\\BookController->Edit($authorid, $bookid)
RouteTable::Routes->MapRoute(
    "Admin_Book_Edit_Route", // route name
    "Admin/Author/{authorid}/Book/{bookid}/Edit", // route pattern
    array( // component default values
        "area" => "Admin",
        "controller" => "Book",
        "action" => "Edit"
    )
)

RouteTable::Routes->MapRoute(
    "Default_Route", // route name
    "{controller}/{action}/{id}", // route pattern
    array( // component default values
        "controller" => "Home",
        "action" => "Index"
    )
)

I’m still not sure which way I should go with this. Thanks for the input!

Using Symfony Routing the above code example could be setup as:


$routes = new RouteCollection();
$routes->add(
  'Admin_Book_Edit_Route',
  new Route(
    'Admin/Author/{authorid}/Book/{bookid}/Edit',
    array(
      'area'       => 'Admin',
      'controller' => 'Book',
      'action',    => 'Edit'
    )
  )
);
$routes->add(
  'Default_Route',
  new Route(
    '{controller}/{action}/{id}',
    array(
      'controller' => 'Home',
      'action',    => 'Index'
    )
  )
);

Hey thanks. I actually downloaded it and am going through the code now. I kind of want to write my own because it is going to be released in a product. I’m fairly intelligent, having written dependency injectors and crqs/es systems, but this is kicking my behind! I can’t seem to get behind the logic of it all. Interestingly, several of my own routing classes are very similar to the ones in this package, up to the point where I can build a collection of route objects with pattern, defaults and restrictions. I even have eventual dispatch to the correct controller/action, complete with model and scalar parameter binding in place. I just can’t seem to get the part down where I can determine which route (if any) matches the request. I see where they use a compiled route and a matcher that builds regex…uhg…I’ve been writing over thirty years and still can’t write the simplest regex. There has to be a better, simpler way.

Well… you could always use the well crafted wheel that’s sitting right in front of you.

Just sayin’.

:wink:

Where’s the fun, and learning in that? LOL I know what you mean, and I probably will, but I would kind of like to learn how it’s done in case I need something similar elsewhere. Regex has been a pain in my side for far too long. =)

I read a nice article on phpmaster.com about Aura.Router, maybe that is more to your liking?
It’s PHP5.4+, but has been ported to work with 5.3+ as well; anything below that is not supported as far as I know.

The official site is here: https://github.com/auraphp/Aura.Router

Aura has other packages as well which may also be worth checking out: https://github.com/auraphp/

Excellent find, thanks, so that is what happened to Solar …