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 ?
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; ?>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With