poojan mehta PM
poojan mehta PM

Reputation: 75

how to use constant in cakephp 3?

Hello Friends This is my Function ...

bootstrap.php

$support_email = '[email protected]';
define('SUPPORT_EMAIL', $support_email);

testcontroller.php

public function testcron(){

        $email = new Email('default');
        $email->from(['[email protected]' => 'My Site'])
            ->to('[email protected]')
            ->subject('About')
            ->send('My message');
    }

when I run this function it says "already use constant". Actually i don't know how can i use this constant.. Can anyone Tell me ??

Upvotes: 0

Views: 1373

Answers (2)

Rakesh
Rakesh

Reputation: 505

You can use constant in core php like this.

define('SUPPORT_EMAIL', $support_email); 
echo SUPPORT_EMAIL;

In cakephp 3.x you can use constant like this.

In config/Bootstrap.php

$support_email = '[email protected]';
Configure::write('SUPPORT_EMAIL', $support_email);

In controller

use Cake\Core\Configure; 

public function testcron(){

    $support_email = Configure::read('SUPPORT_EMAIL');
 }

In view file

<?php use Cake\Core\Configure; 
$support_email = Configure::read('SUPPORT_EMAIL');
?> 

Upvotes: 1

Sehdev
Sehdev

Reputation: 5662

You are getting this error because this contant is already defined somewhere. Either you can change the constant name or do the following

if (!defined('SUPPORT_EMAIL')) {
    $support_email = '[email protected]';
    define('SUPPORT_EMAIL', $support_email);
}

This will create constant only if it does not exist.

Upvotes: 0

Related Questions