Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Gentle introduction to CakePHP

Gentle introduction to CakePHP

Set of slides intended to help World Skills students and trainers better understand CakePHP as a framework.

KahWee Teng

March 10, 2014
Tweet

More Decks by KahWee Teng

Other Decks in Programming

Transcript

  1. Who am I? • KahWee • [email protected] @kahwee • Four

    time Grammy Awards watcher, 
 knows 8 Korean words, 
 can’t play guitar • Day job is 40% JavaScript/Node.js, 10% HTML, 20% CSS
  2. What is a framework? • A structured or guided way

    of doing things. • Development can be several times faster if you use conventions. • Frameworks constrains you to a set of best practices, e.g.: • Escapes MySQL statements for you • Decides where you put your files • Speeds you up by offering scaffolding* capabilities * Terms & conditions applies
  3. Why CakePHP • A mature MVC (Model-View-Controller) framework • Good

    documentation • Strong online support • Cake shell tasks
  4. Getting started • Download CakePHP from cakephp.org • Put the

    extracted CakePHP inside C:\xampp\htdocs\cakephp • Go to http://localhost/cakephp to see the app • Setup PHP in your console • Get Windows env var PATH to point to C:\xampp\php
  5. Modifying core.php • Search for “time” without quotes • Uncomment

    the following: • //date_default_timezone_set(‘UTC’);   • Change the values for: • Configure::write('Security.salt',   ‘DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi');   • Configure::write('Security.cipherSeed',   '76859309657453542496749683645');
  6. Introducing Console/cake • Command line tool to speed you up

    • Commands available • Console/cake bake: Generate/scaffold • Console/cake console: Start PHP web server without XAMPP
  7. cd  app Everything you should need is in the app

    directory, let’s navigate into it.
  8. Console/cake  bake This will guide you in creating the CakePHP

    database.php file. Make sure you have the database already created and database server properly started.
  9. Model • The Model layer represents the part of your

    application that implements the business logic. • It is responsible for retrieving data and converting it into meaningful concepts for your application. • This includes processing, validating, associating or other tasks related to handling data. • In CakePHP, models are always singular
  10. View • The View renders a presentation of modeled data.

    • For example, as the Modal layer returns a set of data, the view would use it to render a HTML page containing it, or an XML or JSON response. • In CakePHP, views are always plural
  11. Controller • The Controller layer handles requests from users. It’s

    responsible for rendering back a response with the aid of both the Model and the View Layer. • They are, like, a manager. • In CakePHP, controllers are always plural
  12. Turning off debug • Go to config/core.php • Change Configure::write('debug',

     2);   • … Into Configure::write('debug',  0);
  13. Special columns based on conventions • `created` and `modified` is

    automatically populated • `name` is automatically used as a Model’s $displayField • `id` is automatically used as a Model’s $primaryKey • `dessert_id` is the foreign key
  14. Short note on security • If you use Model::find() and

    Model::save(), it is save from SQL injection if you put your variables into parameters. Example: • $this-­‐>Movie-­‐>save({
    ‘name’  =>  ‘Titanic’,
    ‘year’  =>  ‘1997’
 })
  15. Routing in CakePHP • ./desserts corresponds to DessertsController::index() • ./desserts/add

    corresponds to DessertsController::add() • ./desserts/search corresponds to DessertsController::search()
  16. Returning a JSON through a controller   public  function  search()

     {       //  Please  escape  this  before  passing  into  MySQL
     //  Remember  to  remove  %  also.       $search_string  =  $this-­‐>request-­‐>query["s"];       $this-­‐>viewClass  =  'Json';       $movies  =  $this-­‐>Movie-­‐>find('all',  [
       'conditions'  =>  ['name  LIKE'  =>  “%$search_string%”]
     ]);       $this-­‐>set('movies',  $movies);
     $this-­‐>set('_serialize',  ['movies']);     }
  17. Cons of CakePHP • Slower performance: Performance is the key

    concern of CakePHP users. CakePHP 2.4 has gotten better but is not as fast as other frameworks such as Yii Framework • Too magical: CakePHP has a lot of magic underneath and not understanding it may result in the developer spending more time than neccessary
  18. Where to get more information? • Free PDF book for

    CakePHP: http://book.cakephp.org • Code examples and tutorials: http://bakery.cakephp.org • CakePHP documentation: http://api.cakephp.org • Stackoverflow: http://stackoverflow.com