Detecting URL match in YII

Hello guys!

In master layouts we sometimes need to detect whether current URL matches specified route.

For example, having a menu rendering block, we render all items as links except those that

match current page URL. To make things visual:

<li><?= !Ext::isCurrentUrl('/blog.html') ? '<a href="/blog.html">' : '<a class="current">' ?>News & Events</a>|</li>

<li><?= !Ext::isCurrentUrl('/unique-plans.html') ? '<a href="/unique-plans.html">' : '<a class="current">' ?>Unique plans</a>|</li>

<li><?= !Ext::isCurrentUrl('/lottery-for-members.html') ? '<a href="/lottery-for-members.html">' : '<a class="current">' ?>Free lottery</a>|</li>

<li><?= !Ext::isCurrentUrl('/partnership-program.html') ? '<a href="partnership-program.html">' : '<a class="current">' ?>Partnership</a>|</li>

<li><?= !Ext::isCurrentUrl('/feedback.html') ? '<a href="/feedback.html">' : '<a class="current">' ?>Contact us</a></li>

Above block of code, however, is configuration-dependant. To be exact, changing UrlManager configuration will

need to change all the stuff in a template. Unfortunately, I didn’t find anything in YII to perform URL matching.

My current implementation is configuration-dependant, not generic, doesn’t respect parameters:

    public function isCurrentUrl($path)


        static $currentUrl;

        if (is_null($currentUrl))


            $route = Yii::app()->urlManager->parseUrl(Yii::app()->getRequest());

            $currentUrl = Yii::app()->urlManager->createUrl($route);

            $currentUrl = str_replace('/' . $this->getAction()->getId(), '', $currentUrl);


        return strcasecmp($path, $currentUrl) === 0;


Does anyone have anything similar to this task? Any ideas how to implement this?

Why not use CMenu? This widget automatically sets a menu item’s class to “active”, if it links to the current page. You can hide it or change it’s style using css.

You can also use




which would referer to

on the route "controller/action"

Thanks, guys!

Can’t use CMenu due to multiple reasons (still on YII 1.0, use different markup, need this as a function).

Have already took piece of code from CMenu. Works perfect!