# Define global constant for use site wide

**URL:** <https://discourse.laminas.dev/t/define-global-constant-for-use-site-wide/1455>\
**Category:** Components & MVC\
**Tags:** zend-mvc\
**Created:** [March 20, 2020, 9:05am UTC](https://discourse.laminas.dev/t/define-global-constant-for-use-site-wide/1455 "2020-03-20T09:05:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![josiahking](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@josiahking](https://discourse.laminas.dev/u/josiahking)\
**Post date:** [March 20, 2020, 9:05am UTC](https://discourse.laminas.dev/t/define-global-constant-for-use-site-wide/1455/1 "2020-03-20T09:05:32Z")

</div>

Hello everyone,  
how can i define a global constant in Laminas, so that I can retrieve the data across modules or views site wide.

`define('SITE_NAME','Website name');`

how is this possible with laminas?

---

<div class="post-metadata">

**Author:** ![matthew](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matthew/32/14_2.png) [@matthew](https://discourse.laminas.dev/u/matthew)\
**Post date:** [March 20, 2020, 2:42pm UTC](https://discourse.laminas.dev/t/define-global-constant-for-use-site-wide/1455/2 "2020-03-20T14:42:56Z")

</div>

Constants can be defined any time before they are accessed. As such, you could do so in your `public/index.php`, `config/autoload/*.php` files, etc.

We generally recommend against using global constants, however, as it couples your application to global state. A better approach is to define a variable in your configuration, and then inject that value into the services that require it. It takes a little more work, but makes testing easier, and tends to prevent errors due to failure to define the constants.

---

<div class="post-metadata">

**Author:** ![josiahking](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@josiahking](https://discourse.laminas.dev/u/josiahking)\
**Post date:** [March 20, 2020, 3:48pm UTC](https://discourse.laminas.dev/t/define-global-constant-for-use-site-wide/1455/3 "2020-03-20T15:48:33Z")

</div>

Thanks @matthew is there an example to implement the second approach?

---

<div class="post-metadata">

**Author:** ![matthew](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matthew/32/14_2.png) [@matthew](https://discourse.laminas.dev/u/matthew)\
**Post date:** [March 20, 2020, 4:16pm UTC](https://discourse.laminas.dev/t/define-global-constant-for-use-site-wide/1455/4 "2020-03-20T16:16:16Z")

</div>

As an example, your `config/autoload/local.php` might include:

```php
return [
    'site_name' => 'Website Name',
];

```

From there, a factory can pull that value from the config to inject in the instance it is creating:

```php
class SiteHelperFactory
{
    public function __invoke(ContainerInterface $container)
    {
        return new SiteHelper($container->get('config')['site_name']);
    }
}

```

---

<div class="post-metadata">

**Author:** ![josiahking](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@josiahking](https://discourse.laminas.dev/u/josiahking)\
**Post date:** [March 21, 2020, 12:09am UTC](https://discourse.laminas.dev/t/define-global-constant-for-use-site-wide/1455/5 "2020-03-21T00:09:31Z")

</div>

I have created a config file using the first approach.  
How do I get these configurations in my view or controller?

---

<div class="post-metadata">

**Author:** ![matthew](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matthew/32/14_2.png) [@matthew](https://discourse.laminas.dev/u/matthew)\
**Post date:** [March 23, 2020, 2:16pm UTC](https://discourse.laminas.dev/t/define-global-constant-for-use-site-wide/1455/6 "2020-03-23T14:16:50Z")

</div>

> [@josiahking](#):
>
> How do I get these configurations in my view or controller?

For your controller, inject them via the controller’s factory, as I demonstrated with the `SiteHelperFactory` above.

For your view, you’ll need to pass them to your view model. So, pass them to your controller, and then in your controller, when injecting your view model or returning view models, include the value:

```auto
// Injecting the view model:
$this->getEvent()->getViewModel()->site_name = $this->siteName;

// Returning data for a view model:
return [
    'site_name' => $this->siteName,
];

```

---

<div class="post-metadata">

**Author:** ![josiahking](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@josiahking](https://discourse.laminas.dev/u/josiahking)\
**Post date:** [March 28, 2020, 10:51am UTC](https://discourse.laminas.dev/t/define-global-constant-for-use-site-wide/1455/7 "2020-03-28T10:51:12Z")

</div>

Thanks @matthew mixing your idea and this [code example](https://stackoverflow.com/questions/47033015/how-to-retrieve-application-config-values-in-factories-in-zend-framework-3-zf3) helped me achieved the result required.
