Yii Framework Forum: View Rendering from Console App - Yii Framework Forum

Jump to content

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

View Rendering from Console App Standardization Rate Topic: -----

#1 User is offline   paystey 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 12-October 11

Posted 14 December 2011 - 05:06 PM

I'm just building some console functionality into a half existing web application and came across the seemingly common problem of how to render views without a controller reference. I've seen plenty of fine implementations and they work no problem.

The problem is, how do I use existing functionality (namely components and models), of the web app, within the console app which need to render views. Not just for the purpose of output to a browser but for mail, XML among other things as well. They currently use Yii::app()->controller to get their controller instance, this obviously doesn't work in console apps, and even the hacky quick fix on Yii::app()->controller = MyConsoleCommand doesn't work because controller is read only.

What's the best standard way of rendering a view when outside the scope of the request handler (Whether controller or Command). Something like Yii::app()->renderViewFile() would be fine, doesn't use latyouts, just parses a file alias and renders it, returns the content.

Any Ideas?
0

#2 User is offline   paystey 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 12-October 11

Posted 15 December 2011 - 10:51 AM

OK I've managed to find a work around. It's a little horrible and I'm hoping there's some standardized way of rendering that I've missed.

I've overridden the CConsoleApplicaton class, which is harder than it sounds because you have to copy the yiic index files to use a custom Yii base class so you can provide the overriden createConsoleApplication method.

The application can now continue to use Yii::app()->controller->renderPartial/Text etc. so it means very little change to the current code.

The new eConsoleApplication looks like this:

class eConsoleApplication extends CConsoleApplication
{
	private $_controller = false;
   	public function getController(){
		if ($this->_controller === false)
			$this->_controller = new controller('site');
	
		return $this->_controller;
	}
	
	public function getViewRenderer(){
		return null;
	}
	
	public function getViewPath(){
		return $this->getBasePath().DIRECTORY_SEPARATOR.'views';
	}
	public function getTheme(){
		return NULL;
	}
}


It grabs the default controller and passes off view rendering to it. You cannot use render using this method (by design because you're in a console) but you can use render partial etc.

this isn't ideal as it means overwriting the yiic application files with the framework ones, the yii class and the consoleApplication component.

Not great to say all I want is to be able to render a view in my application without having to do console specific code.
0

#3 User is offline   jellysandwich 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 86
  • Joined: 03-May 11

Posted 15 December 2011 - 11:33 PM

Yii-mail has a great and simple example for this.

// if Yii::app()->controller doesn't exist create a dummy 
// controller to render the view (needed in the console app)
if(isset(Yii::app()->controller))
    $controller = Yii::app()->controller;
else
    $controller = new CController('YiiMail');

// renderPartial won't work with CConsoleApplication, so use 
// renderInternal - this requires that we use an actual path to the 
// view rather than the usual alias
$viewPath = Yii::getPathOfAlias(Yii::app()->mail->viewPath.'.'.$this->view).'.php';
$body = $controller->renderInternal($viewPath, array_merge($body, array('mail'=>$this)), true);	

0

#4 User is offline   paystey 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 12-October 11

Posted 16 December 2011 - 04:06 AM

hi, thanks for the help.

Yeh that's pretty much what I arrived at for rendering a view in the console specific models but I'm not keen on putting this chunk everywhere, I'd rather put it somewhere shared and share a common rendering method between console and web because I have so much shared functionality between my console and web app.

But the only real thing console and web apps share is the CApplication Component and as I said this gets messy to override. I was just hoping Yii had some nice hidden feature that I didn't know about for common rendering but perhaps not.

Thanks
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