Cakephp Invalid key for decrypt(), key must be at least 256 bits (32 bytes) long: Solved

In cakephp 3 I tried to set cookie in my controller using write cookie method. Error got invalid key for decrypt(), key must be at least 256 bits (32 bytes) long – used following line of code.

$this->Cookie->write(‘username’, ‘test’, true, ‘1 year’);

I got following error after execting page.
Invalid key for decrypt(), key must be at least 256 bits (32 bytes) long

Before using cookie write method, I loaded cookie component in appcontroller using following line.
$this->loadComponent(‘Cookie’);

I loaded conponnet in initialize method (appcontroller.php)

After searching I found following solution. I changed app.php file and added following line of code

'Security' => [
        'salt' => '123456789purab@eparinay!@#*$($(',
    ]

Above solution worked for me and it solved issue.

If you are getting this error means your Security.salt in app.php does not have the correct length.

cakephp 3 getOriginalSubject undefined index issue fixed

I am using cakephp 3.1 for development one of my portal. While configuring email Igot undefined index getOriginalSubject in DebugkitTransport.php. I searched for some time on net for solution but I did not find any solution. This function tries to get subject from email obejct. I think this is bug in cakephp 3.1.

My solution is as follows. I opened DebugkitTransport.php file and in send function I changed following line


/fixing issue of $email->getOriginalSubject undefined index
$headers['Subject'] = $email->subject();

Above line fixed my issue and I am able to send email now. I using smtp setting for sending email. I am using following code for one of my site called http://eparinay.com


$email = new Email();
$email->transport('eparinay');
try {
$email->template('eparinay')
->emailFormat('html')
->to($email)
->bcc('purabdk@yahoo.co.in')
->from(['noreply@eparinay.com' => 'eParinay.com'])
->viewVars(array('username' => $username, 'email' => $email))
->subject('eParinay.com: New registration.')
->send();
} catch (Exception $e) {

echo 'Exception : ', $e->getMessage(), "\n";
}

 

Get base url or development URL in cakephp 3 and use in view ctp file

Getting baseurl in any framework is easy. But development URL in cakephp 3 in view file…took time…For adding css or js or other purpose we need base path cakephp.

While doing development my path was http://localhost/cakephp3/. I wanted to fetch that in my site.
Their are many ways we can get the path.

You can use any one method of following :

<?php 
    echo $this->Html->url('/', true)
    echo $this->Html->url('/');
    ?>

Define constant in Config/core.php as given bellow:

define(“BASE_URL”, “localhost/cakephp3/”);

Then you can use BASE_URL in view file.

For view file you can use following code.

<?php echo $this->Url->build('/', true); ?>

I used above code reference to following URL:
http://book.cakephp.org/3.0/en/views/helpers/url.html#generating-urls

 

Cakephp installation and blog tutorial step by step

CakePHP is one of the best PHP MVC framework which is purely inspired by Ruby on Rails. I worked on ROR for many years. So I know CakePHP has very similar ROR standards. Here In this article I will show you about CakePHP installation on linux box in very detailed information. I created the very basic blog application using CakePHP and It is free for download.

Cakephp installation and blog tutorial step by step

I am using the linux box for installation following commands will be useful for any linux installation. You can use the any linux OS like (Ubuntu, Fedora, Redhat, centos, etc… )

#pear channel-discover pear.cakephp.org
#pear remote-list -c cakephp
#pear install cakephp/CakePHP

Go to folder where you want to create the project
#cd /var/www/html/

Create the project using following command
#cake bake project cakeblog


{
"name": "cakeblog",
"repositories": [
{
"type": "pear",
"url": "http://pear.cakephp.org"
}
],
"require": {
"pear-cakephp/cakephp": ">=2.4.0"
},
"config": {
"vendor-dir": "Vendor/"
}
}

#cd cakeblog
#chown purab:purab -R cakeblog
#chown apache:apache -R cakeblog/tmp

Download debug_kit
https://github.com/cakephp/debug_kit
Add into the Plugin Dir

Download commposer
http://getcomposer.org/download/

using following command and console:
#curl -sS https://getcomposer.org/installer | php

Create the composer.json file in cakeblog folder and add following lines in that file

After that execute the following command

#php composer.phar install

Open the webroot/index.php file and add the following lines at line no 82.


define(
'CAKE_CORE_INCLUDE_PATH',
ROOT . DS . APP_DIR . '/Vendor/pear-pear.cakephp.org/CakePHP'
);

You can download my Blog code from Following Link.

[sociallocker]https://github.com/purab/cakeblog[/sociallocker]

Reference Used:
http://book.cakephp.org/2.0/en/getting-started.html#blog-tutorial

http://book.cakephp.org/2.0/en/installation/advanced-installation.html