Database settings I'm using Database news here.
Apache:rewrite Module Open
I am here to set up a virtual host to point to Www/app, and if it is a virtual directory, view the relevant documentation.
The field inside the new news table Id,title,content,time,sort
Because you must operate the news table, first build the news model
/app/models/news.php
<?
Class News extends appmodel{
var $name = ' News ';
}
?>
If there is no var $name = ' News '; this line of the model defaults to the table to be processed is NEWSS (personal guess, not tried)
Is this attribute to identify the database used by var $useTable = ' users '; $name just identifies the model name
The attribute $table estimate is used when dealing with model relationships? Save this puzzle
Then create a new/app/controllers/news_controller.php
<?
Class Newscontroller extends appcontroller{
var $uses =array (' News '); Using the News model
function index () {
$this->set (' lists ', $this->news->findall ())
}//Default
function Add ($id =0) {
if ($id) {
$this->set (' id ', $id);
$this->news->id= $id;
}
if (!empty ($this->data)) {
$ret = $this->news->save ($this->data[' News ');
if ($ret) {
if ($id)
$this->flash (' Update succeeded ', '/news/');
Else
$this->flash (' Add success ', '/news/');
}else{
if ($id)
$this->flash (' update failed ', '/news/add/'. $id);
Else
$this->flash (' Add failed ', '/news/add ');
}
}
Exit ();
}//Add/Modify
function Delete ($id) {
$this->news->id= $id;
$ret = $this->news->remove ();
if ($ret)
$this->flash (' delete succeeded ', '/news/');
Else
$this->flash (' delete failed ', '/news/');
Exit ();
}//Delete
function View ($id) {
$this->news->id= $id;
$this->set (' News ', $this->news->find ());
}
}
?>
This makes it possible to access
http://127.0.0.1/app/news/(same as Http://127.0.0.1/app/news/index)
Http://127.0.0.1/app/news/add
Http://127.0.0.1/app/news/view
Http://127.0.0.1/app/news/delete
But when you visit, you'll be prompted Miss View add.thtml view.thtml delete.thtml index.thtml
Because cake will default to the output view name of your current action name if you want it to output using $this->render (' test ') as you wish; That's test.thtml.
These thml files are in the/app/views/controller/directory, and the current controller is news.
So the files are placed in the/app/views/news/directory.
Add.thtml:
<?echo $html->formtag (' New/title '. $id);? >
<?echo $html->input (' new/title ');? >
<?echo $html->textarea (' new/content ');? >
<?echo $html->submit (' submit ');? >
</form>
Index.html:
News
<br>
<?
for ($i =0; $i <count ($lists); $i + +) {
?>
title:<?= $lists [$i] [' News '] [' title ']?>
<?
}
?>
View.thtml:
title:<?= $news [' News '] [' title ']?>
content:<?= $news [' News '] [content ']?>
The above is cakephp instance simple news system content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!