custom config file error - cakephp

I want to a create a config file for site configuration. I create config.php and put it into the Config folder. It's its content:
<?php
Configure::write('Title', 'My App');
Configure::write('Categories', array(
'Recipes' =>
array('url' => '/recipe', 'max' => 10),
'Foods' =>
array('url' => '/food', 'max' => 5)
));
And put this code at the end of bootstrap.php:
Configure::load('config');
But cake returns this error:
No variable $config found in ...........\app\Config\config.php.php
Error: An Internal Error Has Occurred.
Stack Trace
CORE\Cake\Core\Configure.php line 272 → PhpReader->read(string)
APP\Config\bootstrap.php line 110 → Configure::load(string)
CORE\Cake\Core\Configure.php line 92 → include(string)
CORE\Cake\bootstrap.php line 142 → Configure::bootstrap(boolean)
APP\webroot\index.php line 79 → include(string)
What's the problem??
Note: I'm using the latest version: 2.2.1 Stable

In your config file you should be using an array variable called $config instead of Configure::write if you want to use load() with the default PhpReader. See API
Ex.
<?php
$config = array(
'Title' => 'MyApp',
...
);
http://book.cakephp.org/2.0/en/development/configuration.html#built-in-configuration-readers

Related

Application_Model_Classname not found in Zend framework

I am using following directory structure in Wamp server and i am getting an error Fatal error: Class 'Application_Model_ClassName' not found:
www--->pnp--->client--->application
--->controllers
--->modules
--->models
--->docs
--->library
--->public
--->.htaccess
--->.buildpath
But when i remove client directory from the above structure its working fine. With the following structure its working fine.
www--->pnp--->application
--->controllers
--->modules
--->models
--->docs
--->library
--->public
--->.htaccess
--->.buildpath
Please suggest, so that i can use client or dev or qa directory.
We need to autoload model files in the the Bootstrap.php, with the following code.
protected function _initAutoLoad ()
{
$autoLoader = Zend_Loader_Autoloader::getInstance();
$resourceLoader = new Zend_Loader_Autoloader_Resource(
array('basePath' => APPLICATION_PATH, 'namespace' => 'Application',
'resourceTypes' => array(
'form' => array('path' => 'forms/', 'namespace' => 'Form'),
'model' => array('path' => 'models/', 'namespace' => 'Model'))));
return $autoLoader;
}

How can I create cached files within CakePHP Plugin?

I have created a plugin for CakePHP that I would like to generate cached files within its own plugin folder at /app/Plugin/MyPlugin/tmp/cache.
I have already cerated the MyPlugin/tmp/cache directory manually.
I have created a bootstrap file at /app/Plugin/MyPlugin/Config/bootstrap.php with the following content:
<?php
Cache::config('short', array(
'engine' => 'File',
'duration' => '+60 minutes',
'path' => 'Plugin/MyPlugin/tmp/cache',
'prefix' => 'cake_short_',
'mask' => 0666,
));
I have created a Shell script at /app/Plugin/MyPlugin/Console/Command/MyPluginShell.php with the following function:
<?php
...
public function get_listings() {
$listings = $this->Listing->find('all');
Cache::write('listings', $listings, 'short');
$this->out('Task Completed');
}
I can run get_listings from within the Cake console just fine and the Task completes, however there is no Cache file being created at /app/Plugin/MyPlugin/tmp/cache like I would expect.
On a side note, I have tried replacing 'path' => 'Plugin/MyPlugin/tmp/cache' with 'path' => CACHE just to see if it will appear in app/tmp/cache but haven't had any luck.
I have also created the /app/Plugin/MyPlugin/tmp/cache within the plugin.
Any help would be appreciated.
I figured it out.
I wasn't loading the plugin correctly within app/Config/bootstrap.php.
I was doing this (incorrect):
CakePlugin::load('MyPlugin', array('routes' => true));
Instead of this (correct):
CakePlugin::load('MyPlugin', array('routes' => true, 'bootstrap' => true));

CakePHP: Sending Email From Wamp Shows Error Internal Error

I am trying to send email in Cakephp using WAMP. I copied the code from CAKEPHP website but it does't seem to work and it does't provide any helpful information to debug.
This is the error with no further information I have debug kit installed but it also does't show any information.
**
SMTP timeout.
Error: An Internal Error Has Occurred.
**
In Controller I have
App::uses('CakeEmail', 'Network/Email');
App::uses('AppController', 'Controller');
in my function inside controller I have
public function contact(){
$email = new CakeEmail('gmail');
$email->to('shahid.abdullah.android#gmail.com');
$email->subject('Email testing Subject');
$email->send('Email testing content');
}
In app/config/email.php I have
<?php
class EmailConfig {
public $gmail = array(
'host' => 'smtp.gmail.com',
'port' => 465,
'username' => 'myemail#gmail.com',
'password' => 'password**',
'transport' => 'Smtp',
'tls' => true
);
}
You might try changing smtp.gmail.com to ssl://smtp.gmail.com and remove 'tls' => true.
Also you might want to try having a from paramaeter in your contact() function such as:
$email->from('example#domain.com');

unload() CakePHP Uploader Plugin does not seem to work

