Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Exception raised when returning ViewModel from controller action in console environment #2627

Closed
adamlundrigan opened this Issue · 3 comments

4 participants

@adamlundrigan

In the "Handling Console Requests" section of the manual an example action controller is given:

<?php
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        return new ViewModel(); // display standard index page
    }
}

However, trying this out against ZendSkeletonApplication with ZFTool installed, I get this exception:

PHP Fatal error:  Call to undefined method Zend\View\Model\ViewModel::getResult() in <dir>/library/Zend/Mvc/View/Console/DefaultRenderingStrategy.php on line 95

Whenever I return anything other than a Zend\View\Model\ConsoleModel, scalar or array.

Workaround

<?php
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\View\Model\ConsoleModel;
use Zend\Console\Request as ConsoleRequest;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        $vm = $this->getRequest() instanceof ConsoleRequest
            ? new ConsoleModel()
            : new ViewModel();  
        return $vm; // display standard index page
    }
}
@weierophinney weierophinney was assigned
@Thinkscape

It's a similar issue I had with response class (not) having send() method.

The most logical solution is to introduce interfaces to Request, Response and ViewModel which could hint appropriate listeners on the request/response capabilities. That would include SendableInterface and HavingResult interface, or similar.

Less logical and possibly a bit hacky is method sniffing, as it's currently done with send() method on Response - so we'd just check if is_callable(array($viewModel, 'getResult'))

@ThaDafinser

@adamlundrigan @Thinkscape is this still open / a problem?

I though there was a security related fix, that no "guessing" based on request variables is anymore...

@weierophinney

I've got a few thoughts:

The marriage of console and MVC is problematic on many levels. HTTP requests behave very differently than the command-line. Templating is less prevalent or necessary on the command-line. The term "routing" is highly loaded, and primarily associated with HTTP.

As such, my recommendation has been and continues to be: segregate your controllers. If you're writing a controller that will respond to console actions, then make sure you have a console environment, and only return a Console view model or use console::write()/console::writeLine(), etc. It's not a workaround; it's just a good practice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.