Sie sind auf Seite 1von 8

2/2/2017 CakePHP Tutorial for Beginners ­ CodexWorld

Home (http://www.codexworld.com)  »  CakePHP (http://www.codexworld.com/tutorials/cakephp/)   »   CakePHP Tutorial


for Beginners

CakePHP Tutorial for Beginners


By: CodexWorld In: CakePHP (http://www.codexworld.com/tutorials/cakephp/) Last Updated: Oct 30, 2015
Share Tweet

CakePHP is an open source web application framework. It follows the Model-View-Controller (MVC) approach
and written in PHP. CakePHP makes building web applications simpler, faster and require less code.

This CakePHP tutorial will drive you to the right direction for getting started with CakePHP framework and
provide basic guide of CakePHP application development. Our step by step cakePHP tutorial helps beginners
for install and con᷉gures the CakePHP application. You can learn CakePHP from scratch with our easy
tutorial. Also we will develop a sample CakePHP project and it will help you for better understanding the
whole process.

Application 찊ow of the CakePHP are given below:

Download CakePHP
At ᷉rst you need to download the stable release of CakePHP from Github – CakePHP Releases
(https://github.com/cakephp/cakephp/tags)

Basic Con៯�guration
Step1: Extract zip ᷉le and change folder name with your desire project name. For example cakephp/ .
Step2: Move the cakephp/ folder to the localhost server. Your directory setup looks like the following.
/cakephp
/app

http://www.codexworld.com/cakephp­tutorial­beginners/ 1/8
2/2/2017 CakePHP Tutorial for Beginners ­ CodexWorld

/lib
/plugins
/vendors
.htaccess

index.php

README

Step3: Create database at the phpMyAdmin. For example cakephp_db .


Step4: Open the app/Config/core.php ᷉le and make the following changes.
 Change the value of Security.salt at Line no.225.
Before changes:

Configure::write('Security.salt', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi');

After changes:

Configure::write('Security.salt', 'codexworld');

 Change the value of Security.cipherSeed at Line no.230.


Before changes:

Configure::write('Security.cipherSeed', '76859309657453542496749683645');

After changes:

Configure::write('Security.cipherSeed', '123456');

Step5: Rename database.php.default ᷉le to database.php at the app/Config/ directory.


Step6: Open the “app/Con᷉g/database.php” ᷉le and make the following changes.
 Go to the line no.67 and replace the values of host, login, password, database_name with your
database host, database username, database password and database name.
Before changes:

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

After changes:

http://www.codexworld.com/cakephp­tutorial­beginners/ 2/8
2/2/2017 CakePHP Tutorial for Beginners ­ CodexWorld

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

Step7: Run the project URL( http://localhost/cakephp/ ) at the browser.

CakePHP Naming Conventions


Controller Conventions – Controller class names are plural, CamelCased and end in Controller.
( PostsController , LatestPostsController )
Model Conventions – Model class names are singular and CamelCased.( Post , LatestPost )

http://www.codexworld.com/cakephp­tutorial­beginners/ 3/8
2/2/2017 CakePHP Tutorial for Beginners ­ CodexWorld

Database Conventions – Table names corresponding to CakePHP models are plural and underscored.
( posts , latest_posts )
View Conventions – View template ᷉les are named after the controller functions they displayed, in an
underscored form. The postDetails() function of PostController class will look for a view template in
app/View/Post/post_details.ctp . The basic pattern is

app/View/Controller/underscored_function_name.ctp

Sample Project
In this sample project we will create a products table at the cakephp_db database. And we will insert some
data manually at this table. We will fetch and display products in our sample CakePHP project.

Table Creation & Data Insert: Following SQL is used for products table creation.

CREATE TABLE `products` ( 
 `id` int(11) NOT NULL AUTO_INCREMENT, 
 `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
 `description` text COLLATE utf8_unicode_ci NOT NULL, 
 `price` float(10,2) NOT NULL, 
 `created` datetime NOT NULL, 
 `modified` datetime NOT NULL, 
 `status` tinyint(1) NOT NULL DEFAULT '1', 
 PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Once table creation is completed, insert some demo product data into this table.

Controller: Create a products controller with ProductsController class into the app/Controller/ directory.
Controller ᷉le and class name should be ProductsController.

<?php 
class ProductsController extends AppController { 
    public function index() { 
        //fetch products resultset from databse 
        $products = $this‐>Product‐>find('all',array('fields'=>array('Product.id','Product.title','Pro
duct.description','Product.price','Product.created','Product.status'),'conditions'=>array('Product.sta
tus'=>1))); 
 
        //set products data and pass to the view  
        $this‐>set('products',$products); 
    } 
}

View: Create view for display products in app/View/ directory. The controller class name is
ProductsController and method is index. For creating the index view, we need to Products/ directory and a
index.ctp ᷉le. So, the complete path of the view ᷉le ( index.ctp ) would be app/View/Products/index.ctp .

http://www.codexworld.com/cakephp­tutorial­beginners/ 4/8
2/2/2017 CakePHP Tutorial for Beginners ­ CodexWorld

<ul> 
<?php foreach($products as $row): ?> 
    <li> 
        <h1><?php echo $row['Product']['title']; ?></h1> 
        <h6>Price: <?php echo $row['Product']['price']; ?></h6> 
        <p><?php echo $row['Product']['description']; ?></p> 
    </li> 
<?php endforeach; ?> 
</ul> 

Model: Model creation is not required until you need validation or associations.

Routes: Open the app/Config/routes.php ᷉le and set the default controller and action. Go to the line no.27
and change controller name from "pages" to "products" and action name from "display" to "index". If you
want to load the di芪�erent view for this action, you need to pass the view ᷉le name after the action element.

Router::connect('/', array('controller' => 'products', 'action' => 'index'));

Testing: Run the base URL at the browser, products list would displayed.

 Our next post will explain about layout, database and advanced label of CakePHP. Please follow
CodexWorld for notify about the next post.

Previous Next
 PayPal Standard Payment Gateway How to Upload File in PHP 

Integration in PHP (http://www.codexworld.com/php-᷉le-upload/)

(http://www.codexworld.com/paypal-standard-payment-
gateway-integration-php/)

RECOMMENDED TUTORIALS FOR YOU

39 Comments

Vijay Said...

really nice tutorial for beginers

January 15, 2017 at 4:52 PM

Divya Chaubey Said...

http://www.codexworld.com/cakephp­tutorial­beginners/ 5/8
2/2/2017 CakePHP Tutorial for Beginners ­ CodexWorld

very nice tutorial for learning basics. thanks alot

January 7, 2017 at 6:47 AM

Kamaljeet Kaur Said...

WAO great tutorial for beginners of cakephp ….Thanks a lot..

December 19, 2016 at 7:33 AM

Dhaval Said...

thank you ….

October 17, 2016 at 5:27 AM

Ambili R Said...

Very Simple and Understandable.Thank You So much…

October 14, 2016 at 6:19 AM

Khetesh Said...

Thanks a lot. Nice job.

October 12, 2016 at 9:40 AM

Yasar Rana Said...

Very Nyc tutorial

October 1, 2016 at 11:12 AM

Sanjay Said...

sir it was awesome it help me a lot…thanQ.

September 7, 2016 at 7:03 AM

Uday Bhanu Said...

It is so simple and understandable description.

August 19, 2016 at 9:37 AM

Ankit Prajapati Said...

Thank you sooooooooooo much


really a very very helping and easy tutorial.
THANKS

August 8, 2016 at 9:40 AM

Kishor Said...

really good explain

June 23, 2016 at 12:29 PM

Neel Said...

Nice post.Simple and easy to understand for the beginner

June 20, 2016 at 10:47 AM

Arvind Sharma Said...


http://www.codexworld.com/cakephp­tutorial­beginners/ 6/8
2/2/2017 CakePHP Tutorial for Beginners ­ CodexWorld

Realy very good

June 15, 2016 at 9:34 AM

Kanaiya Said...

very nice tutorial for learning basics.

May 11, 2016 at 12:14 PM

Chintan Singh Said...

i need add ,edit and delete funationality tutorial of cake php 2.x version.please provide me soon.Please help me….

April 20, 2016 at 6:20 AM

Mandeep Said...

please send the code of cakephp 3.2 for login,registration page with database connection and with routing. please if
possible…send on my mail mandeepkhaleriya.08@gmail.com.Please (mailto:mandeepkhaleriya.08@gmail.com.Please)
do n’t ignore. i have urgently need for that.please help me……

April 5, 2016 at 9:42 AM

CodexWorld Said...

@Mandeep We’ve already published CakePHP 3.x Tutorial for Beginners


(http://www.codexworld.com/cakephp-3-x-tutorial-for-beginners/), you can read it from here –
http://www.codexworld.com/cakephp-3-x-tutorial-for-beginners/ (http://www.codexworld.com/cakephp-3-x-
tutorial-for-beginners/).

April 5, 2016 at 4:08 PM

« PREV (http://www.codexworld.com/cakephp-tutorial-beginners/comment-page-1/#comments) 1
(http://www.codexworld.com/cakephp-tutorial-beginners/comment-page-1/#comments) 2

Leave a reply

Comment *

Your Name * Your Email * Your Website

Post Comment

CONNECT WITH CODEXWORLD


Subscribe CodexWorld updates via email and get every new post delivered to your inbox.

Enter email address SUBSCRIBE

http://www.codexworld.com/cakephp­tutorial­beginners/ 7/8
2/2/2017 CakePHP Tutorial for Beginners ­ CodexWorld

TRENDING TUTORIALS

Login with Facebook using PHP (http://www.codexworld.com/login-with-facebook-using-php/)

Drag and Drop Reorder Images using jQuery, Ajax, PHP & MySQL (http://www.codexworld.com/drag-drop-images-
reorder-using-jquery-ajax-php-mysql/)

Login with Google Account using PHP (http://www.codexworld.com/login-with-google-api-using-php/)

PayPal Standard Payment Gateway Integration in PHP (http://www.codexworld.com/paypal-standard-payment-


gateway-integration-php/)

Add Remove input ᷉elds dynamically using jQuery (http://www.codexworld.com/add-remove-input-᷉elds-


dynamically-using-jquery/)

Upload multiple images using jQuery, Ajax and PHP (http://www.codexworld.com/upload-multiple-images-using-


jquery-ajax-php/)

CodeIgniter Tutorial for Beginners (http://www.codexworld.com/codeigniter-tutorial-beginners-learn-codeigniter-


scratch/)

Login with Twitter using PHP (http://www.codexworld.com/login-with-twitter-using-php/)

TOPICS

CakePHP CodeIgniter
(http://www.codexworld.com/tutorials/cakephp/) (http://www.codexworld.com/tutorials/codeigniter/)

Drupal (http://www.codexworld.com/tutorials/drupal/) Facebook


(http://www.codexworld.com/tutorials/facebook/)

GoogleAPI GoogleMap
(http://www.codexworld.com/tutorials/googleapi/) (http://www.codexworld.com/tutorials/googlemap/)

htaccess HTML&CSS
(http://www.codexworld.com/tutorials/htaccess/) (http://www.codexworld.com/tutorials/html-css/)

HTML5 (http://www.codexworld.com/tutorials/html5/) JavaScript


(http://www.codexworld.com/tutorials/javascript/)

MySQL (http://www.codexworld.com/tutorials/mysql/) PayPal (http://www.codexworld.com/tutorials/paypal/)

PHP (http://www.codexworld.com/tutorials/php/) Social Media


(http://www.codexworld.com/tutorials/social-media/)

Twitter (http://www.codexworld.com/tutorials/twitter/) Ubuntu


(http://www.codexworld.com/tutorials/ubuntu/)

http://www.codexworld.com/cakephp­tutorial­beginners/ 8/8

Das könnte Ihnen auch gefallen