Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP Defining Constants

I want to include Google API in my app. I want to Define Outh2 Key, Secret Key and Developer Keys as Constants which i can call and use in the App controller.

Which is the best place to define the same ?

like image 471
Harsha M V Avatar asked Jun 06 '12 14:06

Harsha M V


People also ask

How to define constant in cakephp?

In cakephp 3 we declare constants in bootstrap. php file under /app/config folder. A constant can be declared as: Configure::write('CONSTANT_NAME', Value, true);

How to define constants in PHP?

A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note: Unlike variables, constants are automatically global across the entire script.

Are PHP constants case sensitive?

Class constants are always case-sensitive. Global constants declared with const are always case-sensitive. It should be noted that this applies only to the shortname of the constant, while namespaces in PHP are always case-insensitive. Constants declared with define() are case-sensitive by default.


2 Answers

/app/Config/bootstrap.php

I wouldn't set them up as constants, I'd use the configure class to store them:

Configure::write(array(
    'outh2.key'=>'foo',
    'outh2.secret_key'=>'bar',
    'outh2.dev_key'=>'baz'
));

$key = Configure::read('outh2.key');
like image 84
RichardAtHome Avatar answered Sep 21 '22 14:09

RichardAtHome


You may announce class which works with google API in vendors, there you define needle constants as usual. And in your app controller only include vendor like this:

<?php

App::import('Vendor', 'google', array('file' => 'google.php'));

class AppController extends Controller { 
...
like image 30
user1440167 Avatar answered Sep 20 '22 14:09

user1440167