When it comes to Controllers and Actions, Yii will do this automatically - if your URL Manager config is set up correctly.
in config/main.php, in the urlmanager
section:
'urlManager'=>array(
'showScriptName' => false,
'urlFormat'=>'path',
'useStrictParsing'=>true,
'rules'=>array(
// Remove all the default Yii rules
// Add your own specific rules
),
),
Several things here:
- I'm assuming you're using clean URLs from following the Tutorial
useStrictParsing
prevents Yii being over generous when trying to match URLs
- Removing Yii's default generic controller:action route rules means there's no way into your app that you don't specify.
If you're talking about throwing an error within a Controller Action, you can do this using Yii's CException class(es).
public function actionCheckOk()
{
if($ok)
{
$this->render('ok');
}
else
{
throw new CHttpException(404, 'Page not found');
}
}