Yii::app()->Request Or How To Get Url Parts

The main reason for this question is that I always need to get parts of a url with "pretty" urls like:

http://myhost.com/home, http://myhost.com/login, http://myhost.com/goods/109 etc.

I need a real and simple tool to grab any part of URL like I did, for example, in CodeIgniter:




$this->uri->segment(1); // returns - home, login, goods

// or 

$this->uri->segment(2); // returns - 109 



PS I`ve seen many methods (pathInfo, getUrl() etc) and surfed through tons of forums - there was nothing similar.

Please help.

Hi Arthur, please tell me if this helps you. If it doesn’t, please share your goals, too.

I`ve already been on this tutorial, there are just routes examples and link creations - nothing about getting parts.

Seems like Yii doesn`t have this functionality, if this is true I would like to see this as normal and standard part of an Yii FW.

PS of course pathInfo is not a solution, because U then need to parse url Yourself, also it would be better to include this, rather than Cookie for example, the native one (setcookie()) much better in terms of usability.

Do not resent on me, but truth is truth.

Hope this helps U improve Yii.

May be this solution will help somebody:




  public static function getUrlSegment($part) {

    

    $segementsArr = explode('/', Yii::app()->request->pathInfo);


    if (!empty($segementsArr[$part-1])) return $segementsArr[$part-1];

    

    return false;

    

  }



In that page it is described also how you can take parts of the requested URL for routing and for setting parameters for your actions.

I really believe that at least the second part can help you.

But please give us more details of what you’re trying to achieve so we can guide you in the right direction.

The code, examples, explanations are above is clear - isn`t it enough?!

Just read all above ArthurKushman closely, especially the 1-st one, if U can`t understand the code in the last:


  public static function getUrlSegment($part) {

    

    $segementsArr = explode('/', Yii::app()->request->pathInfo);


    if (!empty($segementsArr[$part-1])) return $segementsArr[$part-1];

    

    return false;

    

  }

which is just do what I wanted - Yii devs integrate something like this.

The topic is closed.

This is the solution,

www.hasandemir.com/how-to-get-contoller-action-segments-like-codeigniter/