Today we have some interesting topics to be discussed. Refreshing and updating your knowledge will help you when you are in need. Now here we are going to take you through a detailed tour on the concepts of CakePHP.
CakePHP as a the name sounds, it itself is smooth and easy.
It is a powerful and robust framework which is developed around MVC (i.e. Model-View-Controller (MVC)). It also gives you flexible way to build your application.
Requirements for CakePHP
- PHP 5.2.8 or greater
- It also work with other servers like Lighttpd or Microsoft IIS.
- MySQL, PostgreSQL, Microsoft SQL Server or SQLite.
In this blog you will learn how to install CakePHP and get started with the same. Before going forward I assume that you have already running your own cloud server instance and a web server. We will use Ubuntu for the Operating System and Apache for the Web server (In addition to that we will be using PHP and MYSQL too).
Steps to install CakePHP on an Ubuntu Server:
Step – 1: CakePHP Installing
There are various ways with the help of which you can install CakePHP on your server.
- You can download and unpack the archive available on the CakePHP website.
- You can also use the Git to clone a release from Github.
- You can also use the PEAR.
The first is the most reliable way
In that case the only thing you need is to download the latest stable release (link for the same we can find it from CakePHP website home page). Unzip the it into a folder. Execute the below mentioned steps to navigate to our web server root folder and download the archive:
wget https://github.com/cakephp/cakephp/zipball/<Latest Stable Version>
LInk is for the CakePHP to download. In the <Latest Stable Version> you will be having the latest version number.
Once you execute the above statement. Then the a .zip file with the release version as the name will get downloaded. Next step will be to unzip the file, if we do not want to unzip the file then execute the below mentioned step in that case:
sudo apt-get install unzip
After executing the above statement you will able to unzip now. Just make sure that you replace the file name with one which you have downloaded.
After executing above statement you will be able to get a new folder that has all the CakePHP files. Once you are able to see the same, go ahead and rename folders to something more useful like project.
mv cakephp-cakephp-4b9e390 project
Please make sure that you replace the proper folder name. It must be replaced with the one you got after unzipping the archive. After executing above command it will rename it to project. Hence, after moving you will find your CakePHP in folder called /var/www/project folder.
We have downloaded the CakePHP. It is also moved to another location. Next step is to apply permissions for the app/tmp folder of your application as CakePHP. You will need to use it quite a bit. For Apache server run following command within /var/www/project folder:
cd project chown -R root:www-data app/tmp chmod -R 775 app/tmp
The above statement will change the owner ship of the folder. You can allocate the whole thing beneath it to root user. www-data group has the www-data group user that Apache uses to run the processes. The second command sets the folder permissions such that www-data group can write it.
Step – 2: URL Rewriting
CakePHP usually works fine with clean URLs. Hence, we make sure that our webserver allows URL rewriting. It means to configure URL Rewriting we will need to enabled the mod_rewrite option of Apache. We also need to check that virtual host allows the htaccess files to do their overriding. So verify that mod_rewrite is enabled on your system by executing below steps:
If you are not able to rewrite as above then edit the Apache Default Virtual host file. Then make sure that Allow Overrides is set to All for the /var/www/ directory. You need to just edit the file with following command.
And where you see below block, make changes as below
Options Indexes FollowSymLinks MultiViews
allow from all
Which will make sure that .htaccess files can overrides the default apache instructions. After such steps make sure that you restart the Apache server.
Step – 3: Additional Configuration
Once done with URL Rewriting, let’s focus on some security related configuration for CakePHP. It is named as Salt and Seed.
Salt: Custom string use by CakePHP in security hashes
Seed: Custom number use for encryption
Set it now so there will not be any last minute hiccups while we use CakePHP
In order to set the same edit the core.php file located at app/Config
/** * A random string used in security hashing methods. */ Configure::write('Security.salt', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi'); /** * A random numeric string (digits only) used to encrypt/decrypt strings. */
Please note actual string and number may differ in your case
Once done save and exit out.
Step – 4: Connecting with Database
It is not necessary to setup the database for CakePHP. But most of web applications need to store information. Thus, lets setup connection with MySQL which we will do now:
We probably need database with 1 table and 5 columns (id,title,body,created,modified) out of which id should be autoincremented
Login to your MySQL Database From console create database
create database cake;
After that create table under this database by executing below mentioned steps:
And now you can create the table:
CREATE TABLE cake1( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, title VARCHAR(50), body TEXT, created DATETIME DEFAULT NULL, modified DATETIME DEFAULT NULL );
After that we will insert some data in to the database INSERT INTO cake1 (title,body,created) VALUES ('Title 1', 'Some body text.', NOW());
Last but not least configure your CakePHP by following steps below:
- Take copy of database.php.default located in /app/Config folder
- Name it as database.php
- Open that file and locate the following block. Then change cakeBlog and c4k3-4UI3Z to your mysql login and password
public $default = array( 'datasource' => 'Database/Mysql', 'persistent' => false, 'host' => 'localhost', 'port' => '', 'login' => 'cakePHP', 'password' => 'c4k3-rUl234', 'database' => 'cake', 'schema' => '', 'prefix' => '', 'encoding' => 'utf8' );
You will need to give database connection information specifically to your case. Then save the file and exit out. Navigate to your browser folder and locate project folder and run below
You will be now able to see CakePHP default page. If you are getting timezone error then you can go ahead and uncomment following in core.php file located in app/Config:
/** * Uncomment this line and correct your server timezone to fix * any date & time related errors. */ date_default_timezone_set('UTC');
So you have seen the tutorial and now you are good to go for configuring the CakePHP which is really easy. In case any problem then gets back to us.