Tag Archive for Yii

URL Management in Yii

Having a long url is seo unfriendly so mechanisms are devised to format the url in much cleaner and precise way. One of the best way to achieve this is using .htaccess file to rewrite the urls.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

This script will hide .php extension of the url and provides much cleaner url. For instance: http://ujjwalprajapati.com.np/cleanurl.php would look like http://ujjwalprajapati.com.np/cleanurl

For a url with parameters like http://ujjwalprajapti.com.np/index.php?id=10, it would be much clean to use it like http://ujjwalprajapti.com.np/10

RewriteRule ^(.*)$ index.php?id=$1 [L]

 

In Yii, URL is managed by CUrlManager. To format the URL, we should configure the urlManager application component so that createUrl can automatically switch to the new format and the application can properly understand the new URLs.

array(
    ......
    'components'=>array(
        ......
        'urlManager'=>array(
            'urlFormat'=>'path',
        ),
    ),
);

This will format the url like: http://ujjwalprajapati.com.np/index.php?r=post/read&id=10 to http://ujjwalprajapati.com.np/index.php/post/read/id/10.

Now, we can hide index.php configuring the property showScriptName.

array(
    ......
    'components'=>array(
        ......
        'urlManager'=>array(	
            'showScriptName' => false,
            'urlFormat'=>'path',
        ),
    ),
);

And also, we need to use .htaccess for the purpose.

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

We can also use named parameter in url routing to route specific format url to specific controller/action.

 array(
    ......
    'components'=>array(
        ......
        'urlManager'=>array(
           'showScriptName' => false,
           'urlFormat'=>'path',
           'rules'=>array(
       	       '<userid:[\w.]+>/<imageid:\d+>' => 'profile/image',
               '<userid:\w+>/friends' => 'profile/friends',	
               'message/<userid:[\w.]+>' => 'message/view',
               '<userid:[\w.]+>/<action:\w+>/<id:\d+>' => 'profile/<action>',  
           ), 
        ),
    ),
);
  • The first rule will be implied when calling $this -> createUrl(‘ujjwal/10’), here ‘ujjwal’ is the userid and ‘10’ is the imageid. This will call upon the action imageof the Profile Controller class.
  • The second rule has fixed action. Calling $this -> createUrl(‘ujjwal/friends’) will call upon action friends of Profile Controller class.
  • The third rule has fixed controller. Calling $this -> createUrl(‘message/ujjwal’) will call upon view action of the Message Controller class.
  • The forth rule is implied calling $this -> createUrl(‘ujjwal/message/10’), this will call upon the action message of the Profile Controller Class which is fixed and id 10 is passed as parameter to action specified.

Multilanguage in Yii

If you are wondering about using multi-language feature in your Yii application, then you came to the right spot. In this tutorial, I will be showing you how to implement the multi-language using Message Translation feature in Yii.

In Yii, Message translation is done by calling Yii::t(). This method translates the given message from source language to target language. Also, when translating a message, its category has to be specified since a message may be translated differently under different contexts.

Message translation could be achieved using any three of the message sources.

  • CPhpMessageSource
  • CGettextMessageSource
  • CDbMessageSource

In this tutorial, I will be focusing on CDbMessageSource. CDbMessageSource represents a message source that stores translated messages in database. Let us begin now.

Since we are using database to store the message, we need two tables; one is to store the Source Message and the other to store the Translated Message. Here is the schema for it.[http://www.yiiframework.com/doc/api/1.1/CDbMessageSource]

CREATE TABLE SourceMessage
(
    id INTEGER PRIMARY KEY,
    category VARCHAR(32),
    message TEXT
);
CREATE TABLE Message
(
    id INTEGER,
    language VARCHAR(16),
    translation TEXT,
    PRIMARY KEY (id, language),
    CONSTRAINT FK_Message_SourceMessage FOREIGN KEY (id)
         REFERENCES SourceMessage (id) ON DELETE CASCADE ON UPDATE RESTRICT
);

The ‘SourceMessage’ table stores the messages to be translated, and the ‘Message’ table stores the translated messages.

www.000webhost.com