This articleArticleIt can be said that it is the most classic in the CakePHP tutorial. Although it is not a complete hands-on series, I have summarized 21 articles about my use of CakePHP, which are especially useful to new users.
During translation, some special words in CakePHP are intentionally reserved without translation, such as controller and model. I believe that people who have learned CakePHP should be able to understand what they mean immediately.
In addition, the Wiki of CakePHP has expired and is replaced by a website named bakery. The Wiki links referenced in the original text have also been updated to bakery.
Quick creation of static pages
I want to create several pages that only contain static data. The default layout is used and no model is required. Initially I tried to create a controller and define an action for each static page. However, this method is clumsy and is not suitable for creating static pages quickly.
In fact, you only need to use pages controller to create a view under the views/pages folder and access it through/pages. For example, I created/views/pages/Matt. thtml to access it through a http://www.example.com/pages/matt.
Change the static page title
If you want to change the page title when using pages controller, you only need to add the following to view:Code:
- <? $ This-> pagetitle ='Title of your page.';?>
Send data to layout on the Static Page
To pass data to layout (for example, to indicate which part of the navigation bar should be highlighted), you can add the following code to view:
- <? $ This->_ Viewvars['Somedata'] = Array('Some', 'data');?>
This array can be accessed through $ somedata in layout.
Quickly create backend Management
If you need to create background managementProgramAnd want all management actions to be located in a specific folder, open config/CORE. php and remove the comments of the following line:
- Define ('cake _ admin','Admin ');
In this way, all actions starting with "admin _" can be accessed through/admin/yourcontroller/youraction. For example, if an action named "admin_add" is created in posts controller, you can access this action through www.example.com/admin/posts/add. In this way, you can easily set a password for the Admin directory to avoid arbitrary access by others.
View the SQL statements executed in the background
You only need to change the DEBUG constant in config/CORE. php to view the SQL statements executed in the background. 0 indicates the product level, 1 indicates the development level, 2 indicates the complete SQL debugging, and 3 indicates the complete SQL debugging and displays the object data. I usually set debug to 2 so that a table containing SQL debugging information is displayed at the bottom of each page.
If the table added at the bottom of the page breaks the page layout (especially when Ajax is used to obtain the page and display it in the middle of the page rather than the bottom), you can add the following code in CSS to hide debugging information:
- # Cakesqllog {display: none ;}
In this way, you can maintain the page layout and viewSource codeTo see the debugging information. Of course, do not forget to change the debugging level back to 0 when releasing the website.
Obtain rich development documents
Don't always stare at the manual. Wiki and API are also invaluable. The Development Guide in wiki is very useful, and the API documentation looks difficult at the beginning, but you will soon find that the information here is very important for you to create a CakePHP website. '
Use bake. php
Bake is a command line PHP script that can automatically generate model, Controller, and view based on the database. At the initial stage of development, I strongly recommend using scaffolding to run your prototype program. But if you know clearly that scaffolding is not suitable, I recommend you use bake. Bake generates all the files and saves them to the disk so that you can modify them at will. This saves the effort of creating associations, views, and basic CRUD crollder operations.
(Note: four basic operations for crud-create, read, update, delete, and database applications, namely "add, query, modify, and delete ".)
Bake is very convenient. You only need to create a table in the database and execute PHP bake. php In the/cake/scr ī pts/directory.
If you run bake in interactive mode, it prompts you to create a model, Controller, and view in several steps. After the creation, I usually read all generated code and make necessary modifications.
Pay attention to permissions when releasing a program
Once, when I published a program, I packed the entire cake directory and uploaded it to the server with SCP. Once debugging information is disabled, an error occurs. Database calls cannot return any data. I have nothing to worry about, because I have to pass the debugging information to debug the problem. Someone told me later that/APP/tmp should be writable to Apache. After the permission is changed to 777, the problem is solved.
Complex Model Verification
I need to perform more complex verification, instead of simply verifying that the input box is not empty or conforms to a regular expression. For example, I want to verify that the email address used during user registration is in use. On the Wiki, I found this article on advanced verification, which mentions some useful advanced verification methods.
Record error logs
- $ This->Log ('Something broke ');
In this way, the error is recorded in/tmp/logs/(I initially thought it would be recorded in Apache's error log ).
Allow the Controller to use other models
If your controller needs to call data from different models, use the following code at the beginning of controller:
- ClassYourcontroller extends appcontroller{
- VaR $ uses = array ('Post', 'user ');
- }
In this way, the controller can access post and user model.
Create a model without using a database table
I need to create a model that does not use any table. For example, I want to use the $ validate array to verify the input data and ensure that the model logic is correct. However, when the table corresponding to the model is created does not exist, CakePHP reports an error. You can solve this problem by adding the following code to the model:
- VaR $ usetable = false;
You can also change the table name corresponding to the model in this way.
- VaR$ Usetable = 'some _ table ';
Remember to exit () after redirection ()
For experienced people, this should be taken for granted. After calling $ this-> redirect (), the remaining code should exit () If you do not want to run it (). I also did this, but previously thought that $ this-> redirect () would call exit for me (not actually ).
Advanced Model Functions
By turning over the API, you can find a lot of useful functions that you don't know. I strongly recommend that you read the reference manual for the model class at least once. The following are some important functions I have not noticed before:
* Generatelist ()-mainly used to generate a selection box