Vous êtes sur la page 1sur 3

28/06/13

dotCloud - CakePHP Tutorial

Documentation

0.9

We removed our free Sandbox April 25th . You can read more on our blog.

CakePHP
Note CLI Command examples on this page are always provided without the a p p l i c a t i o n (shorthand A ) argument, assuming youre running these commands in a connected folder (at creation or using the d o t c l o u dc o n n e c tcommand). For more details on connected folders, see Migrating to the CLI 0.9. In this tutorial, we will create a simple CakePHP application that displays a form for new posts, saves the posts to a MySQL database, and lists all of the saved posts. Create a Model Create a Controller Create a View Send requests to the front web controller Create a dotcloud.yml file Enable automatic database configuration Deploy to dotCloud

Create a Model
First, you will need to download the latest version of CakePHP and untar (or unzip) it to a local directory on your computer. Once you have done that, you can add a Post model. To do this, create a file in app/models called post.php. It will need to contain this code:
< ? p h p c l a s sP o s te x t e n d sA p p M o d e l { v a r$ n a m e=' P o s t ' ; v a r$ v a l i d a t e=a r r a y ( ' b o d y '= >a r r a y ( ' r u l e '= >' n o t E m p t y ' ) ) ; } ? >

Create a Controller
Next, create a file called posts_controller.php in app/controllers which contains this code:
< ? p h p c l a s sP o s t s C o n t r o l l e re x t e n d sA p p C o n t r o l l e r{ v a r$ n a m e=' P o s t s ' ; v a r$ c o m p o n e n t s=a r r a y ( ' S e s s i o n ' ) ; f u n c t i o ni n d e x ( ){ $ t h i s > s e t ( ' p o s t s ' ,$ t h i s > P o s t > f i n d ( ' a l l ' ) ) ; } f u n c t i o nv i e w ( $ i d ){ $ t h i s > P o s t > i d=$ i d ; $ t h i s > s e t ( ' p o s t ' ,$ t h i s > P o s t > r e a d ( ) ) ; } f u n c t i o na d d ( ){ i f( ! e m p t y ( $ t h i s > d a t a ) ){ i f( $ t h i s > P o s t > s a v e ( $ t h i s > d a t a ) ){ $ t h i s > S e s s i o n > s e t F l a s h ( ' Y o u rp o s th a sb e e ns a v e d . ' ) ; $ t h i s > r e d i r e c t ( a r r a y ( ' a c t i o n '= >' i n d e x ' ) ) ;

docs.dotcloud.com/tutorials/php/cakephp/#send-requests-to-the-front-web-controller

1/3

28/06/13
} } } } ? >

dotCloud - CakePHP Tutorial

Create a View
Finally, create a directory in app/views called posts. Inside of that directory, create a file called index.ctp:
< ! -F i l e :/ a p p / v i e w s / p o s t s / i n d e x . c t p> < h 1 > A d dP o s t < / h 1 > < ? p h p e c h o$ t h i s > F o r m > c r e a t e ( ' P o s t ' ,a r r a y ( ' a c t i o n '= >' a d d ' ) ) ; e c h o$ t h i s > F o r m > i n p u t ( ' b o d y ' ,a r r a y ( ' r o w s '= >' 3 ' ) ) ; e c h o$ t h i s > F o r m > e n d ( ' S a v eP o s t ' ) ; ? > < h 1 > P o s t s < / h 1 > < t a b l e > < t r > < t h > I d < / t h > < t h > T e x t < / t h > < / t r > < ! -H e r ei sw h e r ew el o o pt h r o u g ho u r$ p o s t sa r r a y ,p r i n t i n go u tp o s ti n f o> < ? p h pf o r e a c h( $ p o s t sa s$ p o s t ) :? > < t r > < t d > < ? p h pe c h o$ p o s t [ ' P o s t ' ] [ ' i d ' ] ;? > < / t d > < t d > < ? p h pe c h o$ p o s t [ ' P o s t ' ] [ ' b o d y ' ] ;? > < / t d > < / t r > < ? p h pe n d f o r e a c h ;? > < / t a b l e >

Send requests to the front web controller


To send requests to the front web controller, youll need to create a file in app/webroot called nginx.conf. Inside of this file, add this line:
t r y _ f i l e s$ u r i$ u r i // i n d e x . p h p ;

This will make sure that all requests for dynamic content will be directed to the front web controller, which will route them appropriately.

Create a dotcloud.yml file


To deploy to dotCloud, you need to create a file in the root directory of your cakephp app (the directory under app) called dotcloud.yml which describes the structure of your application. For this application, the dotcloud.yml will look like this:
w w w : t y p e :p h p a p p r o o t :a p p / w e b r o o t m y s q l : t y p e :m y s q l

Enable automatic database configuration


In app/config, youll find a file called database.php.default. Copy or rename this file to database.php, then add a constructor function inside the DATABASE_CONFIG class which reads the dotCloud environment.json file and updates the default database settings. The end result will look something like this:
< ? p h p c l a s sD A T A B A S E _ C O N F I G{ v a r$ d e f a u l t=a r r a y ( ' d r i v e r '= >' m y s q l ' , ' p e r s i s t e n t '= >f a l s e , ' h o s t '= >' ' , ' p o r t '= >' ' ,

docs.dotcloud.com/tutorials/php/cakephp/#send-requests-to-the-front-web-controller

2/3

28/06/13
' l o g i n '= >' r o o t ' , ' p a s s w o r d '= >' ' , ' d a t a b a s e '= >' c a k e p h p ' , ' p r e f i x '= >' ' , ) ; v a r$ t e s t=a r r a y ( ' d r i v e r '= >' m y s q l ' , ' p e r s i s t e n t '= >f a l s e , ' h o s t '= >' l o c a l h o s t ' , ' l o g i n '= >' u s e r ' , ' p a s s w o r d '= >' p a s s w o r d ' , ' d a t a b a s e '= >' t e s t _ d a t a b a s e _ n a m e ' , ' p r e f i x '= >' ' , ) ;

dotCloud - CakePHP Tutorial

f u n c t i o n_ _ c o n s t r u c t ( ){ $ j s o n=f i l e _ g e t _ c o n t e n t s ( " / h o m e / d o t c l o u d / e n v i r o n m e n t . j s o n " ) ; $ e n v=j s o n _ d e c o d e ( $ j s o n ,t r u e ) ; $ t h i s > d e f a u l t [ ' h o s t ' ]=$ e n v [ ' D O T C L O U D _ M Y S Q L _ M Y S Q L _ H O S T ' ] ; $ t h i s > d e f a u l t [ ' p o r t ' ]=$ e n v [ ' D O T C L O U D _ M Y S Q L _ M Y S Q L _ P O R T ' ] ; $ t h i s > d e f a u l t [ ' p a s s w o r d ' ]=$ e n v [ ' D O T C L O U D _ M Y S Q L _ M Y S Q L _ P A S S W O R D ' ] ; } } ? >

Deploy to dotCloud
Now youre ready to deploy! Just run the following (if youre using git or hg, youll need to commit your changes first):
d o t c l o u dc r e a t em y _ a p p d o t c l o u dp u s h

At the end of the push, youll see the URL for your newly deployed app. Simply open that url in your browser (add /posts at the end) to see your app.

2012 DotCloud View the RST source of this page Search:

docs.dotcloud.com/tutorials/php/cakephp/#send-requests-to-the-front-web-controller

3/3

Vous aimerez peut-être aussi