Skip to content

Instantly share code, notes, and snippets.

@barnabywalters
Created January 16, 2013 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barnabywalters/4546702 to your computer and use it in GitHub Desktop.
Save barnabywalters/4546702 to your computer and use it in GitHub Desktop.
<?php
namespace BarnabyWalters\Rest;
use Symfony\Component\Routing\Matcher\UrlMatcher as Matcher;
/**
* UrlMatcher
*
* A subclass of Symfony’s UrlMatcher which strips the extension from incoming
* URLs and ensures they have a trailing slash, making for easier route
* writing and conneg+extensions when importing routes against a prefix.
*
* @author Barnaby Walters waterpigs.co.uk
*/
class UrlMatcher extends Matcher {
/**
* Match
*
* Performs some normalization to resolve some of the trailing-slash issues
* and allow formats to be appended to the request ad hoc without affecting
* routing.
*
* Effectively, removes the extension from $pathInfo and adds a slash onto
* whatever is left.
*
* @param string $pathInfo
*/
public function match($pathInfo) {
// Remove extension, if any
$parts = pathinfo($pathInfo);
$newPath = rtrim($parts['dirname'], '/') . '/' . $parts['filename'];
$newPath = rtrim($newPath, '/') . '/';
return parent::match($newPath);
}
}
// EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment