How do I concatenate global constants and strings in model $actsAs arrays in CakePHP 2.6?
I've got a file upload plugin that needs directory paths fed to it via the $actsAs configuration, but while it lets me just use a global constant OR a string as an array key value, it doesn't let me concatenate two together. This is a problem because theoretically my app could be living in various environments where the path to the app is different, and I'd rather not have this hard-coded and instead use a path relative to the global constant ROOT
.
It looks to me like I'm running up against a limitation of PHP. What good solutions are there to this? Cake-specific / Cake-friendly solutions are a plus.
What works:
// Import.php
public $actsAs = array(
'Uploader.Attachment' => array(
'filename' => array(
// You can use a global constant by itself:
'tempDir' => TMP,
'uploadDir' =>
// Using a full string works, too.
'/var/www/vhosts/hostname/app/webroot/file/imports',
//
'finalPath' => '/file/imports/',
'nameCallback' => 'nameCallback',
'overwrite' => false,
),
),
);
What I'd like to work, but doesn't work. Instead of concatenating, I get an "unexpected '.'" error:
public $actsAs = array(
'Uploader.Attachment' => array(
'filename' => array(
'tempDir' => TMP,
// Concatenation of the two results in an "unexpected ." error:
// ROOT being '/var/www/vhosts/hostname/'
'uploadDir' => ROOT . '/app/webroot/file/imports',
//
'finalPath' => '/file/imports/',
'nameCallback' => 'nameCallback',
'overwrite' => false,
),
),
);
You can do that in the constructor:
public function __construct($id = false, $table = null, $ds = null) {
$this->actAs = array(
'Uploader.Attachment' => array(
'filename' => array(
'tempDir' => TMP,
'uploadDir' => ROOT . '/app/webroot/file/imports',
'finalPath' => '/file/imports/',
'nameCallback' => 'nameCallback',
'overwrite' => false,
),
),
);
parent::__construct($id, $table, $ds);
}