Skip to content

Commit

Permalink
Allow generating REST resources by setting a default extension.
Browse files Browse the repository at this point in the history
This permits writing routes without the need to specify the extension.

If no extension is found, the default will be used.
If an extension is found, that extension will be used.

It is useful when you always want those resources to be identified as a
specific type.

So after a call like:

Router::mapResources(['categories'], ['_ext' => 'json']);
Router::parseExtensions('json', 'xml');

the following two will render JSON:

http://example.com/categories/1
http://example.com/categories/1.json

and

http://example.com/categories/1.xml

will still be returning XML.

If the parseExtension() call is omitted, the non-extension URL:

http://example.com/categories/1

will still be rendering JSON.
  • Loading branch information
bar committed Mar 31, 2014
1 parent 40bf521 commit 619ef13
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Routing/Route/Route.php
Expand Up @@ -320,7 +320,7 @@ public function parse($url) {
unset($route['_trailing_']);
}

if ($ext && empty($route['_ext'])) {
if (!empty($ext)) {
$route['_ext'] = $ext;
}

Expand Down
19 changes: 12 additions & 7 deletions src/Routing/Router.php
Expand Up @@ -67,14 +67,14 @@ class Router {
*
* @var array
*/
protected static $_prefixes = array();
protected static $_prefixes = [];

/**
* List of valid extensions to parse from a URL. If null, any extension is allowed.
*
* @var array
*/
protected static $_validExtensions = array();
protected static $_validExtensions = [];

/**
* Regular expression for action names
Expand Down Expand Up @@ -151,15 +151,15 @@ class Router {
*
* @var array
*/
protected static $_resourceMapped = array();
protected static $_resourceMapped = [];

/**
* Maintains the request object stack for the current request.
* This will contain more than one request object when requestAction is used.
*
* @var array
*/
protected static $_requests = array();
protected static $_requests = [];

/**
* Initial state is populated the first time reload() is called which is at the bottom
Expand All @@ -168,15 +168,15 @@ class Router {
*
* @var array
*/
protected static $_initialState = array();
protected static $_initialState = [];

/**
* The stack of URL filters to apply against routing URLs before passing the
* parameters to the route collection.
*
* @var array
*/
protected static $_urlFilters = array();
protected static $_urlFilters = [];

/**
* Default route class to use
Expand Down Expand Up @@ -450,10 +450,14 @@ public static function mapResources($controller, $options = array()) {
if ($plugin) {
$plugin = Inflector::underscore($plugin);
}
$prefix = '';

$prefix = $ext = null;
if (!empty($options['prefix'])) {
$prefix = $options['prefix'];
}
if (!empty($options['_ext'])) {
$ext = $options['_ext'];
}

foreach (static::$_resourceMap as $params) {
$id = $params['id'] ? ':id' : '';
Expand All @@ -463,6 +467,7 @@ public static function mapResources($controller, $options = array()) {
'controller' => $urlName,
'action' => $params['action'],
'[method]' => $params['method'],
'_ext' => $ext
);
if ($prefix) {
$params['prefix'] = $prefix;
Expand Down

0 comments on commit 619ef13

Please sign in to comment.