Cake: Let PHP run on the rails, too.

Source: Internet
Author: User
Tags array insert php foreach php framework ruby on rails

Cake is a PHP framework that is structured according to Ruby on Rails. Like Ror, Cake also encapsulates operations on the database. At present, cake is not yet a mature framework, but it is worth paying attention to.

Below describes how to install cake on the wamp.

First download Cake Latest version: cake_0.2.9.zip


After decompression, enter Cakeconfig will database.php.default renamed to Database.php, and set the parameters of the database. Such as:

$DATABASE _config = Array (
' Devel ' => Array (
' Host ' => ' localhost ',
' Login ' => ' user ',
' Password ' => ' user ',
' Database ' => ' cake '
)
);

Then cake need to use the Apache mod_rewrite, open Apache's/config/httpd.conf, will

#LoadModule Rewrite_module modules/mod_rewrite.so

Before the # number is removed,

Will

#AddModule mod_rewrite.c

Before the # number removed.

Then add a virtual host, such as

<virtualhost *>
ServerAdmin Easy@gmail.com
DocumentRoot "f:/cake/"
ServerName cake.com
ErrorLog Logs/cake.com.error_log
Customlog logs/cake.my.com Common
</VirtualHost>

<directory "f:/cake/" >
AllowOverride All
Order Allow,deny
Allow from all
</Directory>

Join a row of local host in c:windowssystem32driversetchosts

127.0.0.1 cake.com

Then restart Apache and the browser.

At this time cake has been able to work properly. Let's create an application:

Create a table in the database

CREATE TABLE posts (    ID INT UNSIGNED auto_increment PRIMARY KEY,    title VARCHAR (), Body    TEXT,    created DA Tetime default NULL,    modified DATETIME default null); INSERT into posts (title,body,created)     VALUES (' title ', ' This is the post body. ', Now (): INSERT into posts (title,body,created)     VALUES (' A title once again ', ' and the ' post Bo DY follows. ', now ()]; INSERT into posts (title,body,created)     VALUES ("title Strikes Back", ' This is really exciting! Not. ', now ());
Cake is based on the MVC pattern. When creating an application, we first create its model.
app/models/post.php;? Phpclass Post extends Appmodel {}?>
And then create control
app/controllers/posts_controller.php;? Phpclass Postscontroller extends AppController {}?>
Add the index method to:
app/controllers/posts_controller.php (fragment) function index () {}
Last CREATE View
app/views/posts/index.thtml<table><tr>    <th>ID</th>    <th>title </th>    <th>Created</th></tr>? PHP foreach ($this->post->find_all () as $post):?><tr>    <td><?= $post [' ID ']?> </td>    <td><?= $this->link_for ($post [' title '], "/posts/view/{$post [' ID ']}"?> </td>    <td><?= $post [' Created ']?> </td></tr>] PHP Endforeach?></table>
Such an application is complete.
Enter Http://cake.com/posts/index to access the program we just created.
Idtitlecreated1the title2005-05-23 09:30:342a title once again2005-05-23 09:30:353title strikes back2005-05-23 09:30:35


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.