Yii 1.1: dburlmanager

Dynamic URL rules based on database (provides pretty permalinks or friendly URLs)
16 followers

DbUrlManager extension.

Provides dynamic database-based URL rules.

Note: As of Yii 1.1.8, this may not be needed anymore. Please refer to custom URL rule classes in the guide.

These dynamic rules are like Wordpress' "pretty permalinks" or "friendly URLs". You do not have to have the controller name (or ID) on the URL: this extension can handle the request URI and route it to the correct controller.

Examples of clean and friendly URLs made possible by this extension:

http://mystore.com/blue-coffe-mug

http://myblog.com/my-first-post

Resources

Documentation

Requirements

  • Yii 1.1.3

Installation

  • Extract the extension directory under protected/extensions

Usage by examples

Setting the extension up (on application configuration file):

'urlManager'=>array(
    'class'=>'ext.DbUrlManager.EDbUrlManager',
    'urlFormat'=>'path',
    'connectionID'=>'db',
    ...
),

Setting up a dynamic rule:

'rules'=>array(
    // A dynamic rule.
    '<author:[\w-]+>/<post:[\w-]+>'=>array(
        'post/view',
        'type'=>'db',
        'fields'=>array(
            'author'=>array(
                'table'=>'tbl_author',
                'field'=>'author_name'
            ),
            'post'=>array(
                'table'=>'tbl_post',
                'field'=>'post_slug'
            ),
        ),
    ),
    // Now additional standard rules.
    '<controller:\w+>/<id:\d+>'=>'<controller>/view',
    '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
    '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),

For further instructions, look at the comment blocks in the beginning of the class file.

Change Log

August 31, 2010

  • Initial release: 1.0.

Total 8 comments

#12363 report it
globaleyeglasses at 2013/03/15 02:05pm
Undefined index: value

Undefined index: value protected/extensions/DbUrlManager/EDbUrlManager.php(307) in main.php config:

'<alias:[-.a-z1-9]+>'=>array( 'product/view', 'type'=>'db', 'fields'=>array( '<alias:[-.a-z1-9]+>'=>array( 'table'=>'zp_catalog_product', 'field'=>'url' ), ), ),

public function actionView($alias) { $this->layout='//layouts/column1'; $model=Product::model()->findByAttributes(array('url'=>$alias)); $this->render('view',array( 'model'=>$model, )); }


it works if I use: '<alias:[-.a-z1-9]+>'=>'product/view', but then I have categories too...

#6333 report it
Chris83 at 2011/12/30 02:38pm
Alternative solution

You can also achieve this with the means provided my by SEO extension, although the approach is quite different and it doesn't require to query the database.

SEO extension

#3458 report it
Rodrigo Coelho at 2011/04/14 02:49am
@sebas, @robregonm

@sebas, @robregonm, Any comment? Did the extension work well for you?

#3457 report it
Rodrigo Coelho at 2011/04/14 02:48am
@besnik

@besnik, I believe that using just plain SQL is better to avoid the extra load of the Active Record objects, since the checks are really simple.

#3455 report it
Rodrigo Coelho at 2011/04/14 02:45am
Update

Dear, I expect to cover at least some of the TODOs soon. The extension will have better performance with the caching implemented. The new features in Yii 1.1.7 will ease a lot this task.

Thank you, Rodrigo Coelho - mentel

#2732 report it
Besnik at 2011/02/02 04:12pm
Nice work.

It looks very interesting the way you specify routes. I'll give it a try.

A suggestion though: it seems more "the Yii way" to specify a model/attribute instead of a table/field!

#1955 report it
sebas at 2010/10/19 09:39am
Nice idea!

I like your Idea, I'll try it later...

#129 report it
robregonm at 2010/09/01 01:04pm
Excellent idea.

Very good, I'll give it a try.

Leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: MIT License
  • Developed by: Rodrigo Coelho
  • Category: Others
  • Votes: +9
  • Downloaded: 1,313 times
  • Created on: Aug 31, 2010
  • Last updated: Jun 25, 2011