Share configuration file at CakePHP between Config files (routles.php, mail.php...) - cakephp

I have a site running in localhost as a development environment and in a server for production.
There are some differences in some configuration files between both and I every time I have to update the changes in the server I have to be careful not to overwrite some files which are different.
I would like to be able to simplify this process by creating just one single file with the correct configuration for each environment.
I need to read that file currently in this Config files:
app/Config/email.php
app/Config/routes.php
And ideally, if possible in:
app/Vendor/Vendor_name/vendor_file.php
Is it possible somehow?
I have tried to use Configure::read and Configure::write but it seems it can not be done inside email settings such as public $smtp or in the routes file.
Thaks.

The routes file is simply a php file with calls to the router. You could very simply split it up into multiple files and load them yourself:
app/Config/
routes.php
routes_dev.php
routes_production.php
routes.php would then load the proper routes file.
<?php
if ($env == 'dev') {
include 'routes_dev.php';
} else {
include 'routes_production.php';
}
The email config is also just a php file. You could write a function to set the proper default config based on the environment.
class EmailConfig {
public function __construct() {
if ($env == 'dev') {
$this->default = $this->dev;
}
}
public $default = array(
'host' => 'mail.example.com',
'transport' => 'Smtp'
);
public $dev = array(
'host' => 'mail2.example.com',
'transport' => 'Smtp'
);
}
As for vendor files, that's a case by case basis.
If you have a deployment system, it might be better to actually have separate files for each environment (maybe even a full config directory) and rename them after the deployment build is complete, making Cake and your code none the wiser.

The way I used to handle this situation was to add environment variables to the apache virtualhost configuration.
SetEnv cake_apps_path /var/www/apps/
SetEnv cake_libs_path /var/www/libs/
This allowed me to then pull $_SERVER['cake_apps_path'] and $_SERVER['cake_libs_path']. Then each developer can set his own variables in his own virtualhost config, and you add that to the server's virtualhost config, and you're done. Each developer can have their own pathing.

Related

integrate rollbar to my Cakephp project

i wan't to integrate Rollbar to my cakephp project but i dont know where to include the code referencing to rolbar in my app ?
i have use this code
<?php
use \Rollbar\Rollbar;
// Installs global error and exception handlers
$config = array(
// required
'access_token' => 'MY_ACCESS_TOKEN',
// optional - environment name
'environment' => 'production',
);
Rollbar::init($config);
But it works only for the page that i have added to, so please help how can i configure rollbar for cakephp application.
I should put this code in bootstrap.php
use \Rollbar\Rollbar;
// Installs global error and exception handlers
$config = array(
// required
'access_token' => 'ACCESS_TOKEN',
// optional - environment name
'environment' => 'production',
// optional - path to directory your code is in. Used for linking stack traces.
'root' => '/Users/brian/www/myapp'
);
Rollbar::init($config);

Setting Environment-specific database in CakePHP

I am using CakePHP and was trying to implement https://github.com/josegonzalez/cakephp-environments
Which seemed to be going fine except that I have no idea where to specify the env specific database info.
Does anyone know where to set these?
I personally haven't used the plugin, however from looking at the code and the docs, if you were using the suggested database configuration, then it seems that you would define the options as either environment variables, which can be done in various ways, for example
in your server configuration (Apache example)
in your cloud varibale settings (Heroku example)
manually using putenv(), $_ENV, $_SERVER
$name = 'MYSQL_DB_HOST';
$value = 'localhost';
putenv("$name=$value");
$_ENV[$name] = $value;
$_SERVER[$name] = $value;
...
or as CakePHP configuration values via the Environment::configure() calls, something like:
Environment::configure('development',
true,
array(
'MYSQL_DB_HOST' => 'localhost',
'MYSQL_USERNAME' => 'user',
// ...
),
// ...
);

CakePHP 1.3 cleares all cached pages after adding new post

I am using CakePHP 1.3 and trying to enable cache for view pages, cache system works fine and caches all pages. But when we add a new post (insert new record to database) or edit an old one (update a record of the table) CakePHP deletes all cached pages, not just the edited page!
app/config/core.php :
Cache::config('default', array('engine' => 'File','duration' => 8640000));
app/controllers/articles_controller.php :
var $helpers = array('Cache');
var $cacheAction = array(
'view' => array('duration' => 8640000),
'latest' => array('duration' => 8640000),
);
How can I tell Cake to delete just the cached version of changed page and not all cached pages?
This it actually pretty hard, so I can't just give you a piece of code to solve this. You need to edit the actual cake files in the lib folder that manage caching. Note: this is super not recommended by the cake people. However the lib/Cake/Cache/Engine/FileEngine.php is the file that has the operations of the file engine. You seem interested in the delete function:
/**
* Delete a key from the cache
*
* #param string $key Identifier for the data
* #return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
*/
public function delete($key) {
if ($this->_setKey($key) === false || !$this->_init) {
return false;
}
$path = $this->_File->getRealPath();
$this->_File = null;
//#codingStandardsIgnoreStart
return #unlink($path);
//#codingStandardsIgnoreEnd
}
Also, instead of editing the core cake files you could add your own file engine and use parted of the cake engine by moving the code and just extending the code there (that's totally cool in open source).
Its also possible that by reading the code used to implement the file caching engine you will find your actual solution. Good Luck.

Redirect to controller action if file not found

I'm trying to configure the rewrite rules for lighttpd so that a specific cakephp controller-action is executed if a file is not found. The controller-action will generate the data (png file), save it for future use, and then serve it to the client. The next time someone tries to access the file, it will be served by lighttpd directly without executing any php. In other words, the png files are cached so there is no need to recreate them.
From what I can tell, url.rewrite-if-not-file is ignored if rewrite-once has a match. Thus, the following can serve up my cached files but not my uncached files.
url.rewrite-if-not-file = (
"^/scan/(.+)\.png" => "/mycontroller/scan/$1"
)
url.rewrite-once = (
"^/(css|files|img|js)/(.*)" => "/$1/$2",
"^/favicon.ico" => "/favicon.ico",
"^/scan/(.+\.png)" => "/scan/$1",
"^([^\?]*)(\?(.+))?$" => "/index.php?url=$1&$3",
)
The only solution I can think of now is to delete the 3rd rule and modify ^([^\?]*)(\?(.+))?$ so it ignores urls starting with \scan\.
Any other suggestions?
i'd do something like this in a controller:
if (file_exists($file_name)) {
$this->redirect($file_name);
} else {
$this->redirect(array('action' => 'some_action', 'controller' => 'some_controller'));
}

how to deal with configuration file in CakePHP

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 ?
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:
you can easily define some more constants (like any file path etc).
you can put all your ajax code into external JS files.
you can directly deploy it onto any server without changing in constants as well as work perfectly onto your localhost.
following standard conventions etc.
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.
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');

Resources