Im new with Yii and Im trying to implement the parseUrl to manage API Version at the url.
I would like to redirect /api/1.0/… to controller apiV1.0.
Main.php (urlManager rules):
...
array('class' => 'application.components.ApiVersion'),
...
ApiVersion.php:
<?php
class ApiVersion extends CBaseUrlRule
{
public $connectionID = 'db';
public function createUrl($manager,$route,$params,$ampersand)
{
if ($route==='car/index')
{
if (isset($params['manufacturer'], $params['model']))
return $params['manufacturer'] . '/' . $params['model'];
else if (isset($params['manufacturer']))
return $params['manufacturer'];
}
return false; // this rule does not apply
}
public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)
{
if (preg_match('%^(\w+)(/(\w+))?$%', $pathInfo, $matches))
$url = $match[1] . $match[2]; // BUILD YOUR URL (controllerName/actionName)
$this->redirect(array($url));
}
return false;
}
}
Im very lost with how it works, someone could help me?
In your function the regex checks if the path is something like “api/num_of_version/your_model/an_id”
So if your url is matching this your are in the condition, you’ll have to set the get values:
And depending on the version you return the good controller:
Some links you need to understand:
Edit: If there is a particular point where your have some trouble please let me now, i’ll try to ellaborate a little more my answer on this point.