CakePHP Simple Example

Source: Internet
Author: User
Tags learn php php foreach

Learn PHP today, see many people on the Internet recommended cakephp, download to try it. Here are the notes I recorded, the entry level. If my understanding and description of the wrong place, please help me to point out, thank you!

1. Download and install cakephp

    • CakePHP Official website: http://cakephp.org/
    • or http://www.microsoft.com/web/webmatrix/download WebMatrix and install cakephp here.

2. Create database phptest, data table users

3, connect the database, app/config/database.php

Public$default=array (' datasource ' = ' database/mysql ',//database type

' Persistent ' =>false, ' host ' = ' localhost ',//hostname

' Login ' = ' root ',//user name

' Password ' = ' root ',//password

' Database ' = ' phptest ',//DB name ' prefix ' = ' + '/' encoding ' + ' UTF8 ',);

4, create model,app/model/user.php. Note that PHP has strict naming conventions, and the data tables are in the plural form, such as Users,model, with the singular form of the table name as user, CakePHP automatically associates the Users data table, user model, Userscontroller controller, and users/***. The CTP view.

<?php

Class User extends appmodel{

var $name = ' User ';//specifying the name of the model in the $name is a recognized best practice.

var $useTable = ' users ';//This sentence is not, the default is to connect the Users data table

}

?>

5. Create the Controller controller,app/controller/userscontroller.php

<?php

Class Userscontroller extends appcontroller{

var $name = ' Users ';

Function Show () {

$this->set (' Users ', $this->user->find (' all ')); Pass the value of users to the view through the Set function

}

}

?>

6. Create a View VIEW,APP/VIEW/USERS/SHOW.CTP

<! DOCTYPE html>

<meta charset= "Utf-8"/>

<title></title>

<body>

<table>

<tr>

<th>id</th><th>username</th><th>password</th>

</tr>

<?php foreach ($Users as $User):?>

<tr>

<td><?php echo $User [' User '] [' id '];?></td>

<td><?php echo $this->html->link ($User [' User '] [' username '], '/users/view/'. $User [' user '] [' id ']); ></td>

<td><?php echo $User [' User '] [' password '];?></td>

</tr>

<?php Endforeach;? >

</table>

</body>

Displays the users list with the table element, the foreach ($Users as $User) loop the Users object, $User [' model name '] [' property '] to get the properties of the object. $this->html->link (display content, URL) Link, cake1.3 version before the use of $html->link, the new version uses $this->html->link.

7. Run test, browser url:http://localhost:16418/users/show, Access Controller/userscontroller Show method, jump to VIEW/SHOW.CTP view

8, Perfect function, click the username link of the list to jump to the new view display user information. Query by ID.

A, add the view method in Userscontroller.php, the method name is the same as the URL in link.

Function view ($id =null) {

$this->user->id= $id;

$this->set (' User ', $this->user->read ());

}

B. Create VIEW.CTP in view

<body>

<?php echo "ID:". $User [' User '] [' ID ']. <br> ";?>

<?php echo "Username:". $User [' User '] [' Username ']. " <br> ";?>

<?php echo "Password:". $User [' User '] [' Password ']. " <br> ";?>

</body>

After clicking the link, the results are displayed as

9. New

Userscontroller.php Adding the Add method

function Add () {

if (!empty ($this->data)) {

if ($this->user->save ($this->data)) {

$this->redirect (Array (' action ' = ' show '));

}else{

$this->redirect (Array (' action ' = ' show '));

}

}

}

New View ADD.CTP

<form action= "/users/add" method= "POST" >

<label>username:</label><input name= "username" size= "/>"

<label>password: </label><input type= "password" name= "password" size= "+"/>

<input type= "Submit" value= "Submission"/>

</form>

Model user.php Adding validation rules

Set up validation rules for new user

var $validate =array (

"Username" =>array ("rule" = "notempty"),

"Password" =>array ("rule" = "notempty")

);

Add unsuccessful if the form content does not conform to the validation rule

does not conform to the "username" =>array ("rule" = "notempty") This validation rule, so failed.

10. Delete

userscontroller.php Adding a Delete method

function Delete ($id =null) {

$this->user->delete ($id);

$this->session->setflash ("Success");

$this->redirect (Array ("action" = "show"));

}

Modify View SHOW.CTP

<td><?php Echo $this->html->link ("delete", Array (' action ' = ' delete ', $User [' User '] [' ID ']), NULL, ' Are you sure you want to delete it? ')?></td>

Link ("Delete", Array (' Action ': ' delete ', parameter), NULL, prompt statement)

11. Modification

Userscontroller.php Modify Method View method

Function view ($id =null) {

$this->user->id= $id;

if (Empty ($this->data)) {//If the submitted data is empty then display

$this->set (' User ', $this->user->read ());

}

else{//if the commit data is not empty then modify

if ($this->user->save ($this->data)) {

$this->session->setflash ("Success");

$this->redirect (Array (' action ' = ' show '));

}

}

}

Modify View VIEW.CTP

<form action= "/users/view/<?php echo $User [' User '] [' id ']?>" method= "POST" >

<label>username:</label>

<input name= "username" size= "" value= "<?php echo $User [' User '] [' username ']?> '/>

<label>password:</label>

<input type= "password" name= "password" size= "" value= "<?php echo $User [' User '] [' password ']?> '/>

<input type= "Submit" value= "Submission"/>

</form>

CakePHP Simple Example

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.