SlideShare ist ein Scribd-Unternehmen logo
1 von 48
Downloaden Sie, um offline zu lesen
CakePHP
                         The Rapid Development PHP Framework
                                    By Khalid Alharbi




Khalid Alharbi, © 2011                                         1
Contents
     What’s CakePHP?
     History.
     Features.
     MVC in CakePHP.

     Naming Convention.
     Core concepts.
     Installing and running Cake.

Khalid Alharbi, © 2011               2
Contents Cont.
      Simple Cake Application.
      Database Cake Application.
      Bake Script.
      Layouts.
      Routes.
      Extending MVC Structure.
      Advanced Cake techniques.
      Cake Community.

Khalid Alharbi, © 2011                    3
What is CakePHP ?
     According to the official CakePHP website, cakephp.org,

              “CakePHP is a rapid development framework for PHP
              that provides an extensible architecture for
              developing, maintaining, and deploying applications.
              Using commonly known design patterns like MVC
              and ORM within the convention over configuration
              paradigm, CakePHP reduces development costs and
              helps developers write less code”




Khalid Alharbi, © 2011                                               4
History
     CakePHP was created by Michal Tatarynowicz On April
        15, 2005.

     Inspired by Roby on Rails.
     Michal published it under MIT licence and opened it to
        the community developers.

     In July 2005, Larry E Masters (aka PhpNut) took over as
        the lead developer.
     In December 2005, Larry and Garrett J. Woodworth (aka
        gwoo) founded the Cake Software Foundation to
        promote development related to CakePHP.


Khalid Alharbi, © 2011
Features
     It’s PHP! Compatibles with versions 4 and 5.
     Open Source, MIT license.
     Object Oriented.
     Design Patterns: MVC and ORM.
     Convention over configuration.
     Framework not set of libraries.
     Bake Script: Automates CRUD scripting.

Khalid Alharbi, © 2011                               6
Features Cont.
      Scaffolding: single line view rendering.
      Helpers: standard HTML, Ajax, and JavaScript helpers to
         create views.

      Customizable Elements: add elements as plugins into the
         application.
      Input validation and data sanitization tools to help create
         secure application.
      Search engine friendly URLs.
      Large growing active community.
      Extremely simple – It’s a piece of Cake!


Khalid Alharbi, © 2011                                               7
MVC Pattern
    Cake enforces Model View Controller (MVC) Pattern.
    Cake splits operations into three parts:
          Models: used for all database interactions.
          Views: used for all output and displays.
          Controllers: used to control the application flow.




Khalid Alharbi, © 2011
How Does MVC Work? (I)
           No MVC PHP App   CakePHP MVC App

Maintenance
headache!
Doesn’t scale
well because
everything is in
the script.




Khalid Alharbi, © 2011
How Does MVC Work? (II)
    ①  The client sends request.
         According to the CakePHP convention, the URL looks like:
         http://{Domain} /{Application}/{Controller}/{Action}/{Parameter 1, etc.}

    ②  The dispatcher script parses the URL, determines which controller to
          execute, and forwards the request to the Controller.

    ③  The action (method) in the controller needs to access data, so it sends
          a database request to the Model.

    ④  The model executes the database request, pulls the output,
    ⑤  The Model sends the output to the Controller.
    ⑥  The Controller sends the output to the corresponding view.
    ⑦  The View adds any design element to the output and sends it to the
          client’s browser.


Khalid Alharbi, © 2011                                                              10
Why CakePHP MVC ?
     Reduce redundancy.
     Organize the different tasks of the web app.
     No need for include statements in PHP scripts.
     Follow Agile techniques.
     Easy to debug and maintain.




Khalid Alharbi, © 2011                                 11
Cake Naming Convention (I)
     Cake adheres the idea of “convention over
         configuration.”

     This organizes the operations of the web
         application.

     It’s strongly recommended to follow Cake's naming
         convention.

     More than one word in the name, must be
         separated by _ when naming the file and camel
         cases when naming the class.

Khalid Alharbi, © 2011                                    12
Cake Naming Convention (II)
      DB Table : Plural form in lowercase letters.
         ex: orders.

      The MVC parts must be named as the following:
                         File Name
                 Class Name
          Base Class      Location
                                                                         Name
     Model
              Singular form of the       The file name in a   AppModel        /app/models
                         table name with .php       Camel case. ex:
                         extension ex: order.php    Order
     Controller
         tablename_controller       The table name       AppController   /app/controllers
                         with .php extension. ex:   appended
                         orders_controller.php      Controller. ex:
                                                    OrdersController

     View
               The action name in the     No classes; a view contains only     /app/views/
                         controller with .ctp       html tags and php scripts.           controllerName
                         extension. ex: add.ctp
Khalid Alharbi, © 2011                                                                                  13
Core Concepts
     Models and Associations.
     Controllers.
     Views.




Khalid Alharbi, © 2011                   14
Models and Association (I)
     The model is the access point to a table in the DB.
     The model can contain data validation rules and
         association information.
     Cake offers Object Relational Mapping (ORM) to
         handle database relationships.
     Using Cake’s ORM, CRUD operations are done
         without writing complex SQL queries.
     Relationships between tables are defined through
         association.


Khalid Alharbi, © 2011                                      15
Models and Association (II)
     Association helps cake link between models
         through relational mapping.

     Association types:
           hasOne: one-to-one relationship. ex: user “has one”
            account.
           hasMany: a user “has many” posts.
           belongsTo: a post “belongs to” one user.
           hasAndBelongsToMany: a post “has and belongs to
            many” tags.



