CakePHP
CakePHP est un framework libre écrit en PHP. Il suit le motif de conception Modèle-Vue-Contrôleur et imite le fonctionnement de Ruby on Rails.
Official website
cake3
La denière version de cakePhp, cakephp3
Blogs
http://cherryonthe.popnews.com/ (thanks rodn!)
API
ubuntu
source: http://bakery.cakephp.org/articles/view/installing-cakephp-on-ubuntu
1.Ensure mod-rewrite is enabled by running the following command:
sudo a2enmod rewrite
If mod-rewrite is not enabled, follow the on-screen instructions to install it.
2.Next, edit the site config file for which you want to enable mod-rewrite. This is likely to be /etc/apache2/sites-available/default. Edit the file as root using your text editor of choice (try sudo Nano default)
3.Add the following entry:
AllowOverride All
http://cakephp.org/downloads/index/nightly
unzip it to webserver, eg /home/radeff/www/cake
setup permissions:
chmod 777 /var/www/cake/app/tmp/cache/ #create a symlink in your $HOMEDIR ln -s /var/www/cake ~ cd ~/cake chmod 777 app/tmp/
si vous avez le message:
Notice (1024): Please change the value of 'Security.salt' in aa01/config/core.php to a salt value specific to your application [CORE\cake\libs\debugger.php, line 535]
The security salt is used for generating hashes, particularly for password security. The Cake build has a default salt which should be changed otherwise there is the potential that another site with the same build and thus the same default salt could be well on the way to crack your passwords. Note that the first Cake bake pass should have modified this salt, but you can manually change it. Sometimes I manually generate a project salt based on the project name and build version so that I can check it during subsequent processing. Change one or more characters of the salt or generate yourself a completely new salt. Use something like echo sha1(’my own security salt string’) and copy the 40 character hashed string. The security.salt is stored in \htdocs\aa01\config\core.php at around line 153.
vi /home/radeff/cake/app/config/core.php /Security.salt :x
et chez moi c'était OK
pour que les css etc s'affichent correctement il faut autoriser le mod_rewrite d'apache et redémarrer le serveur web:
#lien symbolique /etc/apache2/mods-enabled# ln -s ../mods-available/rewrite.load sudo ln -s /etc/apache2/mods-available/userdir.load /etc/apache2/mods-enabled/userdir.load sudo ln -s /etc/apache2/mods-available/userdir.conf /etc/apache2/mods-enabled/userdir.conf #j'ai du ajouter dans mon apache.conf <Directory "/var/www/cake/"> AllowOverride All </Directory> #restart server /etc/init.d/apache2 restart
Pour bosser ensuite on peut utiliser tout éditeur, mais le mieux semble eclipse
.htaccess
Chez infomaniak, c'est simple: dans la racine du cake, placer un htaccess (dans l'exemple protégé par un mdp, vous pouvez supprimer tout ce qui est avant ifmodule si pas nécessaire):
AuthType Basic AuthName Restricted Require user cocagne AuthUserFile /home/www/YOURINFOMANIAKPATH/.htpasswd AuthGroupFile /home/www/YOURINFOMANIAKPATH/.htgroup <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ app/webroot/ [L] RewriteRule (.*) app/webroot/$1 [L] </IfModule>
http://www.infomaniak.ch/guide/french/htaccess/ plus d'infos sur htaccess chez infomaniak
problème .htaccess
si pour une raison ou une autre votre htaccess ne marche pas, Config/core.php et décommenter:
Configure::write('App.baseUrl', env('SCRIPT_NAME'));
attention: vous aurez des “moches urls” avec un “index.php” au lieu des urls SEF
Doc
The mainframe
http://book.cakephp.org/ The official Manual
http://book.cakephp.org/fr/ en français siouplé
http://bakery.cakephp.org/ cf aussi http://bakery.cakephp.org/tags/view/cake
http://www.formation-cakephp.com/
http://cakeforge.org/projects/cakedocs/
http://groups.google.com/group/cake-php
http://www-128.ibm.com/developerworks/edu/os-dw-os-php-cake1.html
http://www-128.ibm.com/developerworks/edu/os-dw-os-php-wiki1.html
CMS
Des CMS écrits en cake se développent rapidement, quelques liens:
http://planetcakephp.org/aggregator/items/1054-simple-cakephp-cms-content-management-system
http://planetcakephp.org/aggregator/items/3543-5-cakephp-based-content-managament-system
http://cupcakecms.sourceforge.net/index.html
episode un russe pour faire bonne mesure
Tutifrutti commercial
Ajax
http://marcgrabanski.com/article/cakephp-ajax-quick-save-jquery
http://bakery.cakephp.org/articles/view/easy-ajax-pagination-using-jquery
Autocomplete / Autofill
http://blogfreakz.com/cakephp/cakephp-jquery-autocomplete-tutorial/
Autocomplete en Ajax (autofill)
http://set4net.org/blog/2010/11/cakephp-%E2%80%93-jquery-autocomplete-tutorial/
Others http://www.nodstrum.com/2007/09/19/autocompleter/
http://www.jensbits.com/2010/03/29/jquery-ui-autocomplete-widget-with-php-and-mysql/
Authentification / protection
http://book.cakephp.org/view/327/simple-user-authentication commencer par là
AutoLogin
dans
src/Controller/UsersController.php
créer une méthode en lui donnant un nom compliqué qu'une attaque par force brute ne permet pas de décoder, p. ex. Tmvxsmz9tvHKpJTsN9NRjhTR
//autologin function Tmvxsmz9tvHKpJTsN9NRjhTR(){ $authUser = $this->Users->get(6)->toArray(); // Log user in using Auth $this->Auth->setUser($authUser); return $this->redirect([ 'controller' => 'Recettes', 'action' => 'add' ]); }
Appeler ensuite par un url caché, ex. https://votredomaine/sitecake/users/Tmvxsmz9tvHKpJTsN9NRjhTR
notes: “6” est la valeur de admin; si on veut logger différents utilisateurs, il faut remplacer par la valeur de la personne à logger et ajouter une méthode pour pouvoir exposer l'url sans compromettre la sécurité, p. ex. avec un clé de hash (on logge la personne sur un premier système, on concatène p. ex. son username avec une hashkey et on passe le tout à l'url précité dans une 2e variable, genre
ou 4CNz9LmkTzhnrHc4JXVtwbsn = p. ex.
avant d'envoyer l'url on le prépare:
$secretkey="pVV3Mz9bqHMWTTmCPcT3znf" $crypt=sha1($secretkey.$user)
et dans cake on vérifie
//d'abord, on vérifie l'IP de la requête $adresse_ip = $_SERVER['REMOTE_ADDR']; if($adresse_ip!=IPADRESSTRUSTEDSERVER){ echo "Your IP adress " .$_SERVER['REMOTE_ADDR'] . " is not allowed to access that url, we'll inform your provider of your hacking activities!"; exit; } //et maintenant on contrôle la clé $user=$_GET['user']; $crypt=$_GET['crypt']; $secretkey="pVV3Mz9bqHMWTTmCPcT3znf"; $cryptcheck=sha1($secretkey.$user); if($crypt==$cryptcheck) { //ok, the keys are matching, do sthing... } else { echo "There is a problem, the system was unable to log you, please inform your webmaster"; exit; }
Méthodes du composant Auth
INSERT INTO `users` (`id`, `login`, `password`, `disabled`, `created`) VALUES (1, 'admin', SHA1('6a10cdde80fb56150efda09365f91579ea74a944admin'), 0, NOW());
Creating an Admin Section with CakePHP
http://www.jamesfairhurst.co.uk/posts/view/creating_an_admin_section_with_cakephp
http://bakery.cakephp.org/articles/search/2/authentication
http://bakery.cakephp.org/articles/view/obauth-simple-authentication
Books / Livres
CakePHP Application Development
Beginning CakePHP: From Novice to Professional
Un bon livre: Beginning CakePHP: From Novice to Professional GOLDING David 2008
Google API's and cake
http://forum.jeanviet.info/discussion/66/google-analytics-avec-lapi-php-cakephp/
http://code.google.com/intl/fr/apis/ajaxlanguage/
http://code.google.com/intl/fr/apis/ajaxlanguage/documentation/
http://thechaw.com/api_generator ???
http://code.google.com/p/php-language-api/
http://snipplr.com/view/15538/google-ajax-libraries-api-handler-for-cakephp/
cake php project (bouquin)
Tutorials
http://www.sitepoint.com/article/application-development-cakephp
http://www.journaldunet.com/developpeur/tutoriel/php//070403-php-cakephp-framework/0.shtml
http://www.jamesfairhurst.co.uk/posts/view/full_cakephp_application_part_6 plusieurs tutoriaux intéressants, notamment sur les login
Basic HTML
<?php echo $html->link( $html->image('/img/loupe.jpg', array("alt" => "Détail", "title"=>"Détail")), array('action'=>'view', $saisy['Saisy']['id']), array('escape' => false)); ?>
Scriptaculous, Ajax, Drag&Drop
http://www.reversefolds.com/articles/show/ajax
Getting started quickly with Scriptaculous effects
voir aussi sur ce wiki scriptaculous
bof http://mhjewel.wordpress.com/2008/01/26/my-first-project-with-cakephp-and-scriptaculous-framework/
FCKEditor
Générer un flux rss
Tutoriel de base (blog)
il faut suivre le tutoriel pour créer un blog, qui demandera les commandes suivantes:
~/cake/app/config cp database.php.default database.php vi database.php #faire les modifs #créer le premier controleur ~/cake/app/models gedit post.php #copier/coller/sauver/sortir cd ../controllers/ gedit posts_controller.php #copier/coller/sauver/sortir ~/cake/app/views mkdir posts gedit index.ctp #copier/coller/sauver/sortir http://localhost/cake/posts
faut changer les droits dans le cache:
chmod -R 777 /var/www/cake/app/tmp/cache
Dans “Validation rule”, on parle du fichier models/post.php
pas s'inquiéter si la suite marche pas, faut voir l'ancienne version (en date du — Fred Radeff 2008/07/10 12:58)
Tutoriel pour chercher un string et renvoyer les valeurs
Tutoriel bookmarks avec scaffolding
Pour utiliser les fonctions scaffold de cakephp, qui permettent de construire une application en 5 minutes chrono à partir d'une base de données, suivre cet exemple: Copie locale de http://www.grahambird.co.uk/cake/tutorials/scaffolding.php
Ensuite, pour montrer la puissance de cakephp, faire une modification dans la base de données et recharger l'exemple, vous verrez alors toute l'application s'adapter à la nouvelle structure de la table
ALTER TABLE `bookmarks` ADD `category` VARCHAR( 255 ) NOT NULL AFTER `url` ;
Join
voir aussi si on veut faire des JOIN tables
http://www.developer.com/lang/php/article.php/3636686
CREATE TABLE players ( id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(50) NOT NULL, position VARCHAR(25) NOT NULL, team_id SMALLINT(6) ) CREATE TABLE teams ( id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, )
Breadcrumbs
Date / hours
Chercher / Search
faire un moteur de recherche avec cakephp
http://manual.cakephp.org/view/313/Controllers
Livesearch (Ajax style search) essayé mais planté
http://www.littlehart.net/atthekeyboard/2007/08/07/stupid-cakephp-controller-tricks/ pas trouvé mais trucs intéressants
http://www.scribd.com/doc/5545/CakePHP-tutorial-no-2-from-IBM tutoriel IBM pas trouvé (mais pas trop cherché)
http://code.google.com/p/searchable-behaviour-for-cakephp/
http://bakery.cakephp.org/articles/view/search-feature-to-cakephp-blog-example marche mais plante à la fin
HABTM (hasAndBelongsToMany)
http://stackoverflow.com/questions/327052/cakephp-find-based-on-habtm-relationship un exemple ou un modèle appelle plusieurs tables relationnelles, à creuser pour ergolang (categories communes à tous les vocabulaires)
voir aussi
http://book.cakephp.org/view/73/Retrieving-Your-Data
http://teknoid.wordpress.com/2008/07/03/notes-on-cakephp-habtm-part-1-the-basics/
Sauvegarde des champs spéciaux dans la table de relation
Exemple: recettes, ingredients, ingredients_recettes
Si vous avez des champs supplémentaires dans ingredients_recettes, p .ex.
identifiant | identifiant ingrédient | identifiant recette | quantité de l'ingrédient* |
---|---|---|---|
id | ingredient_id | recette_id | quantite_ingredient |
Lorsque vous éditez et sauvegardez une recette /cakephp/recettes/edit/…
La valeur de quantite_ingredient est PERDUE!
Le plus simple est de supprimer le lien dynamique dans le controller edit, en virant ou commentant la ligne
function edit($id = null) { ... /* * tip: do not include any ingredients, so that the quantities are preserved in the link table ingredients_recettes */ //$ingredients = $this->Recette->Ingredient->find('list');
Autres solutions (merci NiKo)
- détruire la relation HABTM à la volée et recréer un relation hasMany juste avant de sauver ton modèle, vieil exemple http://nuts-and-bolts-of-cakephp.com/2008/09/24/saving-extra-fields-in-the-join-table-for-habtm-models/, à voir s'il fonctionne encore
- plugin Alaxos, regarder dans le Behavior UserLinker, pour sauver les champs created_by et modified_by dans les tables de jointures.
- en Cake 2, regarde la propriété 'unique' ⇒ 'keepExisting' des descriptions HABTM, ça permet de ne pas effacer les relations existantes lors de la sauvegarde, ce qui peut être utile pour ne pas perdre les infos
Validation
Cake propose toute une série de validations, y compris en ligne de commande (bake)
http://book.cakephp.org/view/134/Core-Validation-Rules
Un truc qui m'a pris la tête: avoir plus d'une validation pour un champ
En fait c'est assez simple, cf http://jamnite.blogspot.com/2009/05/cakephp-form-validation-with-ajax-using.html
<?php class Post extends AppModel { var $name = 'Post'; var $validate = array( 'title' => array( 'required' => array('rule' => 'notEmpty'), 'maxlength' => array('rule' => array('maxLength', 30)) ), 'body' => array( 'required' => array('rule' => 'notEmpty'), 'maxlength' => array('rule' => array('maxLength', 200)) ) ); } ?>
ici les champs title et body sont à la fois requis ET ont une longueur max; comme d'habitude, faut faire des array dans des array… pas encore trop l'habitude!
non, marche pas
mieux vaut utiliser cette solution:
'paper_title' => array( 'This field cannot be left blank' => array('rule' => 'notempty'), 'max. 250 characters' => array('rule' => array('maxLength', 250)), ),
antispam
email Validation
attention la validation par défaut marche PAS!!!
http://planetcakephp.org/aggregator/items/3150-cakephp-%E2%80%93-email-validation
After working for about one year, the BHJS Alumni website phase 2 is completed. But after it is deployed in production server, i found that some email addresses are regarded as invalid email address. The email validation rule which i used is provided by CakePHP. Only email addresses ended with .com, .org etc… could pass the validation rule. So i ask the Google teacher and luckily i am not the only one to come across this problem. Email Validation in cakephp What i can do is writing my own validation rule which can be doned by regular expression in the model.php. Again, Google is really helpful and there are many regular expressions for email validation. Finally, i pick the following one. ^[A-Za-z0-9._%+-]+@([A-Za-z0-9-]+\.)+([A-Za-z0-9]{2,4}|museum)$ Reference: TIP: Simple Regular Expression for Email Validation And so in the model.php, i replaced the email validation as follow. var $validate = array( 'id' => array('numeric'), /* CakePHP has problem on email validate. Reference: http://groups.google.com.tw/group/cake-php/browse_thread/thread/3d0a47d4... 'email_address' => array('notempty', 'email' => array('rule' => array('email', true),'message' => 'Please supply a valid email address.')), */ // The following change is done for the above problem. Reference: http://gofedora.com/tip-simple-regular-expressi-email-validation/ 'email_address' => array('notempty', 'email' => array('rule' => '/^[A-Za-z0-9._%+-]+@([A-Za-z0-9-]+\.)+([A-Za-z0-9]{2,4}|museum)$/','message' => 'Please supply a valid email address.')) );
Bake
pour faire votre cake en un coup de baguette magique ligne de commande
bake all
./bin/cake bake all TABLENAME
Pour faire ça sur une autre base que la base principale, utiliser -c ou –connection, ex.
~/cake3 ./bin/cake bake all produits_sur_commande_livraisons --connection=erp
Baking Controllers
You can go through all the prompts, but in my opinion this takes too much time for me. Here are some straight to the point commands to get you on your way:
cake bake controller Users
This bakes an empty controller using scaffolding. If you don’t know, Scaffolding is sort of like baking, except with no functions and no views. This is usually used to test of database schema, model association, model validation, etc.
cake bake controller Users scaffold
This does the same thing as the command above, except it actually creates the functions: index, add, view, edit, and delete.
cake bake controller Users scaffold admin
Again, this does the same thing as the top, but with admin_index, admin_add, admin_view, admin_edit and admin_delete functions. Starting to get the picture?
Baking Views
This couldn’t be simpler:
cake bake view Users
Creates views for all the functions created in the Users controller.
To generate only the admin functions you have to provide “null” instead of “scaffold”:
cake bake controller Users null admin
Editeurs
Il semble qu'eclipse soit le plus populaire, mais j'utilise pas mal aussi start pour les expressions régulières et quanta:
Utiliser Quanta avec cake: http://forum.cakephp-fr.org/viewtopic.php?id=22 (voir aussi autres liens, eg. http://groups.google.com/group/cake-php/browse_thread/thread/efc0a080cceb37f5/700861ec9b4151b8
Tips
Limiter le nombre de caractères dans un champ (add/edit)
utiliser l'attribut maxlength dans l'array:
echo $this->Form->input('Titre', array("class"=>"titreedit", "label"=>"", 'maxlength'=>'80'));
Templates
CakePHP est pauvre en templates, mais potentiellement très riche puisqu'il s'adapte à des moteurs de templates comme joomla
http://jeremydorn.blogspot.com/2008/10/site-templates-with-cakephp.html
en gros: on télécharge un template p. ex. sur http://www.oswd.org/designs/browse/
on adapte default.ctp avec le bon css et les éléments cake, p.ex.
<!-- Main Site Content --> <?php $session->flash(); echo $content_for_layout; ?>
Traduction
kw: Langue / Language / i18n / traduction / translation
outre i18n, on peut aussi faire plus moche mais aussi efficace avec du replace - p. ex. si le i18n tourne pas sur votre serveur
Trim
dans le modèle:
class Exercice extends AppModel { var $name = 'Exercice'; ... /*trim posted values*/ function beforeSave() { $this->data['Exercice']['titreexercice']=trim($this->data['Exercice']['titreexercice']); $this->data['Exercice']['soustitre']=trim($this->data['Exercice']['soustitre']); $this->data['Exercice']['liengrammaire']=trim($this->data['Exercice']['liengrammaire']); $this->data['Exercice']['debutcontenu']=trim($this->data['Exercice']['debutcontenu']); $this->data['Exercice']['fincontenu']=trim($this->data['Exercice']['fincontenu']); $this->data['Exercice']['source']=trim($this->data['Exercice']['source']); return true; }
Select
Sessions
pour avoir toutes les sessions:
print("<pre>"); print_r($_SESSION); print("</pre>");
pour récupérer le contenu d'une variable session (style php): MARCHE PARTOUT
$auth_role=$_SESSION['Auth']['User']['group_id'];
Referer
Uploads
Utiliser meioupload, installé en 5'
clear cache
rm -fR tmp/cache/persistent/* rm -fR tmp/cache/models/* rm -fR tmp/cache/views/*
pour rediriger après un update / insert
function add() { if (!empty($this->data)) { $this->Remarque->create(); if ($this->Remarque->save($this->data)) { #$this->flash(__('Remarque saved.', true), array('action'=>'index')); $this->redirect(array('action'=>'index'), null, true); } else { } } } function edit($id = null) { if (!$id && empty($this->data)) { $this->flash(__('Invalid Remarque', true), array('action'=>'index')); } if (!empty($this->data)) { if ($this->Remarque->save($this->data)) { #$this->flash(__('The Remarque has been saved.', true), array('action'=>'index')); $this->redirect(array('action'=>'index'), null, true); } else { } } if (empty($this->data)) { $this->data = $this->Remarque->read(null, $id); } }
rsync_du_pauvre_avec_ftp / wget
cake multiplateformes
J'ai eu beau essayer chez infomaniak, j'y suis toujours pas parvenu, par contre sur des serveurs ou je peux travailler en root pas de problème: il suffit de faire une installation d'un cake, puis de placer des app/ ailleurs et d'y modifier webroot/index.php
ROOT /user/home APP_DIR www CAKE_CORE_INCLUDE_PATH user/home
if (!defined('ROOT')) { define('ROOT', DS.'user'.DS.'home'); } if (!defined('APP_DIR')) { define('APP_DIR', 'www'); } if (!defined('CAKE_CORE_INCLUDE_PATH')) { define('CAKE_CORE_INCLUDE_PATH', DS.'user'.DS.'home'); }
Critiques
- bonne critique anti-cake
http://ajbrown.org/blog/2008/12/22/four-reasons-to-hate-cakephp/
Caddie
caddie - basket - panier - ecommerce
http://cakeforge.org/projects/bakesale/
Versions
composant versionning cake http://bakery.cakephp.org/articles/view/revision-behavior-revision-control-made-easy
top et simple: permet de gérer les versions en faisant simplement une copie de la table et UNE ligne dans le model
Pagination / Tri
http://book.cakephp.org/fr/view/165/Controller-Setup
le plus simple, ajouter au début du controller local:
class DonneesController extends AppController { #criteres de tri var $paginate = array( 'limit' => 25, 'order' => array( 'Donnee.NOM' => 'asc' ) );
Mais méchant défaut: il ne conserve pas les GET / POST lors de motifs de recherche, à proscrire donc! Suivre la méthode décrite ici copie locale | fichier tar modifié par mes soins - version française