I'm using CakePHP 2.1.1 and Miles Johnson's Uploader Plugin v 3.5.
It's working pretty sweet although I think I have a problem when trying to unload the FileValidation behavior.
I have set up both the behaviors Uploader.Attachment and Uploader.FileValidator (see the bottom of the question).
In the afterSave callback I now need to save the Post again to add a Translated field for a different locale.
When I do save again, this seems to cause en error in the FileValidation behavior. I get the error:
failed to open stream: No such file or directory [APP/Plugin/Uploader/Model/Behavior/FileValidationBehavior.php, line 296]
Somehow the Behavior is looking again for the tmp file.
When I do not define the FileValidation behavior at all, all goes well. So I figured to disable the behavior after it has done it's job during regular save(), right before I go for my second save().
In afterSave($created) I therefore state
$this->Behaviors->unload('FileValidation');
$this->save($data);
The error disappears, but I get 4 warnings in return:
Warning (512): Could not find validation handler maxWidth for file [CORE/Cake/Model/Model.php, line 3155]
Warning (512): Could not find validation handler maxHeight for file [CORE/Cake/Model/Model.php, line 3155]
Warning (512): Could not find validation handler filesize for file [CORE/Cake/Model/Model.php, line 3155]
Warning (512): Could not find validation handler required for file [CORE/Cake/Model/Model.php, line 3155]
I also tried $this->Behaviors->disable('FileValidation') but to no avail.
Is this a bug in the Behavior (not properly unloading itself) or am I not properly handling the unload?
kind regards,
Bart
The behavior setup:
public $actsAs = array('Uploader.Attachment' => array(
'file' => array(
'name' => 'uniqueFilename', // Name of the function to use to format filenames
'baseDir' => APP, // See UploaderComponent::$baseDir
'uploadDir' => 'webroot/img/upload/', // See UploaderComponent::$uploadDir
'dbColumn' => 'uploadPath', // The database column name to save the path to
'importFrom' => '', // Path or URL to import file
'defaultPath' => '', // Default file path if no upload present
'maxNameLength' => 30, // Max file name length
'overwrite' => false, // Overwrite file with same name if it exists
'stopSave' => true, // Stop the model save() if upload fails
'transforms' => array(), // What transformations to do on images: scale, resize, etc
's3' => array(), // Array of Amazon S3 settings
'metaColumns' => array( // Mapping of meta data to database fields
'ext' => 'ext',
'type' => 'type',
'size' => 'size',
'group' => 'group',
'width' => 'width',
'height' => 'height',
'filesize' => 'size',
'name'=>'name'
)
)
),
'Uploader.FileValidation' => array(
'file' => array(
'maxWidth' => array(
'value' => 1000,
'error' => 'Image too wide. Max 1000px'
),
'maxHeight' => array(
'value' => 1000,
'error' => 'Image too high. Max 1000px'
),
'extension' => array(
'value' => array('gif', 'jpg', 'png', 'jpeg'),
'error' => 'Mimetype incorrect',
),
'filesize' => array(
'value' => 1048576,
'error' => 'Filesize too high. Max 1 MB'
)
)
)
);
Not directly related, but are you game to try another file upload plugin? This one might do the trick: http://bakery.cakephp.org/articles/srs2012/2012/03/12/ajaxmultiupload_plugin_for_cake_2_0_x_and_2_1
Not sure if this will fix yours but I found (for some reason I don't know why) this error gets thrown if you don't have debug set to 0 in your Config/core.php

CakePHP page with no headers/footers

In a download page for a blob from a database, how would I make it so that no other output is sent? Right now it's sending the header, debug info, and a footer. How do I make it so that none of that is sent, just for that view?
you can create an clear layout (e.g. empty.ctp ) in you layouts folder, only with
<?php echo $content_for_layout ?>
and then in you action where you're getting your blob data use that layout
$this->layout = 'empty.ctp';
and also to disable debugging, in your controllers use
Configure::write('debug',0);
if you're unable to create new layout you could try this.
$this->layout = null;
$this->render("view_name");
If you're using this to download files, you should use the Media view in cakePHP
http://book.cakephp.org/view/1094/Media-Views
$this->view = 'Media';
$params = array(
'id' => 'example.zip',
'name' => 'example',
'download' => true,
'extension' => 'zip', // must be lower case
'path' => APP . 'files' . DS // don't forget terminal 'DS'
);
CakePhp 2.3 users :
use Sending files from the Book
CakePhp 2.x users :
use '$this->viewClass' instead of '$this->view'
copy-paste ready full solution, right in any controller file:
<?php
public function download($file) {
$fsTarget = APP.WEBROOT_DIR.DS.'files'.DS.$file; // files located in 'files' folder under webroot
if (false == file_exists($fsTarget)){
throw new NotFoundException(__('Invalid file'));
}
$pathinfo = pathinfo($fsTarget);
$this->viewClass = 'Media';
$params = array(
'id' => $file,
'name' => $pathinfo['filename'], // without extension
'download' => true,
'extension' => $pathinfo['extension'], // must be lower case
'path' => dirname($fsTarget) . DS // don't forget terminal 'DS'
);
$this->set($params);
}
Hope this helps!

Resources