Yii:: SEO friendly url with hypen or dash

Posted: October 24, 2012 in Php, Yii Framework
Tags:

Hi,

With Yii, to have a friendly url with dash like this http://www.pulaisprings.com/proposal/photo-gallery.html

You can add this code into your controller:

public function missingAction($action_id) {
        /**
         * Support dash separated action ids: convert whatever-action-id to actionWhateverActionId method name, check if it exists and if it does - run it.
         */
        $action_id = explode('-', $action_id);
        $action_id = array_map('strtolower', $action_id);
        $action_id = array_map('ucfirst', $action_id);
        $action_id = implode('', $action_id);
        if (method_exists($this, 'action' . $action_id) || array_key_exists('action' . $action_id, $this->actions())) {
            $this->setAction($action_id);
            $this->run($action_id);
        }
        else {
            throw new CHttpException(404);
        }
    }
then create your menu url something like this: <?php echo CHtml::normalizeUrl(array('photo-gallery')) ?>

Eg:
<li><a id="photo" href="<?php echo CHtml::normalizeUrl(array('photo-gallery')) ?>" title="Photo Gallery">Photo Gallery</a></li>

*** this function i copy from Yii forum.. not sure who is the author.. please email me if you know who is the author.. will give credit to him/her.

Comments
  1. Donny Hansen says:

    I personally would have just suggested to make this change in the UrlManager rules in the config file. See http://www.yiiframework.com/wiki/404/hyphenation-of-routes-in-url-management/

  2. Sameera says:

    I have a URL like this
    http://www.mydomain.com/post/update-post/3

    I cant pass id to my action method
    updatePost($id)

    How to pass id from missingAction to update method?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s