Home > Article > Backend Development > cakephp example simple news system
Database settings I am using the database news here
apache: rewrite module open
I set up a virtual host here to point to www/app. If it is a virtual directory, check the relevant documents.
Create the fields id, title, content, time, sort in the new news table
Because the news table must be operated, first create the news model
/app/models/news.php
class news extends AppModel{
var $name='news';
}
?>
If there is no var $name='news'; this line, the model will default to the table to be processed is newss (personal guess, haven't tried it)
This attribute is used to identify the database used var $useTable='users'; $name is just the name that identifies the model
The attribute $table is probably used when dealing with the relationship between models, right? Save this doubt
and then create a new /app/controllers/news_controller.php
class newsController extends AppController{
var $uses=array('news'); //Use 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 successful','/news /');
$this-> /news/add/'.$id);
using using using ’ through using using ’’s out through out out out together's out way out's together out out out together out out outmb out out out out out out out out out out out out out out out out out out out out out Out Out out I I -, ?
function delete($id){
>flash('Deletion successful','/news/');
else
$this->flash('Deletion failed','/news/');
exit();
} //Delete
function view($id){
$this->news->id=$id;
$this->set('news',$this->news->find());
}
}
?>
This way you can 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 accessing, you will be prompted to miss view add.thtml view. thtml delete.thtml index.thtml
Because the view name output by cake will be your current action name by default. If you want it to output as you wish, use $this->render('test'); which is test.thtml
These thml files are all under the /app/views/controller/ directory. The current controller is news
so the files are placed under the /app/views/news/ directory
add.thtml:
formTag( 'new/title'.$id);?>
input('new/title');?>
textarea('new/ content');?>
submit('submit');?>
news:
< ;?
for($i=0;$i
title:=$lists[$i]['news']['title'] ?>
}
title:=$news['news']['title']?>
content:= $news['news']['content']?>
The above is the content of the cakephp example simple news system. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!