9

I know that in folder config are file called core.php which I can configure application options as a debug mode, session, cache etc.

But I want do configure file for my application. I want for example configure how many post can be displayed in main page, thubnails size etc.

I think that the best place in config folder but where and when parse thos file in application (bootstrap, AppController another mechanism ?) and what is the best extension .ini or PHP array (for performance reason too). What is the best practice to do this ?

Arun Jain
  • 5,476
  • 2
  • 31
  • 52
R4D3K
  • 117
  • 1
  • 8

3 Answers3

8

DEFINE OWN CONSTANT FILE

Create a file lets suppose 'site_constants.php' containing some constant variables in app/Config folder. Define the following constants into it:

<?php    
define('HTTP_HOST', "http://" . $_SERVER['HTTP_HOST'].'/');
if(HTTP_HOST == 'localhost' || HTTP_HOST == '127.0.0.1')
{
     define('SITE_URL', HTTP_HOST.'app_folder_name/');
}
else
{
     define('SITE_URL', HTTP_HOST);
}

Include it in app/Config/bootstrap.php

require_once('site_constants.php');

Now you can use it anywhere into your website. And this is also a dynamic.

DEFINE OWN CONFIGURATION FILE

Create a file lets suppose 'my_config.php' containing some constant variables in app/Config folder. Define the constant in the following way:

<?php
$config['PageConfig'] = array('PostPerPage' => 5, 'UserPerPage' => 15);

Then in your app/Controller/AppController.php write the following line in beforeFilter() method:

function beforeFilter()
{ 
     Configure::load('my_config');        
}

Now in your controller's method, where you want to access the page number to be listed in your pagination list. You can use it by following code:

$page_config = Configure :: read('PageConfig');   
$user_per_page = $page_config['UserPerPage']; 
//or
$post_per_page = $page_config['PostPerPage']; 

This might looks long process to handle this task, but once done, it will help you in many senses.

Here are the advantages:

  1. you can easily define some more constants (like any file path etc).
  2. you can put all your ajax code into external JS files.
  3. you can directly deploy it onto any server without changing in constants as well as work perfectly onto your localhost.
  4. following standard conventions etc.
Arun Jain
  • 5,476
  • 2
  • 31
  • 52
  • About define own constant file i think that better idea is create VirtualHost and set ENV variable. Do you have idea how redefine some constract, when is another enviroment ? Using another config file ? – R4D3K Sep 05 '12 at 09:11
  • According to me, By creating a VirtualHost and set ENV variable, that would be server dependent and when you deploy site on more than one server, you will have to make the same changes in that server(manually setting env variable), which is not a good practice at all. And my answer is independent of host setting. I hope you understood, what I meant. Thanks :) – Arun Jain Sep 05 '12 at 09:36
  • Please keep in mind, answer was provided in 2012 while negative voting :) – Arun Jain Jul 27 '17 at 08:31
1

CakePHP provides the Configure class for this purpose. See the documentation.

You can use Configure::write($key,$value) in your own config file, and then read the values elsewhere in your application through Configure::read($key). It also allows you to use readers that automate the process and read in external configuration files. CakePHP provides a PHPreader and an INIreader by default and you can create readers to extend it.

mtsvetkov
  • 885
  • 10
  • 21
  • Yes I know about this class now i add some variables who want config in core.php but i think that is not good idea, better is do another config file. But i want know what is the best pratice too doing this. Im also using Zend Framework and i want do like in this frameowork creating ENV variable and have configure for prodaction and can rewrite some variables where in ENV is value or not. – R4D3K Sep 05 '12 at 09:01
0

Create a new file with configuring variables, like:

Configure::write('Twitter', array(
    'consumer_key' => "OTh1sIsY0urC0n5um3rK3Y4T878676",
    'consumer_secret' => "OTh1sIsY0ur53cReT76OTIMGjEhiWx94f3LV",
    'oauth_access_token' => "12345678-OTh1sIsY0urAcc355K3YT878676Y723n4hqxSyI4",
    'oauth_access_token_secret' => "OTh1sIsY0urACC355T0KEnsdjh4T878676FPtRRtjDA29ejYSn"
));

save this file in app/Config/twitter.php

Include that file in app/Config/bootsrap.php:

require_once('twitter.php');

In the Controller (this example 'app/Controller/TwitterController.php'), you can use that like:

$settings = Configure :: read('Twitter'); 
Sadee
  • 3,010
  • 35
  • 36