Hi,
I have this simple module in which the last paramter in the clean url i assumed to be the view filename. It is convenient in that case that each URL (in that module) has its own view file.
When someone makes a mistake on the URL, I want a CHttpException 404 to be thrown, and not a CException, which is the case if CController.render() doesn’t find the view file.
Since on every webapp there’s a shared controller, Controller, that can be customized, I’ve customized mine to achieve the above need. Here’s the code snippet:
// Controller class:
/**
* Checks if given view filename exists for 'this' controller.
* @param string $view_filename basename of the view filename, with no 'file extension' (just as you'd pass to 'render()')
* @return bool exists or not.
*/
public function isViewFileExists($view_filename) {
if (!is_readable($this->getViewPath() . '/' . $view_filename . '.php')) {
return false;
}
return true;
}
/**
* Renders a view with a layout.
* This method overrides parent one to introduce check on the view file and throw a 404 in case it doesn't.
* For complete documentation of this method please see parent implementation.
*
* @param string $view name of the view to be rendered. See {@link getViewFile} for details
* about how the view script is resolved.
* @param array $data data to be extracted into PHP variables and made available to the view script
* @param boolean $return whether the rendering result should be returned instead of being displayed to end users.
* @return string the rendering result. Null if the rendering result is not required.
* @throws CHttpException
*/
public function render($view, $data = null, $return = false) {
if (!$this->isViewFileExists($view)) {
Yii::log("Error: view file doesn't exists: " . $this->getViewPath() . '/' . $view, CLogger::LEVEL_ERROR, __METHOD__);
throw new CHttpException(404);
}
return parent::render($view, $data, $return);
}