-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Support strict_vars globally in application config #6127
Comments
@hschletz why not making it a small module that registers a listener, and that can be re-distributed and enabled/disabled? |
The change would not break BC if the default remains unchanged. I'm talking about a new option "strict_vars" in the application config. Since this option does not exist yet (only if passed manually to the Variables object), it will not be present in any existing application code - it would be an entirely new feature. I think view variables should be treated like any other variable - ignoring reference to an undefined variable is bad practice.The hassle of an external module for this essential feature looks like overkill to me. |
Yeah, for this path, I really suggest having a module that deals with it. It can be as minimal as |
Sure, my workaround is not much code, but it's a bit quirky because it adds some postprocessing to all view models, instead of having things set up correctly in the first place. Additionally, I want to keep external dependencies as minimal as possible. This functionality really should be provided by the ZF core which currently not only encourages writing bad code, but makes writing better code needlessly complicated. I'm not exaggerating with the "bad code" thing: Once enabled, the notices revealed several previously undiscovered bugs in one of my projects. |
I fully agree on the strictness here, it would indeed help, but I would really just change that default value in 3.x and have a debug module in 2.x |
Interesting info about I did some testing by attaching a listener to <?php
namespace MyStrictViewVars;
use Zend\EventManager\EventInterface;
use Zend\ModuleManager\Feature;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\View\Variables as ViewVariables;
use Zend\View\ViewEvent;
class Module implements Feature\BootstrapListenerInterface
{
/**
* @var ServiceLocatorInterface
*/
protected $serviceManager;
/**
* Bootstrap event listener
*
* @param EventInterface $event
*/
public function onBootstrap(EventInterface $event)
{
$application = $event->getApplication();
$eventManager = $application->getEventManager();
$identifier = 'Zend\View\View';
$event = ViewEvent::EVENT_RENDERER;
$callback = array($this, 'setStrictVars');
$priority = 100;
$eventManager->getSharedManager()->attach($identifier, $event, $callback, $priority);
$serviceManager = $application->getServiceManager();
$this->setServiceManager($serviceManager);
}
/**
* Set strict vars on ViewModel
*
* @param EventInterface $event
*/
public function setStrictVars(EventInterface $event)
{
$config = $this->getServiceManager()->get('Config');
if (!isset($config['view_variables']['strict_vars'])) {
return;
}
$useStrictVars = (bool) $config['view_variables']['strict_vars'];
$viewModel = $event->getModel();
/**
* If $variables is not instance of ViewVariables,
* convert it to ViewVariables and overwrite $viewModel variables
*/
$variables = $viewModel->getVariables();
if (!$variables instanceof ViewVariables) {
$viewVariables = new ViewVariables($variables);
$viewModel->setVariables($viewVariables, true);
}
$viewModel->getVariables()->setStrictVars($useStrictVars);
}
/**
* @param ServiceLocatorInterface $serviceManager
*/
protected function setServiceManager(ServiceLocatorInterface $serviceManager)
{
$this->serviceManager = $serviceManager;
}
/**
* @return ServiceManager
*/
protected function getServiceManager()
{
return $this->serviceManager;
}
} |
@Martin-P the solution there would be to just replace the |
Did some digging. The creation of return array(
'service_manager' => array(
'factories' => array(
'ServiceListener' => 'ModuleName\Factory\ServiceListenerFactory',
),
),
// more config
); But now the namespace is still unknown, because the module is not yet loaded. Perhaps the dirty way by overriding the protected property Edit: nevermind, this is becoming a mess 😃 |
Always funny to read back your own comments 😄 |
Why not making it a small configuration option? :) There is no reason to handle view variables different than regular variables, if they are not set but used. |
This sounds like the job of a module to me. |
This issue has been moved from the |
I want a notice whenever a view template tries to access an undefined view variable via $this->var. I know only 2 ways to achieve this:
Zend\View\Variables even supports an option "strict_vars" via its constructor, but neither this option nor setStrictVars() appear to be used anywhere within the ZF code.
It would be nice to be able to set strict_vars globally in the application configuration and have the Variables constructor evaluate it.
The text was updated successfully, but these errors were encountered: