Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8744277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:49:03+00:00 2026-06-13T11:49:03+00:00

Im new with Yii and Im trying to implement the parseUrl to manage API

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:49:04+00:00Added an answer on June 13, 2026 at 11:49 am

    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:

    $_GET['apiversion'] = $matches[2];
    $_GET['model'] = $matches[4];
    $_GET['id'] = $matches[6];
    

    And depending on the version you return the good controller:

    return "apiV" . $_GET['apiversion'] . "/view";
    

    Some links you need to understand:

    • How preg_match works (what is the structure of $matches)
    • Yii custom Url class

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to yii, and I'm trying to integrate the facebook-wall-like style into
I am new to the Yii framework and I would like to know the
I am new to Yii and am trying to understand it by creating a
I'm trying to implement CGridView in my website which only uses Yii framework without
I'm pretty new to Yii as well as Twitter Bootstrap, I am trying to
I'm trying to unit-test my controller (Yii framework). /** * @dataProvider provider */ public
I am new to Yii. I want to implement custom pagination. I want to
I fairly new to YII and still trying to understand it all. However from
I am trying to build a multi-page form with Yii, but am quite new
I am toying with my new install of yii framework, and trying to compile

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.