Pages

Sunday, February 3, 2013

Constants,Configuration Variables in cake php

Many a times we need to use some variables throughout our application.

CakePHP's Configure class can be used to store and retrieve application or runtime specific values. This class can be used to store anything and can be accessed from any part of the application

For the application of the same we define a file in app/config/config.php

To store a configuration variable:

Configure::write('variable_name', 'value') ;

And to read from configuration:
 
Configure::read('variable_name');

Now we can use Configure::write() or we can also use the $config array

as follows

// Create the config array
$config = array();


// Define config variable
$config['from_email'] = '';


//  Or Define variable as follows

Configure::write('from_email', 'abc@abc.com') ;

 
$config['api_key'] = 'http://www.example.com';
$config['default_timezone'] = 'Europe/London'; 
$config['page_limit'] = 6;


Now our Framework must know about this file so add the following code in /app/config/bootstrap.php

Configure::load('config');


Now read the configuration variables anywhere in the application as follows

Configure::read('from_email');
Configure::read('api_key');
Configure::read('page_limit');
 

7 comments:

  1. Thanks for sharing these niche piece of knowledge. Here, I had a solution for my inconclusive problems & it’s really helps me a lot keep updates…
    PHP Training Chennai|PHP Training Institute in Chennai

    ReplyDelete
  2. Thanks for sharing these niche piece of knowledge. Here, I had a solution for my inconclusive problems & it’s really helps me a lot keep updates…
    PHP Training Chennai|PHP Training Institute in Chennai

    ReplyDelete
  3. Go to Team Admin > Website Design > Website Layout Configuration > Main Text tab
    Add this HTML code where you want your message, of course putting in your own message:
    This is my scrolling message
    .If you become to learn about Data Warehousing one main tools Big data Hadoop Training to search that Best Big Data Hadoop Training Institutes in Chennai in chennai city area Besant Technologies is Software Training Institute in Chennai to reach us Hadoop Training in Chennai
    Best Hadoop training Center in Velachery

    ReplyDelete
  4. You are explanation about cake php variable is really useful for me. Thanks for your effort.
    php training institute in chennai | php certification course

    ReplyDelete
  5. This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.
    Java training in Bangalore | Java training in Indira nagar

    Java training in Bangalore | Java training in Rajaji nagar

    Java training in Bangalore | Java training in Marathahalli

    Java training in Bangalore | Java training in Btm layout

    ReplyDelete
  6. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.

    Data Science Training in Indira nagar
    Data Science Training in btm layout
    Python Training in Kalyan nagar
    Data Science training in Indira nagar
    Data Science Training in Marathahalli | Data Science training in Bangalore

    ReplyDelete
  7. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's. great work
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete