Skip to content

Commit cd2c25c

Browse files
authored
Merge pull request tunecino#11 from LettuceBox/master
Allow config to specify plural or singular form for relations
2 parents af50783 + 20ec914 commit cd2c25c

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/UrlRule.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ class UrlRule extends BaseObject implements UrlRuleInterface
9797
];
9898

9999
private $_rulesFactory;
100+
/**
101+
* @var bool whether to automatically pluralize the URL names for controllers.
102+
* If true, a controller ID will appear in plural form in URLs. For example, `user` controller
103+
* will appear as `users` in URLs.
104+
* @see controller
105+
*/
106+
public $pluralize = true;
100107

101108
/**
102109
* Returns the UrlRule instance used to generate related rules to each model.
@@ -156,23 +163,27 @@ public function parseRequest($manager, $request)
156163
{
157164
$modelName = Inflector::camel2id(StringHelper::basename($this->modelClass));
158165

159-
$resourceName = isset($this->resourceName) ?
160-
$this->resourceName : Inflector::pluralize($modelName);
166+
if ( isset($this->resourceName) ) {
167+
$resourceName = $this->resourceName;
168+
}
169+
else {
170+
$resourceName = $this->pluralize ? Inflector::pluralize($modelName) : $modelName;
171+
}
161172

162173
$link_attribute = isset($this->linkAttribute) ? $this->linkAttribute : $modelName . '_id';
163174
$this->config['prefix'] = $resourceName . '/<' .$link_attribute. ':\d+>';
164175

165176
foreach ($this->relations as $key => $value) {
166177
if (is_int($key)) {
167178
$relation = $value;
168-
$urlName = Inflector::camel2id(Inflector::pluralize($relation));
179+
$urlName = $this->pluralize ? Inflector::camel2id(Inflector::pluralize($relation)) : Inflector::camel2id($relation);
169180
$controller = Inflector::camel2id(Inflector::singularize($relation));
170181
}
171182
else {
172183
$relation = $key;
173184
if (is_array($value)) list($urlName, $controller) = each($value);
174185
else {
175-
$urlName = Inflector::camel2id(Inflector::pluralize($relation));
186+
$urlName = $this->pluralize ? Inflector::camel2id(Inflector::pluralize($relation)) : Inflector::camel2id($relation);
176187
$controller = $value;
177188
}
178189
}

0 commit comments

Comments
 (0)