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 ?

4
  • 1
    What use do these constants have? Where to use them?
    – Sam
    May 9, 2013 at 18:32
  • How would you do it without a framework? If only there was a function that let you define constants.
    – Crisp
    May 9, 2013 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. May 9, 2013 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 May 9, 2013 at 18:53

4 Answers 4

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 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; ?>
2
  • Thanks for this! I'm also placing arrays in there by serializing them: stackoverflow.com/questions/1290318/… Reduces a lot of duplicated code in your modules!
    – Armfoot
    Jun 27, 2013 at 7:41
  • const UPLOAD_PATH = getcwd() . '/public/uploads/'; why this is generating error?
    – Katty
    Dec 28, 2016 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
2
  • what is $obj ? don't you think it will give error ?
    – iit2011081
    Dec 16, 2016 at 19:47
  • If you're using PHP 5.4 or higher, you can do it in 1 line, like so: echo $obj->getServiceLocator()->get('config')['arrayname']['variable_name'];
    – evilReiko
    Oct 30, 2017 at 12: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 protected]';

        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(); ?>
1
  • 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, 2017 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.