Khalid Alharbi, © 2011                                            16
Controllers
     Controllers handle all logic and requests.
     Use functions like set(), and render() to call out the
         views and provide it with variables.
     Use read(), and find() model’s methods to get a list
         of fields from the database.


     Use the standardized array $this->data to
         handle user form data in the view and runs it
         through the Model.


Khalid Alharbi, © 2011                                         17
Views (I)
     Views handle the presentation layer and displays.
     Views contain HTML and PHP scripts.
     Views are associated with actions in the controller.
     When Cake launches an action in the controller, it
         will automatically render the corresponding view for
         it or display error.
     If the action in the controller is only called by
         another action, then there is no need to create a
         view for it.


Khalid Alharbi, © 2011                                          18
Views (II)
     When a user fills out a form, Cake places the form
         data into the $this->data standard array.
     Cake parses the $this->data like the following:




     User-submitted data can be pulled from
         $this->data array like any PHP array.
         Ex: $this->data['Post']['date']['year’]

Khalid Alharbi, © 2011                                     19
Installing CakePHP (I)
      Before beginning, install these on your localhost:
           Apache server with mod_rewrite.
           PHP 4.3.2 or greater.
           MySQL (the default database engine in Cake) or you can
              use any supported DBMSs: PostgreSQL, Microsoft SQL
              Server 2000, Firebird, IBM DB2,Oracle, SQLite, ODBC, or
              ADOdb.

      You can easily install all of these with XAMPP!
      Download the latest stable release version 1.3.8 of Cake
         from http://cakephp.org.

      Extract the compressed package to your local host root.

Khalid Alharbi, © 2011                                                  20
Installing CakePHP (II)
     You will end up with the following directories:
           /app contains all controllers, models, views, and
            anything related to your application.
           /cake contains the cake’s libraries and scripts.
           /plugins contains plugins packages.
           /vendors contains other independent Cake scripts.




Khalid Alharbi, © 2011                                          21
Running CakePHP
     Before running:
           Change the /tmp folder permission to read and write.
           Change the security.salt value in app/config/core.php
           Create the database.
           Rename app/config/database.php.default to
              database.php and edit the connection settings.

     Go to http://localhost/cakephp




Khalid Alharbi, © 2011                                              22
The Resulting Application




Khalid Alharbi, © 2011                23
The oven is ready!
          Let’s put on the cooking hat and
                 bake our first cake ☺




Khalid Alharbi, © 2011                        24
Hello World! (I)
     The required steps:
           Create the Model: /app/models/example.php




           Create the controller: /app/controllers/
              examples_controller.php




Khalid Alharbi, © 2011                                  25
Hello World! (II)
           Create the view: /app/views/examples/index.ctp

           Running the app: navigate to http://localhost/
              application_name/examples/index




Khalid Alharbi, © 2011                                       26
Blog Application (I)
     Creating a blog using CakePHP.

     The required steps:
         1)  Create a new cake app in localhost root named blog.
         2)  Create the blog’s database.
         3)  Create five tables: users, posts, comments, tags,
             posts_tags.
         4)  Create the models and specify the association
             relationship between them.
         5)  Create the controllers and test the association using
             scaffolding.


Khalid Alharbi, © 2011                                               27
Create the blog’s database (I)
               users table:
     CREATE TABLE `users` ( `id` int(11) unsigned NOT NULL auto_increment,
     `name` varchar(100), `email` varchar(150),`firstname` varchar
     (60),`lastname` varchar(60),PRIMARY KEY (`id`));


               posts table:
     CREATE TABLE `posts` ( `id` int(11) unsigned NOT NULL auto_increment,
     `name` varchar(255), `date` datetime, `content` text, `user_id` int(11),
     PRIMARY KEY (`id`), FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE
     CASCADE ON UPDATE CASCADE );


               comments table:
     CREATE TABLE `comments` ( `id` int(11) unsigned NOT NULL auto_increment,
     `name` varchar(100), `content` text, `post_id` int(11), PRIMARY KEY
     (`id`), FOREIGN KEY(post_id) REFERENCES posts(id) ON DELETE CASCADE ON
     UPDATE CASCADE );

Khalid Alharbi, © 2011                                                          28
Create the blog’s database (II)
               tags table:

     CREATE TABLE `tags` ( `id` int(11) unsigned NOT NULL auto_increment,
     `name` varchar(100), `longname` varchar(255), PRIMARY KEY (`id`) );



               posts_tags table:

     CREATE TABLE `posts_tags` ( `id` int(11) unsigned NOT NULL
     auto_increment, `post_id` int(11) unsigned, `tag_id` int(11) unsigned,
     PRIMARY KEY (`id`), FOREIGN KEY(post_id) REFERENCES posts(id) ON DELETE
     CASCADE ON UPDATE CASCADE, FOREIGN KEY(tag_id) REFERENCES tags(id) ON
     DELETE CASCADE ON UPDATE CASCADE );



                                       The naming convention for foreign
                                        keys is: [singular table name]_id



Khalid Alharbi, © 2011                                                         29
Models and Association
           User Model: /app/models/user.php




           Post Model: /app/models/post.php




           Tag Model: /app/models/tag.php




Khalid Alharbi, © 2011                         30
Controllers and Scaffolding
     Scaffolding is a technique for testing table
         associations without needing to code any HTML.

     To add scaffolding to the application, define the
         $scaffold variable in the controller.
           Posts Controller: /app/controllers/posts_controller.php



           Tags Controller: /app/controllers/posts_controller.php




