Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

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

Closed
adamlundrigan opened this issue Sep 30, 2012 · 3 comments
Assignees
Labels

Comments

@adamlundrigan
Copy link
Contributor

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
    }
}
@ghost ghost assigned weierophinney Oct 1, 2012
@Thinkscape
Copy link
Member

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
Copy link
Contributor

@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
Copy link
Member

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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants