8

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 ?

Wilt
  • 41,477
  • 12
  • 152
  • 203
user2367458
  • 91
  • 1
  • 3
  • 1
    What use do these constants have? Where to use them? – Sam May 09 '13 at 18:32
  • How would you do it without a framework? If only there was a function that let you `define` constants. – Crisp May 09 '13 at 18:39
  • @Sam These constants are to define global variables which I set in configuration for ex. LINK_TO_SOAP_SERVICE , their values are different for different environments. So I set them at one place and use them in entire project. – user2367458 May 09 '13 at 18:51
  • @Crisp : I can do it with a define() in php , but I wanted to follow the recommended way if there is in zend framework 2 as there was in zend framework 1 – user2367458 May 09 '13 at 18:53

4 Answers4

9

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@gmail.com';
}

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; ?>
Community
  • 1
  • 1
Black
  • 9,541
  • 3
  • 54
  • 54
  • Thanks for this! I'm also placing arrays in there by serializing them: http://stackoverflow.com/questions/1290318/php-constants-containing-arrays Reduces a lot of duplicated code in your modules! – Armfoot Jun 27 '13 at 07:41
  • const UPLOAD_PATH = getcwd() . '/public/uploads/'; why this is generating error? – Katty Dec 28 '16 at 11:30
2

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
Maulik Patel
  • 650
  • 5
  • 22
0

You can also write function and variable that can be accessed any where of your application like 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 class function and property like

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

and

<?php echo Webapp\ControllerName::myFunction(); ?>
Md Mehedi Hasan
  • 1,733
  • 1
  • 21
  • 34
  • What you said is technically correct. For sake of clarification, normally no one should put constants and static methods in a controller (unless if it's a rare case where they can belong nowhere except in that controller) – evilReiko Oct 30 '17 at 12:26
0

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

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

And then use below code to your 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;

It will give o/p:

bar
Rizban Ahmad
  • 139
  • 1
  • 13