Url Rules From Database

Hey, i’ve been trying to create a custom rule class extending ‘CBaseUrlRule’ that pulls a page up based on the custom seoURL that is stored within a database.

Can anyone show/explain an example of how to implement this?

My URL’s at the moment are like: .../pages/view/id/5

Within the database I have stored the custom seoURL column.

How I want the url’s to look: .../home (home being the seoURL stored within the database)

Thanks in advanced =)

Have you read Using Custom URL Rule Classes? The example explains how to use a database.

Yes but I don’t fully understand the parsing of the URL section.

I have this:


 public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)

    {

        if(preg_match('%^[a-z0-9\-]+$%', $pathInfo, $matches))

        {

            $seoURL=$matches[0];

            $command=Yii::app()->{$this->connectionID}->createCommand();

            $result=$command->select('seoURL')->from('Pages');

            $result->where('seoURL=:seoURL', array(':seoURL'=>$seoURL));

            $result = $result->queryRow();

            if(!empty($result))

            {

                $_GET['seoURL']=$result['seoURL'];

                return 'pages/view';

            }

        }

        return false;  // this rule does not apply

    }

I’m pretty sure that should work. Are you sure the query is returning a result?

Here is my full rule class:


	public function createUrl($manager,$route,$params,$ampersand)

    {

        if($route==='pages/view')

        {

            if (isset($params['seoURL']))

                return $params['seoURL'];

        }

        return false;  // this rule does not apply

    }

 

    public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)

    {

    	var_dump($pathInfo);

        if(preg_match('%^[a-z0-9\-]+$%', $pathInfo, $matches))

        {

        	var_dump($pathInfo);

            $seoURL=$matches[0];

            $command=Yii::app()->{$this->connectionID}->createCommand();

            $result=$command->select('seoURL')->from('Pages');

            $result->where('seoURL=:seoURL', array(':seoURL'=>$seoURL));

            $result = $result->queryRow();

            if(!empty($result))

            {

                $_GET['seoURL']=$result['seoURL'];

                return 'pages/view';

            }

        }

        return false;  // this rule does not apply

    }

Going to /pages/view/id/5 - goes to the homepage

Yet /home - throws ‘Error 400 Your request is invalid.’

I have stuck a var_dump within the parseUrl function and the path gets dumped when going through /pages/view/id… but nothing when just the seoURL

Unsure, sort of knew to PHP, trying to get to grips with everything still.

What do your rules look like? Your custom rule should be at the top, so another rule doesn’t match first.

Since he’s a new user and can’t post anymore for a few hours, the solution was to set $_GET[‘id’] instead of $_GET[‘seoURL’] :)