Khalid Alharbi, © 2011                                                31
The Resulting Application (I)
     Navigate to: http://localhost/blog/posts/add




Khalid Alharbi, © 2011                               32
The Resulting Application (II)
     Navigate to: http://localhost/blog/tags/add




Khalid Alharbi, © 2011                              33
The Bake Script
     The bake script is a shell script that automatically
         generates the basic elements of CakePHP.
     It saves a lot of time generating the needed
         controllers, models, views, and unit tests.
     It helps understand how MVC works in Cake.
     It is located in cake/console/libs/bake.php
     Configuring and using the bake script is illustrated
         in the following link:
         http://www.screencast.com/t/Lyr5li6btec


Khalid Alharbi, © 2011                                       34
Callback methods
     Helps add special logic before or after actions are
         executed.

     Cake supports callback methods for both controllers
         and models.
           Controller callback methods:
               beforeFilter(), beforeRender(), afterFilter(),
                  etc.
           Model callback methods:
               beforeFind(), afterFind(), beforeValidate(),
                  beforeSave(), beforeDelete(), etc.


Khalid Alharbi, © 2011                                         35
Layouts
     Contain the overall design that wraps a view.
     Layouts files must be placed in /app/views/layouts
         directory and named lowercase with .ctp extension.

     The variable $content_for_layout is used to
         place the code for the views.




Khalid Alharbi, © 2011                                        36
Routes
     Mechanism that maps URLs to controller actions.
     Helps customize URLs and maintain the MVC
         structure.
     Helps generate non-HTML files such as PDFs, RSS
         feeds, etc.
     Cake comes configured with a default set of routes:
           Ex: Router::connect('/', array('controller' =>
              'pages', 'action' => 'display', 'home'));

     Routing configuration file is located in:
          /app/config/routes.php.

Khalid Alharbi, © 2011                                       37
Extending MVC Structure
     Cake contains resources that extends the MVC
         structure while maintaining its lightweight
         organization.

     Cake has the following extensions:
           Helper: extends the View’s functionality.
           Component: extends the Controller’s functionality.
           Data Source: extends the Model’s functionality.
           Behavior: extends the Model’s functionality.




Khalid Alharbi, © 2011                                           38
Helpers
      Classes for the presentation layer to extend the
         functionality of the views.
      Contain the presentation logic shared between views,
         elements, or layouts.
      Simplify rendering HTML and processing forms.
      CakePHP has a set of built in helpers:
           HTML, Form, AJAX, Cache, JavaScript, RSS, XML, sessions,
              Time, Text, etc.
      Ex:               <?=$html->link('Add a Post','/posts/add');?>

      Ability to create custom helpers.


Khalid Alharbi, © 2011                                                  39
Components
     Used to extend the functionality of the controllers.
     Components are specific purpose shared actions
         that can be used across all controllers such as
         sending email, etc.
     Components are stored in the /app/controllers/
         components directory.
     Cake comes with built-in components:
           Auth, Session, Cookie, Email, Security, etc.
     Ability to create custom controllers.

Khalid Alharbi, © 2011                                       40
Data Sources
     Connect to extra data-sources and provide data to
         the models.

     Models can reference functions in the DataSource
         without performing extra connections and data
         handling operations.

     Cake comes with built-in DataSources that support
         only relational database systems.

      Ability to create custom data sources and connect
         to REST APIs.

Khalid Alharbi, © 2011                                     41
Behaviors
     Extend the functionality of models without writing a
         set of functions in the models.
     Keep repetitive tasks in separate resource file and
         allow models to access it.
     Example: when updating a record in the database,
         perform many updates across the database.
     Cake comes with built-in behaviors: ACL,
         Containable, Translate, and Tree.
     Ability to create custom behaviors.

Khalid Alharbi, © 2011                                       42
Utilities
     General-purpose libraries that can be called from
         anywhere in the app through the App::import()
         function.

     Cake comes with built-in utilities:
           Configure: storing global variables.
           File and Folder: managing file and folder operations.
           HTTP Socket: managing requests to web services.




Khalid Alharbi, © 2011                                              43
Plugins
     Mini-cake application inside other Cake app.
     Plugins are useful as third-party resources.
     Plugins contain models, views, and controllers just
         like any Cake application.

     Plugins are droped into the app/plugins directory.
     No built-in plugins in Cake only third-party plugins.
     Ability to create custom plugins so they can be
         used by other applications.

Khalid Alharbi, © 2011                                        44
Cake Community (I)
     Cookbook – http://book.cakephp.org
           Official CakePHP documentation.
     Bakery - http://bakery.cakephp.org
           Official CakePHP community portal.
     API - http://api.cakephp.org
     All questions at CakePHP- http://ask.cakephp.org
           Official questions and answers website.
     CakePHP TV - http://tv.cakephp.org
           Free video tutorials.

Khalid Alharbi, © 2011                                   45
Cake Community (II)
     CakeFest - http://cakefest.org
           The CakePHP conference.
     CakeForge - http://cakeforge.org
           Hosts open source CakePHP projects.
     CakePackages - http://www.cakepackages.com
           Free CakePHP code to download.

     GitHub – https://github.com/cakephp/cakephp
           Official CakePHP repository.


Khalid Alharbi, © 2011                              46
References
      Beginning CakePHP: From Novice to Professional By
         David Golding, ISBN: 1-4302-0977-1

      CakePHP Application Development By Anupom Syam
         and Ahsanul Bari, ISBN: 9781847193896

      CakePHP 1.3 Manual Book, http://book.cakephp.org
      CakePHP after 3 years, looking back and moving ahead
         by Gwoo
         http://bakery.cakephp.org/articles/gwoo/2008/04/22/
         after-3-years-looking-back-and-moving-ahead



