Une introduction
Par Padraig Galvin
Présentation du framework
●

Architecture Model View Controller (MVC)

●

Programmation orientée objet (OOP)

●

Crée en 2005 (pour PHP 4.3)

●

Inspirée de Ruby on Rails
–

Convention over configuration

–

DRY : don't repeat yourself

●

Licence MIT (open source)

●

Projet actif avec des mise a jour fréquente

●

PHP version 5.2.8 +
Caractéristiques
●

●

●

●

Validation et assainissement des données simple
Gestion des associations entre les données
automatique
Système de templates avec des layouts, éléments
réutilisables et helpers
Composants et utilitaires intégrés pour gérer la cache,
les emails, cookies …

●

Sessions, authentification et contrôle d'accès

●

Internationalisation (i18n) et localisation (i10n)

●

Échafaudage et génération de code
Structure du répertoire
/app
/Config
/Console
/Controller
/Lib
/Locale
/Model
/Plugin
/Test
/Vendor
/View
/tmp
/webroot
/lib
/Cake
/plugins
/vendors

← Votre application
← Configuration : routage, base de donné, sessions …
← Scripts CLI/Shell (Cron jobs).

← Traductions Gettext (fichiers PO).

← Unit tests (PHPUnit).

← Fichiers temporaire : cache, logs …
← Répertoire publique : CSS, JS, images …

← Cœur de CakePHP
Les conventions
Route
Controller

Action (method)

View

Model

Database table

/articles/add
GET / POST

/articles

/articles/edit/36

Class: ArticlesController
File: app/Controller/ArticlesController.php
index()

add()

edit($id)

app/View/Articles/
index.ctp

app/View/Articles/
add.ctp

app/View/Articles/
edit.ctp

Class: Article
File: app/Model/Article.php
articles
Un simple exemple
Exemple d'application :

Le schéma
clients
id
name
email
created
modified

tags
id
name

tickets
id
client_id
description
created
modified

tags_tickets
id
tag_id
ticket_id
Exemple d'application :

Les modèles
/app/Model/Client.php

/app/Model/Tag.php

/app/Model/Ticket.php
Exemple d'application :

Les relations
/app/Model/Client.php

/app/Model/Tag.php

/app/Model/Ticket.php
Exemple d'application :

Validation de donnée
/app/Model/Client.php

Règles de validation disponibles
alphaNumeric
between
blank
boolean
cc
comparison
custom
date
datetime
decimal
email
equalTo
extension
fileSize
inList
ip
luhn

maxLength
mimeType
minLength
money
multiple
naturalNumber
notEmpty
numeric
phone
postal
range
ssn
time
uploadError
url
userDefined
uuid
Exemple d'application :

Liste des clients (index)
Un petit test
/app/Controller/ClientsController.php

/app/View/Client/index.ctp

example.com/clients

/app/View/Client/index.ctp
find(string $type, array $params)

first
all
count
list
threaded
neighbors

conditions
recursive
fields
order
limit
...
Exemple d'application :

Afficher un client (view)
/app/Controller/ClientsController.php

Un petit test
/app/View/Client/view.ctp

example.com/clients/view/3
/app/View/Client/view.ctp
Exemple d'application :

Ajouter un client (add)
/app/Controller/ClientsController.php

Les données
/app/Controller/ClientController.php

/app/View/Client/add.ctp

POST : example.com/clients/add
Exemple d'application :

Ajouter un client (add)
/app/View/Client/add.ctp - vues alternatifs
Minimal :

Précis :

HTML généré :

HTML généré :
Exemple d'application :

modifier un client (edit)
/app/Controller/ClientsController.php

/app/View/Client/edit.ctp
Exemple d'application :

supprimer un client (delete)
/app/Controller/ClientsController.php

Lien pour supprimer
Astuces et conseils
●

●

●
●

●

Commencez par les tutoriels de la
documentation officielle
Prenez le temps de réfléchir à votre schéma de
base de données
Utilisez bake avec des templates personnalisé
Ne pas réinventez la roue, surtout pour la
sécurité
Respectez les conventions !
Version 3.0
●

PHP version 5.4 +

●

Installation avec Composer (http://getcomposer.org/)

●

Namespaces

●

Modèle amélioré avec données en forme d'objet

●

Configuration consolidée

●

Et beaucoup plus …
Ressources
●

Sites et téléchargements :
–
–

●

http://cakephp.org
http://www.cakephp-fr.org/

Le « Cookbook » (documentation) :
–

●

Support :
–
–
–

●

http://book.cakephp.org/2.0/fr/
https://groups.google.com/forum/#!topic/cakephp/
http://stackoverflow.com/tags/cakephp
irc://irc.freenode.net/cakephp

Code source :
–

https://github.com/cakephp/cakephp

Cakephp