Custom Url Read From Database

Hi,

I have a site that is designed around organisations. each organisation needs a landing page.

I want to be able to send to users the link to their organisation which would look something like http://www.mywebsite.com/orgname.

I have added the following to the config/main.php file




array(

    'class' => 'application.components.OrgUrlRule',

    'connectionID' => 'db',

),                    



I have a file called OrgUrlRule.php that is called when I manually type the url in the correct format and it fires the parseUrl function




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

{

    if (preg_match('%^(\w+)(/(\w+))?$%', $pathInfo, $matches))

    {

        $parts = explode('/', $pathInfo);

        $org = $parts[0];

        $orgmodel = Organisation::model()->find('orgcode=:id', array(':id'=>$org));

        if($orgmodel===null) {

            return $pathInfo;

        }

        else {

            $url = Yii::app()->createUrl('organisation/home', array('id'=>$orgmodel->id));

            return $url;

        }

    }

    return $pathInfo;

}



The url seems to be properly formed as it returns ‘/organisation/home/3’ (or whatever the id of the org is) and if I copy and paste this url into the browser the correct page loads, however, the function results in an error 400, details of the error are below:




exception 'CHttpException' with message 'Your request is invalid.' in

..\site-files\yii\framework\web\CController.php:336

..

REQUEST_URI=/orgname



Not sure where to go from here, any help would be appreciated.

Regards

Greg J

Hi,

Try this code:




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

{

    if (preg_match('%^(\w+)(/(\w+))?$%', $pathInfo, $matches))

    {

        $parts = explode('/', $pathInfo);

        $org = $parts[0];

        $orgmodel = Organisation::model()->find('orgcode=:id', array(':id'=>$org));

        if($orgmodel===null) {

            return false; // not return $pathInfo;

        }

        else {

            $_GET['id'] = $_REQUEST['id'] = $orgmodel->id;

            return "organisation/home";

            // or

            // return "organisation/home/id/" . $orgmodel->id; // don't use the Yii::app()->createUrl(...) here!

        }

    }

    return false; // not return $pathInfo;

}



and check this part from the guide: Using Custom URL Rule Classes ;)