Zend framework 2 constants

I need to declare constants that are available anywhere in the application. In Zend Framework 1, we declared in application.ini as:

 constants.NAME_TITLE = "User Name", 

Where and how do we do it in Zend Framework 2?

+7
source share
4 answers

I found a solution here . You must create a storage class in the model. In this class, you can create as many constants as you want.

 <?php namespace Application\Model; class Application { const EMAIL = ' email@gmail.com '; } 

Now you can get it everywhere:

 NameOfModule\Model\NameOfModel::NAMEOFCONSTANT 

Thus, you can, for example, print a constant in this form:

 <?php echo Application\Model\Application::EMAIL; ?> 
+8
source

For Zend Framework 2, one alternative solution.

you can define your global variable inside config / autoload / local.php

  'array_name' => array( 'variable_name' => value, ), 

and use it anywhere like:

 $this->config = $obj->getServiceLocator()->get('config'); //create config object $this->you_variable = $this->config['arrayname']['variable_name']; // fetch value echo $this->you_variable; // print value 
+1
source

You can define, assign and access CONSTANT as follows: Use these two classes with an alias:

 use Zend\Config\Config as Zend_Config; use Zend\Config\Processor\Constant as Zend_Constant; 

And then use the code below for any function of the controller class:

 define ('TEST_CONST', 'bar'); // set true to Zend\Config\Config to allow modifications $config = new Zend_Config(array('foo' => 'TEST_CONST'), true); $processor = new Zend_Constant(); $processor->process($config); echo $config->foo; 

He will give o / p:

 bar 
0
source

You can also write a function and a variable that can be accessed anywhere in your application, for example, in the controller, model, and views.

 <?php namespace Webapp; class ControllerName { const EMAIL = ' email@gmail.com '; public static function myFunction() { echo "doing work well."; } } 

and you can access this function and class property like

 <?php echo Webapp\ControllerName::EMAIL; ?> 

and

 <?php echo Webapp\ControllerName::myFunction(); ?> 
-one
source

All Articles