Khalid Alharbi, © 2011                                         47
Thank You!




Khalid Alharbi, © 2011                48

Weitere ähnliche Inhalte

Was ist angesagt?

CakePHP - Admin Acl Controlled
CakePHP - Admin Acl ControlledCakePHP - Admin Acl Controlled
CakePHP - Admin Acl ControlledLuís Fred
 
2 introduction-php-mvc-cakephp-m2-installation-slides
2 introduction-php-mvc-cakephp-m2-installation-slides2 introduction-php-mvc-cakephp-m2-installation-slides
2 introduction-php-mvc-cakephp-m2-installation-slidesMasterCode.vn
 
Behavior & Specification Driven Development in PHP - #OpenWest
Behavior & Specification Driven Development in PHP - #OpenWestBehavior & Specification Driven Development in PHP - #OpenWest
Behavior & Specification Driven Development in PHP - #OpenWestJoshua Warren
 
Go swagger tutorial how to create golang api documentation using go swagger (1)
Go swagger tutorial how to create golang api documentation using go swagger (1)Go swagger tutorial how to create golang api documentation using go swagger (1)
Go swagger tutorial how to create golang api documentation using go swagger (1)Katy Slemon
 
PHPUnit with CakePHP and Yii
PHPUnit with CakePHP and YiiPHPUnit with CakePHP and Yii
PHPUnit with CakePHP and Yiimadhavi Ghadge
 
Behaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & DrupalBehaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & Drupalsparkfabrik
 
Getting started-with-zend-framework
Getting started-with-zend-frameworkGetting started-with-zend-framework
Getting started-with-zend-frameworkMarcelo da Rocha
 
Phalcon 2 High Performance APIs - DevWeekPOA 2015
Phalcon 2 High Performance APIs - DevWeekPOA 2015Phalcon 2 High Performance APIs - DevWeekPOA 2015
Phalcon 2 High Performance APIs - DevWeekPOA 2015Jackson F. de A. Mafra
 
Php Conference Brazil - Phalcon Giant Killer
Php Conference Brazil - Phalcon Giant KillerPhp Conference Brazil - Phalcon Giant Killer
Php Conference Brazil - Phalcon Giant KillerJackson F. de A. Mafra
 
Plugin architecture (Extensible Application Architecture)
Plugin architecture (Extensible Application Architecture)Plugin architecture (Extensible Application Architecture)
Plugin architecture (Extensible Application Architecture)Chinmoy Mohanty
 
Developing html5 mobile applications using cold fusion 11
Developing html5 mobile applications using cold fusion 11Developing html5 mobile applications using cold fusion 11
Developing html5 mobile applications using cold fusion 11ColdFusionConference
 
Advanced PHP Web Development Tools in 2015
Advanced PHP Web Development Tools in 2015Advanced PHP Web Development Tools in 2015
Advanced PHP Web Development Tools in 2015iScripts
 
Introduction to Using PHP & MVC Frameworks
Introduction to Using PHP & MVC FrameworksIntroduction to Using PHP & MVC Frameworks
Introduction to Using PHP & MVC FrameworksGerald Krishnan
 
PloneNG: What's new in Plone 4.2, 4.3, and beyond
PloneNG: What's new in Plone 4.2, 4.3, and beyondPloneNG: What's new in Plone 4.2, 4.3, and beyond
PloneNG: What's new in Plone 4.2, 4.3, and beyondDavid Glick
 
Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)Viral Solani
 

Was ist angesagt? (20)

CakePHP - Admin Acl Controlled
CakePHP - Admin Acl ControlledCakePHP - Admin Acl Controlled
CakePHP - Admin Acl Controlled
 
CakePHP and AJAX
CakePHP and AJAXCakePHP and AJAX
CakePHP and AJAX
 
2 introduction-php-mvc-cakephp-m2-installation-slides
2 introduction-php-mvc-cakephp-m2-installation-slides2 introduction-php-mvc-cakephp-m2-installation-slides
2 introduction-php-mvc-cakephp-m2-installation-slides
 
CakePHP: An Introduction
CakePHP: An IntroductionCakePHP: An Introduction
CakePHP: An Introduction
 
Behavior & Specification Driven Development in PHP - #OpenWest
Behavior & Specification Driven Development in PHP - #OpenWestBehavior & Specification Driven Development in PHP - #OpenWest
Behavior & Specification Driven Development in PHP - #OpenWest
 
Go swagger tutorial how to create golang api documentation using go swagger (1)
Go swagger tutorial how to create golang api documentation using go swagger (1)Go swagger tutorial how to create golang api documentation using go swagger (1)
Go swagger tutorial how to create golang api documentation using go swagger (1)
 
PHPUnit with CakePHP and Yii
PHPUnit with CakePHP and YiiPHPUnit with CakePHP and Yii
PHPUnit with CakePHP and Yii
 
Behaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & DrupalBehaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & Drupal
 
Getting started-with-zend-framework
Getting started-with-zend-frameworkGetting started-with-zend-framework
Getting started-with-zend-framework
 
Presentation laravel 5 4
Presentation laravel 5 4Presentation laravel 5 4
Presentation laravel 5 4
 
