Cake

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.

http://fr.wikipedia.org/wiki/CakePHP

notes perso (intranet)

Official website

Blogs

API

Changelog

Version 1.2

Todo / wish list v1.3

Install notes

cakephp-instaweb

intéressant aussi, permet d'installer un serveur cake rapidement:

http://bakery.cakephp.org/articles/view/the-cakephp-instaweb-webserver

sudo apt-get install cakephp-instaweb

cakephp-instaweb

http://localhost:3000/

KO

sudo apt-get install cakephp cakephp-instaweb cakephp-scripts php5 python-twisted-bin python-twisted-core python-twisted-web python-zopeinterface bake

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

OK

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

voir tutoriel_de_base_blog

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

ancien

cakephp_russe

Doc

The mainframe

CMS

Ajax

Autocomplete / Autofill

Authentification / protection

Méthodes du composant Auth

Méthodes

INSERT INTO `users` (`id`, `login`, `password`, `disabled`, `created`) VALUES (1, 'admin', SHA1('6a10cdde80fb56150efda09365f91579ea74a944admin'), 0, NOW());

Creating an Admin Section with CakePHP

Books / Livres

CakePHP Application Development

Bon bouquin pour démarrer rapidement

Anupom Syam, Ahsanul Bari

Notes de lecture (intranet)

Beginning CakePHP: From Novice to Professional

Un bon livre: Beginning CakePHP: From Novice to Professional GOLDING David 2008

Notes de lecture (intranet)

Google API's and cake

Tutorials

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

TinyMCE

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

copie locale

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

HABTM (hasAndBelongsToMany)

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

FIXME 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 générer du code en bash avec cake:

sudo apt-get install cakephp1.3-scripts

puis voir doc sous http://book.cakephp.org/fr/view/113/code-generation-with-bake

http://www.formation-cakephp.com/40/console-ligne-de-commande

cake bake db_config
cake bake model
cake bake view 
cake bake controller
cake bake project
cake bake test
cake bake all
cake bake controller Users

http://www.webdevelopment2.com/cakephp-bake-baking-models-controllers-views-cakephp-12/

si ça coince: http://grasshopperpebbles.com/ubuntu/cakephp-baking-on-ubuntu/

echo $PATH

echo 'export PATH=/home/les/public_html/cake/cake/console:$PATH' >> ~/.profile

. .profile

cake

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

All

With the “all” parameter it is now possible to bake with one command a model plus the corresponding controller with its views:

cake bake all  // asks you for the model name
cake bake all User

Editeurs

Il semble qu'eclipse soit le plus populaire, mais j'utilise pas mal aussi emacs 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

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

echo "<select id=\"CmTypeId\" name=\"data[Cm][type_id]\">";	
/* Type */
$i=0;
while($i<mysql_num_rows($sqlq)){
	echo "<option value=\"" .mysql_result($sqlq,$i,'id')."\"";
		if($form->value('type_id')==mysql_result($sqlq,$i,'id')) {
			echo " selected";
		}
	echo ">" .mysql_result($sqlq,$i,'lib');
	echo "</option>";
	$i++;
	}			
echo "</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'];

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 et Joomla

Il est possible d'intégrer cake et joomla, plus d'infos: cake_joomla

cake et drupal

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

Caddie

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 ;-)

Script d'aide pour changer la table

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

 
info/cake.txt · Dernière modification: 2011/12/21 15:03 par radeff
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki