Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zend framework 2 constants

I have to declare constants which are available anywhere in application. In Zend Framework 1 we used to declare in application.ini as :

constants.NAME_TITLE = "User Name",

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

like image 842
user2367458 Avatar asked May 09 '13 18:05

user2367458


2 Answers

I have found solution here. You have to create storage class in Model. In that class you can create as many constants as you want.

<?php  
namespace Application\Model;
class Application {
    const EMAIL = '[email protected]';
}

Now it can be accessed everywhere by:

NameOfModule\Model\NameOfModel::NAMEOFCONSTANT

So you can for example print the constant in a view like this:

<?php echo Application\Model\Application::EMAIL; ?>
like image 193
Black Avatar answered Oct 02 '22 13:10

Black


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 just 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
like image 20
Maulik Patel Avatar answered Oct 02 '22 13:10

Maulik Patel