You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
<?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
}
}
The text was updated successfully, but these errors were encountered:
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'))
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.
In the "Handling Console Requests" section of the manual an example action controller is given:
However, trying this out against ZendSkeletonApplication with ZFTool installed, I get this exception:
Whenever I return anything other than a
Zend\View\Model\ConsoleModel
, scalar or array.Workaround
The text was updated successfully, but these errors were encountered: