Parsing URL (string) to array (controller, action)

Hi Guys,

I know CUrlManager has a parseUrl method which accepts a CHttpRequest. This is fine.

However, I need the ability to get the parse in a url and get out the the controller and action like so





Does anybody know how this could be done. I have been pulling my hair out trying to get this to work whilst also being efficient…

My final aim is to hide links based on whether a user has permission to view page it points to. Extending CHTML requires me to convert the url string to a controller and action in order to check the permission.

Any ideas??

Why not use the php explode function?

list($domain,$controller,$action,$param) = explode("/", $urlstring);

Ideally you should build a URL using Yii::app()->createUrl,

maybe you can have that URL creation inside your Companies module.

that could be a good place to insert your logic, checking if the user have rights or stuff like that.

For instance, you could have something like this:

//in the Companies module

public function getUrlUpdate()


        if (/*logic to check that the user is not allowed to follow this link */) return false;

        return Yii::app()->createUrl('companies/update', array(




and then you can check if the url isn’t false before giving it to CHtml::link

I was thinking of doing this, but its not very reliable.

Lets say I have a site at

if I then change hosts at its at everything would break!!

its not very flexible.

Ideally, this is what I want.

Inside CHTML there is a link method. We call this to write our links. I want to extend this so before the link is returned the CONTROLLER/ACTION is checked to see if they have permission to access that page. If they dont, return false and the link will not be displayed on the page.

Problem is, once I am inside CHTML, the link that is passed is in the format I need to parse that into

array(controller, action) so I can check the permission use my permission checking function. I could indeed go through and add if statements to the links, but I was hoping to do the change in a central place so it gets applied to the whole site…(as its quite large)

Any thoughts?

I should point out, we have the permission checking in place. It works fine.

Its just merging this with CHTML is proving tricky…the only way I can think of is to parse the url that gets passed to CHTML…

You can handle this by removing the Yii::app()->baseUrl ."/" from the string before you parse it. So, search and replace …baseUrl . "/" with an empty string. Then explode should work consistently:

$urlstring2 = str_replace(Yii::app()->baseUrl ."/", "", $urlstring);

list($controller,$action) = explode("/", $urlstring2);

The other way to handle this is to "explode" the url and assign the values to an array. Count the number of elements in the array and just take the last 2. (assuming .../controller/action is a consistent format of your URL) The first method (prior post) should work, however.

i think it decide by urlManager rules.

so should have a better method.

but i don’t know , anyone can tell me?

The closest I can find is CWebApplication.createController()