5

I have a list of constants that i want to use for the every view i have and i also used these in the constants in controller is it possible.

1
  • i want this all should work in the module that i created. Jul 5, 2016 at 5:13

2 Answers 2

6

You can define constant in file using const keyword.

const TRANSACTION_COMPLETE = '1';
const TRANSACTION_PENDING = '2';
const PATH_EMAIL_IDENTITY = 'trans_email/ident_sales';

You can call all const using self keyword. self::XML_PATH_EMAIL_IDENTITY self::TRANSACTION_COMPLETE self::TRANSACTION_PENDING

You can call constant in anyfile also you can use inside controller file.

5
  • which file i can use to define all these constants. Jul 5, 2016 at 5:15
  • 1
    you can call inside any file. you have to define constant in all file where you have really need Jul 5, 2016 at 5:20
  • if i need constant in controller first of all i must define them like this in the controller file. const FLAG= '1' ; then used that in the controller. Jul 5, 2016 at 5:24
  • 1
    yes this is the correct, you can call like self::FLAG Jul 5, 2016 at 5:24
  • 1
    you have to need this flag in other file then you can call like this, \Vendor\Module\Controller\FileName::FLAG, first pass your file path then call const flag Jul 5, 2016 at 5:26
0

Constants that are used in a single class, such as a controller, should be defined in that class. Constants that relate to a specific model should be on that model's Model class, even if they are used by other classes.

Constants that are not specific to a model, or relate to models defined in dependency modules, I prefer to define in the module's Api/ directory. Constants, like Interfaces, define a common point to be accessed and so should be found in a similar location.

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.