Phalcon 2 High Performance APIs - DevWeekPOA 2015
Phalcon 2 High Performance APIs - DevWeekPOA 2015Phalcon 2 High Performance APIs - DevWeekPOA 2015
Phalcon 2 High Performance APIs - DevWeekPOA 2015
 
Php Conference Brazil - Phalcon Giant Killer
Php Conference Brazil - Phalcon Giant KillerPhp Conference Brazil - Phalcon Giant Killer
Php Conference Brazil - Phalcon Giant Killer
 
Plugin architecture (Extensible Application Architecture)
Plugin architecture (Extensible Application Architecture)Plugin architecture (Extensible Application Architecture)
Plugin architecture (Extensible Application Architecture)
 
Why Laravel?
Why Laravel?Why Laravel?
Why Laravel?
 
Developing html5 mobile applications using cold fusion 11
Developing html5 mobile applications using cold fusion 11Developing html5 mobile applications using cold fusion 11
Developing html5 mobile applications using cold fusion 11
 
Advanced PHP Web Development Tools in 2015
Advanced PHP Web Development Tools in 2015Advanced PHP Web Development Tools in 2015
Advanced PHP Web Development Tools in 2015
 
Introduction to Using PHP & MVC Frameworks
Introduction to Using PHP & MVC FrameworksIntroduction to Using PHP & MVC Frameworks
Introduction to Using PHP & MVC Frameworks
 
PloneNG: What's new in Plone 4.2, 4.3, and beyond
PloneNG: What's new in Plone 4.2, 4.3, and beyondPloneNG: What's new in Plone 4.2, 4.3, and beyond
PloneNG: What's new in Plone 4.2, 4.3, and beyond
 
Phalcon - Giant Killer
Phalcon - Giant KillerPhalcon - Giant Killer
Phalcon - Giant Killer
 
Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)
 

Ähnlich wie Cakephp

Why CakePHP Is Superior to Other Web Frameworks! Examine Its Pros & Cons For ...
Why CakePHP Is Superior to Other Web Frameworks! Examine Its Pros & Cons For ...Why CakePHP Is Superior to Other Web Frameworks! Examine Its Pros & Cons For ...
Why CakePHP Is Superior to Other Web Frameworks! Examine Its Pros & Cons For ...JPLoft Solutions
 
Benefits of cake_php_in_web_development
Benefits of cake_php_in_web_developmentBenefits of cake_php_in_web_development
Benefits of cake_php_in_web_developmentXicom Technologies
 
Trusted PHP Development Services in the USA
Trusted PHP Development Services in the USATrusted PHP Development Services in the USA
Trusted PHP Development Services in the USAtechnoprofiles
 
Rapid Development With CakePHP
Rapid Development With CakePHPRapid Development With CakePHP
Rapid Development With CakePHPEdureka!
 
Building Restful Web App Rapidly in CakePHP
Building Restful Web App Rapidly in CakePHPBuilding Restful Web App Rapidly in CakePHP
Building Restful Web App Rapidly in CakePHPEdureka!
 
Web application development with laravel php framework version 4
Web application development with laravel php framework version 4Web application development with laravel php framework version 4
Web application development with laravel php framework version 4Untung D Saptoto
 
PHP Frameworks and CodeIgniter
PHP Frameworks and CodeIgniterPHP Frameworks and CodeIgniter
PHP Frameworks and CodeIgniterKHALID C
 
Laravel : A Fastest Growing Kid
Laravel : A Fastest Growing KidLaravel : A Fastest Growing Kid
Laravel : A Fastest Growing KidEndive Software
 
Apigility-powered API's on IBM i
Apigility-powered API's on IBM iApigility-powered API's on IBM i
Apigility-powered API's on IBM ichukShirley
 
Principles of MVC for Rails Developers
Principles of MVC for Rails DevelopersPrinciples of MVC for Rails Developers
Principles of MVC for Rails DevelopersEdureka!
 

Ähnlich wie Cakephp (20)

Php Framework
Php FrameworkPhp Framework
Php Framework
 
Php framework
Php frameworkPhp framework
Php framework
 
Why CakePHP Is Superior to Other Web Frameworks! Examine Its Pros & Cons For ...
Why CakePHP Is Superior to Other Web Frameworks! Examine Its Pros & Cons For ...Why CakePHP Is Superior to Other Web Frameworks! Examine Its Pros & Cons For ...
Why CakePHP Is Superior to Other Web Frameworks! Examine Its Pros & Cons For ...
 
Know about cake php framework with vertexplus
Know about  cake php framework with vertexplusKnow about  cake php framework with vertexplus
Know about cake php framework with vertexplus
 
Benefits of cake_php_in_web_development
Benefits of cake_php_in_web_developmentBenefits of cake_php_in_web_development
Benefits of cake_php_in_web_development
 
Trusted PHP Development Services in the USA
Trusted PHP Development Services in the USATrusted PHP Development Services in the USA
Trusted PHP Development Services in the USA
 
cakephp UDUYKTHA (1)
cakephp UDUYKTHA (1)cakephp UDUYKTHA (1)
cakephp UDUYKTHA (1)
 
Rapid Development With CakePHP
Rapid Development With CakePHPRapid Development With CakePHP
Rapid Development With CakePHP
 
Building Restful Web App Rapidly in CakePHP
Building Restful Web App Rapidly in CakePHPBuilding Restful Web App Rapidly in CakePHP
Building Restful Web App Rapidly in CakePHP
 
Web application development with laravel php framework version 4
Web application development with laravel php framework version 4Web application development with laravel php framework version 4
Web application development with laravel php framework version 4
 
