Cakephp instance simple news system

Source: Internet
Author: User
Here I set up a VM pointing to www/app. if it is a virtual directory, view the relevant documentation. Create the id, title, content, time, and sort fields in the news table.

Basic configuration:
Database settings I use database news here
Open apache: rewrite module
Here I set up a VM pointing to www/app. if it is a virtual directory, view the relevant documentation.

Create the id, title, content, time, and sort fields in the news table.

Because you must operate the news table, create the news model first.
/App/models/news. php

Class news extends AppModel {
Var $ name = 'news ';
}
?>

If there is no var $ name = 'new'; by default, the table to be processed is newss (I guess I have not tried it)
This attribute is used to identify the database var $ useTable = 'users'; $ name only indicates the name of the model.
Attribute $ table is used to process the relationship between models? Save this question
Create/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 ('updated successfully', '/news /');
Else
$ This-> flash ('added successfully', '/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 ('deleted successfully', '/news /');
Else
$ This-> flash ('deletion failed', '/news /');

Exit ();
} // Delete
Function view ($ id ){
$ This-> news-> id = $ id;
$ This-> set ('news', $ this-> news-> find ());
}
}
?>
In 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

However, the system will prompt you to miss view add. thtml view. thtml delete. thtml index. thtml
Because cake outputs the view name by default. your current action name. if you want it to output $ this-> render ('test'); that is, test. thtml
All these thml files are in the/app/views/controller/Directory. the current controller is news.
Therefore, files are stored in the/app/views/news/directory.
Add. thtml:
FormTag ('New/title'. $ id);?>
Input ('New/title');?>
Textarea ('New/content');?>
Submit ('submit ');?>

Index.html:

News:


For ($ I = 0; $ I ?>
Title:
}

?>

View. thtml:

Title:
Content:

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.