Yii Api Version Urlmanager W/ Parseurl

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:


class ApiVersion extends CBaseUrlRule

{

    public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)

    {

        if (preg_match('%^(api/(\d+))(/(\w+))(/(\d+))$%', $pathInfo, $matches))

        {

            // $matches[2] is the version and $matches[4] the model

            // If it matches we can check the version api and the model

            // If it's ok, set $_GET['model'] and/or $_GET['id']

            // and return 'apiVx/view'

        }

        return false;

    }

}

Im very lost with how it works, someone could help me?

Hi there are more than one way to write it here is the simplest one

First:




$str = trim("/api/1.0/", "/");

list($str, $ver) =  explode('/', $str);

Second (your way):


if(preg_match('/^(\w+)\/(\d+)\.(\d+)$/', $str, $match)) {

     $match[1] // will return api

     $match[2] // will return 1

     $match[3] // will retun 0

}

Note:

you can join the last to with dot

Ah, thanks for the reply.

But how do I redirect to the correct controller?

I dont know what to write inside:


...

		if (preg_match('%^(api/(\d+))(/(\w+))(/(\d+))$%', $pathInfo, $matches))

		{

	            // What I put here to redirect to controller apiVx.x?

		}

...

if i have to implement services I create a controller called api and i create methods like versions v1, v2, v3 some thing of that sort since I dont know your app structure i can not suggest you anything

but you can do a redirect with the following method


if(preg_match('/^(\w+)\/(\d+)\.(\d+)$/', $str, $match)) {

   $url =  $match[1] .  $match[2] . $match[3] // BUILD YOUR URL (controllerName/actionName)

   $this->redirect(array($url));

}

Thanks to help aliz23!

So, Im still confused. My API have different parameters depending the action.

/api/view/car?id=xxx

/api/list/cars?city=xxx

And I dont know what I need to write in the createUrl function too.


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;

	}

}

No one?

do you really need custom urlManager or custom urlRule class??

try just adding standard urlRule like this:




'api/<v1:\d+>\.<v2:\d+>/<action:\w+>' => 'api<v1><v2>/<action>',



THank you redguy!

Exactly what I needed.