Cakephp
CakephpCakephp
Cakephp
 
Cakephp
CakephpCakephp
Cakephp
 
Cakephp
CakephpCakephp
Cakephp
 
Laravel overview
Laravel overviewLaravel overview
Laravel overview
 
PHP Frameworks
PHP FrameworksPHP Frameworks
PHP Frameworks
 
PHP Frameworks and CodeIgniter
PHP Frameworks and CodeIgniterPHP Frameworks and CodeIgniter
PHP Frameworks and CodeIgniter
 
Laravel : A Fastest Growing Kid
Laravel : A Fastest Growing KidLaravel : A Fastest Growing Kid
Laravel : A Fastest Growing Kid
 
Apigility-powered API's on IBM i
Apigility-powered API's on IBM iApigility-powered API's on IBM i
Apigility-powered API's on IBM i
 
Principles of MVC for Rails Developers
Principles of MVC for Rails DevelopersPrinciples of MVC for Rails Developers
Principles of MVC for Rails Developers
 
yii framework
yii frameworkyii framework
yii framework
 

Kürzlich hochgeladen

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 

Kürzlich hochgeladen (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

Cakephp

  • 1. CakePHP The Rapid Development PHP Framework By Khalid Alharbi Khalid Alharbi, © 2011 1
  • 2. Contents  What’s CakePHP?  History.  Features.  MVC in CakePHP.  Naming Convention.  Core concepts.  Installing and running Cake. Khalid Alharbi, © 2011 2
  • 3. Contents Cont.   Simple Cake Application.   Database Cake Application.   Bake Script.   Layouts.   Routes.   Extending MVC Structure.   Advanced Cake techniques.   Cake Community. Khalid Alharbi, © 2011 3
  • 4. What is CakePHP ?  According to the official CakePHP website, cakephp.org, “CakePHP is a rapid development framework for PHP that provides an extensible architecture for developing, maintaining, and deploying applications. Using commonly known design patterns like MVC and ORM within the convention over configuration paradigm, CakePHP reduces development costs and helps developers write less code” Khalid Alharbi, © 2011 4
  • 5. History   CakePHP was created by Michal Tatarynowicz On April 15, 2005.   Inspired by Roby on Rails.   Michal published it under MIT licence and opened it to the community developers.   In July 2005, Larry E Masters (aka PhpNut) took over as the lead developer.   In December 2005, Larry and Garrett J. Woodworth (aka gwoo) founded the Cake Software Foundation to promote development related to CakePHP. Khalid Alharbi, © 2011
  • 6. Features  It’s PHP! Compatibles with versions 4 and 5.  Open Source, MIT license.  Object Oriented.  Design Patterns: MVC and ORM.  Convention over configuration.  Framework not set of libraries.  Bake Script: Automates CRUD scripting. Khalid Alharbi, © 2011 6
  • 7. Features Cont.   Scaffolding: single line view rendering.   Helpers: standard HTML, Ajax, and JavaScript helpers to create views.   Customizable Elements: add elements as plugins into the application.   Input validation and data sanitization tools to help create secure application.   Search engine friendly URLs.   Large growing active community.   Extremely simple – It’s a piece of Cake! Khalid Alharbi, © 2011 7
  • 8. MVC Pattern  Cake enforces Model View Controller (MVC) Pattern.  Cake splits operations into three parts:   Models: used for all database interactions.   Views: used for all output and displays.   Controllers: used to control the application flow. Khalid Alharbi, © 2011
  • 9. How Does MVC Work? (I) No MVC PHP App CakePHP MVC App Maintenance headache! Doesn’t scale well because everything is in the script. Khalid Alharbi, © 2011
  • 10. How Does MVC Work? (II) ①  The client sends request. According to the CakePHP convention, the URL looks like: http://{Domain} /{Application}/{Controller}/{Action}/{Parameter 1, etc.} ②  The dispatcher script parses the URL, determines which controller to execute, and forwards the request to the Controller. ③  The action (method) in the controller needs to access data, so it sends a database request to the Model. ④  The model executes the database request, pulls the output, ⑤  The Model sends the output to the Controller. ⑥  The Controller sends the output to the corresponding view. ⑦  The View adds any design element to the output and sends it to the client’s browser. Khalid Alharbi, © 2011 10
  • 11. Why CakePHP MVC ?  Reduce redundancy.  Organize the different tasks of the web app.  No need for include statements in PHP scripts.  Follow Agile techniques.  Easy to debug and maintain. Khalid Alharbi, © 2011 11
  • 12. Cake Naming Convention (I)  Cake adheres the idea of “convention over configuration.”  This organizes the operations of the web application.  It’s strongly recommended to follow Cake's naming convention.  More than one word in the name, must be separated by _ when naming the file and camel cases when naming the class. Khalid Alharbi, © 2011 12
  • 13. Cake Naming Convention (II)   DB Table : Plural form in lowercase letters. ex: orders.   The MVC parts must be named as the following: File Name Class Name Base Class Location Name Model Singular form of the The file name in a AppModel /app/models table name with .php Camel case. ex: extension ex: order.php Order Controller tablename_controller The table name AppController /app/controllers with .php extension. ex: appended orders_controller.php Controller. ex: OrdersController View The action name in the No classes; a view contains only /app/views/ controller with .ctp html tags and php scripts. controllerName extension. ex: add.ctp Khalid Alharbi, © 2011 13
  • 14. Core Concepts  Models and Associations.  Controllers.  Views. Khalid Alharbi, © 2011 14
  • 15. Models and Association (I)  The model is the access point to a table in the DB.  The model can contain data validation rules and association information.  Cake offers Object Relational Mapping (ORM) to handle database relationships.  Using Cake’s ORM, CRUD operations are done without writing complex SQL queries.  Relationships between tables are defined through association. Khalid Alharbi, © 2011 15
  • 16. Models and Association (II)  Association helps cake link between models through relational mapping.  Association types:   hasOne: one-to-one relationship. ex: user “has one” account.   hasMany: a user “has many” posts.   belongsTo: a post “belongs to” one user.   hasAndBelongsToMany: a post “has and belongs to many” tags. Khalid Alharbi, © 2011 16
  • 17. Controllers  Controllers handle all logic and requests.  Use functions like set(), and render() to call out the views and provide it with variables.  Use read(), and find() model’s methods to get a list of fields from the database.  Use the standardized array $this->data to handle user form data in the view and runs it through the Model. Khalid Alharbi, © 2011 17
  • 18. Views (I)  Views handle the presentation layer and displays.  Views contain HTML and PHP scripts.  Views are associated with actions in the controller.  When Cake launches an action in the controller, it will automatically render the corresponding view for it or display error.  If the action in the controller is only called by another action, then there is no need to create a view for it. Khalid Alharbi, © 2011 18
  • 19. Views (II)  When a user fills out a form, Cake places the form data into the $this->data standard array.  Cake parses the $this->data like the following:  User-submitted data can be pulled from $this->data array like any PHP array. Ex: $this->data['Post']['date']['year’] Khalid Alharbi, © 2011 19
  • 20. Installing CakePHP (I)   Before beginning, install these on your localhost:   Apache server with mod_rewrite.   PHP 4.3.2 or greater.   MySQL (the default database engine in Cake) or you can use any supported DBMSs: PostgreSQL, Microsoft SQL Server 2000, Firebird, IBM DB2,Oracle, SQLite, ODBC, or ADOdb.   You can easily install all of these with XAMPP!   Download the latest stable release version 1.3.8 of Cake from http://cakephp.org.   Extract the compressed package to your local host root. Khalid Alharbi, © 2011 20
  • 21. Installing CakePHP (II)  You will end up with the following directories:   /app contains all controllers, models, views, and anything related to your application.   /cake contains the cake’s libraries and scripts.   /plugins contains plugins packages.   /vendors contains other independent Cake scripts. Khalid Alharbi, © 2011 21
  • 22. Running CakePHP  Before running:   Change the /tmp folder permission to read and write.   Change the security.salt value in app/config/core.php   Create the database.   Rename app/config/database.php.default to database.php and edit the connection settings.  Go to http://localhost/cakephp Khalid Alharbi, © 2011 22
  • 23. The Resulting Application Khalid Alharbi, © 2011 23
  • 24. The oven is ready! Let’s put on the cooking hat and bake our first cake ☺ Khalid Alharbi, © 2011 24
  • 25. Hello World! (I)  The required steps:   Create the Model: /app/models/example.php   Create the controller: /app/controllers/ examples_controller.php Khalid Alharbi, © 2011 25
  • 26. Hello World! (II)   Create the view: /app/views/examples/index.ctp   Running the app: navigate to http://localhost/ application_name/examples/index Khalid Alharbi, © 2011 26
  • 27. Blog Application (I)  Creating a blog using CakePHP.  The required steps: 1)  Create a new cake app in localhost root named blog. 2)  Create the blog’s database. 3)  Create five tables: users, posts, comments, tags, posts_tags. 4)  Create the models and specify the association relationship between them. 5)  Create the controllers and test the association using scaffolding. Khalid Alharbi, © 2011 27
  • 28. Create the blog’s database (I)  users table: CREATE TABLE `users` ( `id` int(11) unsigned NOT NULL auto_increment, `name` varchar(100), `email` varchar(150),`firstname` varchar (60),`lastname` varchar(60),PRIMARY KEY (`id`));  posts table: CREATE TABLE `posts` ( `id` int(11) unsigned NOT NULL auto_increment, `name` varchar(255), `date` datetime, `content` text, `user_id` int(11), PRIMARY KEY (`id`), FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE );  comments table: CREATE TABLE `comments` ( `id` int(11) unsigned NOT NULL auto_increment, `name` varchar(100), `content` text, `post_id` int(11), PRIMARY KEY (`id`), FOREIGN KEY(post_id) REFERENCES posts(id) ON DELETE CASCADE ON UPDATE CASCADE ); Khalid Alharbi, © 2011 28
  • 29. Create the blog’s database (II)  tags table: CREATE TABLE `tags` ( `id` int(11) unsigned NOT NULL auto_increment, `name` varchar(100), `longname` varchar(255), PRIMARY KEY (`id`) );  posts_tags table: CREATE TABLE `posts_tags` ( `id` int(11) unsigned NOT NULL auto_increment, `post_id` int(11) unsigned, `tag_id` int(11) unsigned, PRIMARY KEY (`id`), FOREIGN KEY(post_id) REFERENCES posts(id) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE ON UPDATE CASCADE ); The naming convention for foreign keys is: [singular table name]_id Khalid Alharbi, © 2011 29
  • 30. Models and Association   User Model: /app/models/user.php   Post Model: /app/models/post.php   Tag Model: /app/models/tag.php Khalid Alharbi, © 2011 30
  • 31. Controllers and Scaffolding  Scaffolding is a technique for testing table associations without needing to code any HTML.  To add scaffolding to the application, define the $scaffold variable in the controller.   Posts Controller: /app/controllers/posts_controller.php   Tags Controller: /app/controllers/posts_controller.php Khalid Alharbi, © 2011 31
  • 32. The Resulting Application (I)  Navigate to: http://localhost/blog/posts/add Khalid Alharbi, © 2011 32
  • 33. The Resulting Application (II)  Navigate to: http://localhost/blog/tags/add Khalid Alharbi, © 2011 33
  • 34. The Bake Script  The bake script is a shell script that automatically generates the basic elements of CakePHP.  It saves a lot of time generating the needed controllers, models, views, and unit tests.  It helps understand how MVC works in Cake.  It is located in cake/console/libs/bake.php  Configuring and using the bake script is illustrated in the following link: http://www.screencast.com/t/Lyr5li6btec Khalid Alharbi, © 2011 34
  • 35. Callback methods  Helps add special logic before or after actions are executed.  Cake supports callback methods for both controllers and models.   Controller callback methods:  beforeFilter(), beforeRender(), afterFilter(), etc.   Model callback methods:  beforeFind(), afterFind(), beforeValidate(), beforeSave(), beforeDelete(), etc. Khalid Alharbi, © 2011 35
  • 36. Layouts  Contain the overall design that wraps a view.  Layouts files must be placed in /app/views/layouts directory and named lowercase with .ctp extension.  The variable $content_for_layout is used to place the code for the views. Khalid Alharbi, © 2011 36
  • 37. Routes  Mechanism that maps URLs to controller actions.  Helps customize URLs and maintain the MVC structure.  Helps generate non-HTML files such as PDFs, RSS feeds, etc.  Cake comes configured with a default set of routes:   Ex: Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));  Routing configuration file is located in: /app/config/routes.php. Khalid Alharbi, © 2011 37
  • 38. Extending MVC Structure  Cake contains resources that extends the MVC structure while maintaining its lightweight organization.  Cake has the following extensions:   Helper: extends the View’s functionality.   Component: extends the Controller’s functionality.   Data Source: extends the Model’s functionality.   Behavior: extends the Model’s functionality. Khalid Alharbi, © 2011 38
  • 39. Helpers   Classes for the presentation layer to extend the functionality of the views.   Contain the presentation logic shared between views, elements, or layouts.   Simplify rendering HTML and processing forms.   CakePHP has a set of built in helpers:   HTML, Form, AJAX, Cache, JavaScript, RSS, XML, sessions, Time, Text, etc.   Ex: <?=$html->link('Add a Post','/posts/add');?>   Ability to create custom helpers. Khalid Alharbi, © 2011 39
  • 40. Components  Used to extend the functionality of the controllers.  Components are specific purpose shared actions that can be used across all controllers such as sending email, etc.  Components are stored in the /app/controllers/ components directory.  Cake comes with built-in components:   Auth, Session, Cookie, Email, Security, etc.  Ability to create custom controllers. Khalid Alharbi, © 2011 40
  • 41. Data Sources  Connect to extra data-sources and provide data to the models.  Models can reference functions in the DataSource without performing extra connections and data handling operations.  Cake comes with built-in DataSources that support only relational database systems.   Ability to create custom data sources and connect to REST APIs. Khalid Alharbi, © 2011 41
  • 42. Behaviors  Extend the functionality of models without writing a set of functions in the models.  Keep repetitive tasks in separate resource file and allow models to access it.  Example: when updating a record in the database, perform many updates across the database.  Cake comes with built-in behaviors: ACL, Containable, Translate, and Tree.  Ability to create custom behaviors. Khalid Alharbi, © 2011 42
  • 43. Utilities  General-purpose libraries that can be called from anywhere in the app through the App::import() function.  Cake comes with built-in utilities:   Configure: storing global variables.   File and Folder: managing file and folder operations.   HTTP Socket: managing requests to web services. Khalid Alharbi, © 2011 43
  • 44. Plugins  Mini-cake application inside other Cake app.  Plugins are useful as third-party resources.  Plugins contain models, views, and controllers just like any Cake application.  Plugins are droped into the app/plugins directory.  No built-in plugins in Cake only third-party plugins.  Ability to create custom plugins so they can be used by other applications. Khalid Alharbi, © 2011 44
  • 45. Cake Community (I)  Cookbook – http://book.cakephp.org   Official CakePHP documentation.  Bakery - http://bakery.cakephp.org   Official CakePHP community portal.  API - http://api.cakephp.org  All questions at CakePHP- http://ask.cakephp.org   Official questions and answers website.  CakePHP TV - http://tv.cakephp.org   Free video tutorials. Khalid Alharbi, © 2011 45
  • 46. Cake Community (II)  CakeFest - http://cakefest.org   The CakePHP conference.  CakeForge - http://cakeforge.org   Hosts open source CakePHP projects.  CakePackages - http://www.cakepackages.com   Free CakePHP code to download.  GitHub – https://github.com/cakephp/cakephp   Official CakePHP repository. Khalid Alharbi, © 2011 46
  • 47. References   Beginning CakePHP: From Novice to Professional By David Golding, ISBN: 1-4302-0977-1   CakePHP Application Development By Anupom Syam and Ahsanul Bari, ISBN: 9781847193896   CakePHP 1.3 Manual Book, http://book.cakephp.org   CakePHP after 3 years, looking back and moving ahead by Gwoo http://bakery.cakephp.org/articles/gwoo/2008/04/22/ after-3-years-looking-back-and-moving-ahead Khalid Alharbi, © 2011 47