Because CakePHP does not have a good Chinese manual, take notes during study. At the beginning, I should not give it all the reason. I will do it again, so I can see the effect, and I will learn it later. My environment is AppServ.
Because CakePHP does not have a good Chinese manual, take notes during study. At the beginning, I should not give it all the reason. I will do it again, so I can see the effect, and I will learn it later. My environment is AppServ, installed in C:/AppServ/, the root directory is C:/AppServ/www/, and cakephp is placed in
C:/AppServ/www/cakephp/
Precautions in advance:
Load rewrite module
Make sure that apache modules are loaded:
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
And strict directory rewriting is allowed.
Options FollowSymLinks
AllowOverride All
# Order deny, allow
# Deny from all
Enable the pdo_mysql module in php (remove the module in the php configuration file; or # remove it)
1. download cakephp2.3.0-beta.zip
Web: https://github.com/cakephp/cakephp/tags
2. decompress the package and change the name to cakephp. put it to the root directory of the website.
2.1 make sure that the app/tmp directory and sub-directories have the write permission. you can ignore this permission on windows servers.
3. configure the database connection information and security-related configuration values.
3.1 database configuration: copy app/config/database. php. default and name it database. php. it mainly configures the user name and password, database, and table prefix.
Public $ default = array (
'Datasource '=> 'Database/mysql ',
'Persistent' => false,
'Host' => 'localhost ',
'Login' => 'root', // mysql User name
'Password' => 'root', // mysql password
'Database' => 'test', // database name
'Prefix' => '', // table prefix
// 'Encoding' => 'utf8', // character set encoding default utf8
);
3.2 configure security parameters: find the following options in app/config/core. php and replace the values with random strings.
Configure: write ('security. salt', 'Random string after replacement ');
Configure: write ('security. herherseed', 'Random number after replacement ');
4. create a data table and insert test data
Create table posts (
Id int unsigned AUTO_INCREMENT primary key,
Title VARCHAR (50 ),
Body TEXT,
Created datetime default null,
Modified datetime default null );
Insert into posts (title, body, created) VALUES ('The title', 'This is The post body. ', NOW ());
Insert into posts (title, body, created) VALUES ('a title once again ',' And the post body follows. ', NOW ());
Insert into posts (title, body, created) VALUES ('title strikes Back', 'This is really exciting! Not. ', NOW ());
5. create a model and add automatic verification rules
Create Post. php under app/Model/(pay attention to naming conventions, capital of the first letter of the file), write the previous class, and add automatic verification rules
Class Post extends AppModel {
// Verification rules. The title and body fields cannot be blank.
Public $ validate = array (
'Title' => array (
'Rule' => 'notempty'
),
'Body' => array (
'Rule' => 'notempty'
)
);
}
6. create a controller and write down the method
Create PostsController under app/Controller. php (note the naming rules. The first letter of the name is in upper case and the complex form of the table is used: for example, peopleController. php, BooksController. php) now, if you want to access the view method, you can use http: // localhost/cakephp/posts/view
Class PostsController extends AppController {
Public $ helpers = array ('html', 'form ');
Public function index () {// query all records in the Post table and send the results to the view layer.
$ This-> set ('posts', $ this-> Post-> find ('all '));
}
Public function view ($ id) {// query a record based on the id
$ This-> Post-> id = $ id;
$ This-> set ('post', $ this-> post-> read ());
}
Public function add () {// add method
If ($ this-> request-> is ('post ')){
$ This-> Post-> create ();
If ($ this-> Post-> save ($ this-> request-> data )){
$ This-> Session-> setFlash ('Your post has been saved .');
$ This-> redirect (array ('action' => 'index '));
} Else {
$ This-> Session-> setFlash ('unable to add your post .');
}
}
}
Public function edit ($ id = null) {// edit method
$ This-> Post-> id = $ id;
If ($ this-> request-> is ('get ')){
$ This-> request-> data = $ this-> Post-> read ();
} Else {
If ($ this-> Post-> save ($ this-> request-> data )){
$ This-> Session-> setFlash ('Your post has been updated .');
$ This-> redirect (array ('action' => 'index '));
} Else {
$ This-> Session-> setFlash ('unable to update your post .');
}
}
}
Public function delete ($ id) {// delete method
If ($ this-> request-> is ('get ')){
Throw new MethodNotAllowedException ();
}
If ($ this-> Post-> delete ($ id )){
$ This-> Session-> setFlash ('post with id: '. $ id. 'has been deleted .');
$ This-> redirect (array ('action' => 'index '));
}
}
}
7. create a view layer
Create a directory Posts/in app/View/, and then create index. ctp under the Directory (the default template suffix is ctp. Modify the template suffix in app/AppController. add a member attribute public $ ext = '.html '; to the class AppController extends Controller {} class in the PHP file.) (to modify other configuration information, refer to the core file: lib/Cake/Controller. php. all attributes can be customized. you can modify the attributes in the core file or in app/AppController. php modification, which is created in the app for easy framework upgrade)
Blog posts
Html-> link ('add post', array ('action' => 'Add');?>
Id |
Title |
Actions |
Created |
|
Html-> link ($ post ['post'] ['title'], array ('action' => 'view ', $ post ['post'] ['id']);?> |
Form-> postLink ( 'Delete ', Array ('action' => 'Delete', $ post ['post'] ['id']), Array ('confirm' => 'Are you sure? ')); ?> Html-> link ('edit', array ('action' => 'edit', $ post ['post'] ['id']);?> |
|
Continue to create view. ctp for browsing a single record
Created:
Continue to create the edit record Template edit. ctp
Edit Post
Echo $ this-> Form-> create ('post', array ('action' => 'Edit '));
Echo $ this-> Form-> input ('title ');
Echo $ this-> Form-> input ('body', array ('rows '=> '3 '));
Echo $ this-> Form-> input ('id', array ('type' => 'den den '));
Echo $ this-> Form-> end ('save Post ');
Add. ctp
Add Post
Echo $ this-> Form-> create ('post ');
Echo $ this-> Form-> input ('title ');
Echo $ this-> Form-> input ('body', array ('rows '=> '3 '));
Echo $ this-> Form-> end ('save Post ');
?>
8. configure routing rules and use the index method of the posts controller as the access address of the root directory.
In app/Config/routes. php, find
Router: connect ('/', array ('controller' => 'pages', 'Action' => 'display', 'home '));
Changed to: Router: connect ('/', array ('controller' => 'posts', 'Action' => 'index '));
9. open the address http: // localhost/cakephp/to access the home page, and add, delete, modify, and query the post table.
On the first day, I learned how to add, delete, modify, and query cakephp and how to modify the template suffix. I have a general understanding that cakephp works in MVC mode. The following figure shows the cakephp request.