Introduction to CakePHP

CakePHP, the sweet cake. Ever you tested it? If not, then it is the time to test it’s sweetness. From this tutorial, I am starting new series of CakePHP. Progressively we will dig dipper into it, very fast.

Cake logo

What is CakePHP?
If you don’t know, what CakePHP is? Then, CakePHP is the old boy in the family of PHP framework. CakePHP is an open source web application framework. It is one of the oldest PHP frameworks in this world. CakePHP started from back end 2005. At that time Zend framework did not present. Since then to till now, CakePHP has very powerful existent in the world of development.

Let’s Run
I think you are happy with the CakePHP introduction. So, it is the time to crack it. You can download it from CakePHP home. That’s it. Extract and rename as cakephp24. Remember you can give this folder name whatever you like. Copy and paste this folder into your htdocs. Open your web browser and go to –

localhost/cakephp24

Make sure, you already run your apache and MySQL servers. What you see?
Some red, green and yellow boxes, right? All of these have some meaning. I will explain one by one.

Beginning screen

Before explaining these colorful boxes, I think, we should get know with CakePHP folder structure.

cake folder structure

Yes, this is the long listing folder you will get when you open the CakePHP folder. One thing need to mention, you may not see .htaccess and other (.) files if you open through normal window. I am using Sublime Text as an editor. Normally, it shows everything inside the folder.

Inside cakephp24 folder there are 4 folders.

  • app: This folder contains all the MVC contents, configuration and other necessary folders for the project.
  • lib: This folder contains CakePHP core files. Never touch this folder.
  • plugins: It contains third party plugins.
  • vendor: This folder contains vendor related files.

Now let’s try to solve our colorful opening screen. The first two red boxes said that, we need to fix two security salts inside App/Config/Core.php. Open this file and replace the existing salt with some other secret text.

/**
 * A random string used in security hashing methods.
 */
	Configure::write('Security.salt', 'ShahjalalHossain');

/**
 * A random numeric string (digits only) used to encrypt/decrypt strings.
 */
	Configure::write('Security.cipherSeed', 'ShahjalalHossain');

Greens are good. So, now go to the yellow boxes. It says, we need to configure our database from App/Config/database.php. Here you may not find this database.php file. Rather, you will find database.php.default. Rename this file to database.php. And, set the database info-

class DATABASE_CONFIG {

	public $default = array(
		'datasource' => 'Database/Mysql',
		'persistent' => false,
		'host' => 'localhost',
		'login' => 'root',
		'password' => 'root',
		'database' => 'cakedb',
		'prefix' => '',
		//'encoding' => 'utf8',
	);

	public $test = array(
		'datasource' => 'Database/Mysql',
		'persistent' => false,
		'host' => 'localhost',
		'login' => 'user',
		'password' => 'password',
		'database' => 'test_database_name',
		'prefix' => '',
		//'encoding' => 'utf8',
	);
}

Here, I have configured only default database. You have to create a database named cakedb. You can do it by phpMyAdmin. For this tutorial we will not need any table. Only database is ok. Now, go to your CakePHP welcome screen. What you see? All red and yellow has gone. Only green boxes are there. It is good, really good. Wait, except one yellow box. At this moment this yellow box is not our headache.

after-configuration

Model for MVC
Now our first task is creating our model class. Usually model class is responsible for containing business logic or database connection with controller. But in CakePHP, it works differently. For this tutorial only an empty model class is enough. So create a file inside Model folder named HelloWorld.php.

class HelloWorld extends AppModel { 

}

Controller for MVC
In CakePHP, controller class contains all the business logics and others necessary methods. So, CakePHP is different then other MVC frameworks. One other important expect is CakePHP believes on plural controller name with Controller word. Little tricky, right? Ok, let me clear it. For our Hello World application the controller name will be HelloWorldsController. I hope you are clear by now. So create a file inside controller folder named as HelloWorldsController.php.

class HelloWorldsController extends AppController{
	
	
	public function index(){
		
	}
}

Yes, a blank index function. We are almost set.

View for MVC
At this time we will use default theme. So create a folder inside the View folder called HelloWorlds. Again, the plural concept of CakePHP. Now, create a file inside View/HelloWorlds/index.ctp. ctp stands for cake template. CakePHP works with templates, a lot. We will see more in near future.

<h2>Hello World!!!</h2>

Simple or what!!! Yes this is CakePHP. Now go to –

localhost/cakephp24/HelloWords

HelloWorld-final-screen

Conclusion
Piece a cake. Yes, this is CakePHP. Everything is as simple as it is. Next tutorial, I will cover some more advance feature of CakePHP, like CRUD.

Happy Coding… 🙂

Introduction to CakePHP

Hej, I’m from Bangladesh. Learning programming is one of the freaking decisions I have taken in my life. Because, it makes me and my life crazy. I have great weakness on open source technologies. Perhaps, that’s why I do not know any closed source language. I fall in love with programming, when I started my undergraduate in East West University. Till now, I can not live without it.
Print Friendly
2 comments on “Introduction to CakePHP
  1. I am fan of this website.. I lern a lot of new things in this website. i am very excite for next part of cake php… 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *