Yii Framework Forum: Checking If A View File Exists Automatically Before Rendering - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Checking If A View File Exists Automatically Before Rendering Rate Topic: -----

#1 User is offline   Boaz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 367
  • Joined: 23-January 11

Posted 25 November 2012 - 03:37 AM

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);
	}

Therapeutic PHP sessions My LinkedIn Profile
1

#2 User is offline   yiqing95 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 594
  • Joined: 27-December 10
  • Location:china

Posted 25 November 2012 - 09:55 PM

nice sharing ! :lol: thanks

may be should check if use another viewRender . if so the view file extension normally is not "php" (tpl,twig....)
0

#3 User is offline   Boaz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 367
  • Joined: 23-January 11

Posted 26 November 2012 - 12:15 AM

View Postyiqing95, on 25 November 2012 - 09:55 PM, said:

may be should check if use another viewRender . if so the view file extension normally is not "php" (tpl,twig....)


Yes, you're definitely right. I thought about it too but since this is not a robust extension that I've released, but rather some code snippet, I've decided to 'leave this as a home-assignment to the students'... Posted Image
Therapeutic PHP sessions My LinkedIn Profile
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users