Zend Framework 2 set global module variable

Asked
Active3 hr before
Viewed126 times

7 Answers

global
90%

In Zend Framework 2 there is a build in view helper called "Identity" that is available in all views. It requires that you register the service manager in a config.module.php like that:, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , If you have a Service, the Service is accessible in all Module anyways. There's no way to have one variable available everywhere (layout, controller, serviceclasses, etc...) – Sam Aug 28 '13 at 20:48 ,I want to make it so the variable "user" is global to all modules, so I've added this code

Use Config-Variables to make stuff application-wide available.

// module.config.php
return array(
   'someVariable' => 'someValue',
   // all the other stuff
);

And all you have to to is access the config. Of course the access varies depending on where you're trying to access it, but ultimately it's done like this:

// Example for being inside any of your Controllers
$servoceLocator = $this - > getServiceLocator();
$config = $serviceLocator - > get('config');
$myValue = $config['someVariable'];
load more v
88%

For other web servers, consult the web server documentation to determine how to set environment variables.,Advanced Configuration Tricks — Zend Framework 2 2.2.10 documentation,Configuration of Zend Framework 2 applications happens in several steps:,To simplify matters, we’ll assume the environment is “production” if no environment variable is present.

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
load more v
72%

Zend Framework 2 set global module variable

1.2. // module.config.php3.return array(4.    'someVariable' => 'someValue',5.    // all the other stuff6.);7.8.// Example for being inside any of your Controllers9.$servoceLocator = $this->getServiceLocator();10.$config         = $serviceLocator->get('config');11.$myValue        = $config['someVariable'];12.
65%

Zend Framework 2 2.1.2dev documentation »,The HeadLink helper has special methods for adding stylesheet links to its stack:,A model used with the Partial view helper can be one of the following:,Basic usage of partials is to render a template fragment in its own view scope. Consider the following partial script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
load more v
75%

Configuration mapping table ,Considering that you may have service configuration in your module configuration file, what has precedence?,configuration returned by the various service configuration methods in a module class,Configuration of zend-mvc applications happens in several steps:

zfcampus/zf-development-mode provides a concise and conventions-based approach to switching between specifically production and development. The package is installed by default with version 3+ skeletons, and can be installed with existing v2 skeletons using the following:

$ composer require zfcampus / zf - development - mode
load more v
40%

Is it possible to create variables or even shared objects (like DB adapter) at in Module.php for use in all view controllers? (Zend Framework 2),And in a controller, somehow access using:,Problem 2 solved! Init for controllers.,I see that one can do this in a controller action:

For example:

class Module {
   public
   function onBootstrap(MvcEvent $e) {

      $moduleConfig = $e - > getServiceManager() - > get('Config')
   }
}

And in a controller, somehow access using:

$this - > moduleConfig['db']

I see that one can do this in a controller action:

$config = $this - > getServiceLocator() - > get('Config')
$dba = new DbAdapter($config['db']);

FYI, I do have this in my global.php (and no, its not staying there, just example):

'db' => array(
   'driver' => 'Pdo',
   'dsn' => 'mysql:dbname=mydb;host=localhost;',
   'username' => 'root',
   'password' => '',
   'driver_options' => array(
      PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
   ),

),
load more v
22%

If you have assigned a variable to the layout’s view model in, say, an event listener within Module.php:,This article was posted on 3 April 2012 in Zend Framework, Zend Framework 2 ,Zend Framework Training,Another, more long winded way is to use the getRoot() method on the viewModel view helper:

Consider this controller code:


class IndexController extends ActionController
{
public function indexAction()
{
return array('myvar' => 'test');
}
}

class IndexController extends ActionController {
   public
   function indexAction() {
      return array('myvar' => 'test');
   }
}
load more v

Other "global-undefined" queries related to "Zend Framework 2 set global module variable"