Detailed description of common CakePHP development skills

Source: Internet
Author: User
In development, the program is generally set to debug 2. how to disable debug in a specific action?

Q: During development, the program is generally set to debug 2. how can I disable debug in a specific action?
A: Configure: write ('debug', 0 );
Q: How can I statically render a view of an action?
A: The process is to first obtain the rendered content and then generate a static file. here we can see how to obtain the rendered content. There are two methods:
1. you can use the ob function.
2. in the controller, $ this-> requestAction ('/controller/action/param', array ('Return '));
Q: How can I change the relationship between models and generate efficient SQL statements?
A: During the process of CakePHP development, make sure that DEBUG is set to 2. always pay attention to whether the SQL statements generated by CakePHP are normal. Recursive and unbindModel are usually used here. Note that if $ this-> model-> read () is used, you can use $ this-> model-> recursive = N; to adjust the number of links. if $ this-> model-> find ()/$ this-> model-> findAll () is used, their last parameter is to control the cascade number.
Find ($ conditions,
$ Fields,
$ Order,
$ Recursive );
String $ conditions;
Array $ fields;
String $ order;
Int $ recursive;
FindAll ($ conditions,
$ Fields,
$ Order,
$ Limit,
$ Page,
$ Recursive );
String $ conditions;
Array $ fields;
String $ order;
Int $ limit;
Int $ page;
Int $ recursive;
Q: Can the session of CakePHP programs and other PHP programs be unified?
A: This is because CakePHP modifies the Cookie name by default, so it cannot correspond to other PHP, as long as it modifies the core. the corresponding option of the PHP file can be: define ('Cake _ SESSION_COOKIE ', ini_get ('session. name '); PHPSESSID by default.
Q: In the CakePHP program, what are the URL formats?
A: There are roughly three forms.
First, with the help of mod_rewrite, its URL is roughly like http: // xxx/controller/action/param. you only need to enable the rewrite module of Apache.
The second is in PATH_INFO format. its URL is roughly like http: // xxx/index. php/controller/action/param. if you want to use this form, you only need to modify the core. the corresponding line in php can be: define ('base _ url', env ('script _ name '));
The third form is the traditional URL form. its URL is roughly like http: // xxx/index. php? Url =/controller/action/param. As for why, you just need to check the method of the. htaccess file in CakePHP.
Q: What if the CakePHP program only displays some static pages?
A: The default/pages controller is used. There are several examples by default.
Q: How do I name common actions in a uniform manner?
A: common actions are named in a uniform manner, which helps with standardized processing. common actions refer to common CRUD operations, such:
Class ExampleModel extends AppController
{
Var $ name = 'example ';
Function index ()
{
}
Function add ()
{
}
Function edit ($ id)
{
}
Function view ($ id)
{
}
Function remove ($ id)
{
}
}
Q: How do I set and verify the input tag in CakePHP1.2?
A: In CakePHP1.2, we do not recommend using html assistants anymore. Instead, we recommend using the new form helper to set the input tag, as shown in" Text ('Model. field', array ('size' => 50);?>", Verification error information is like" Error ('Model. field', 'error msg ')?>"
Q: How can I conveniently display the information to be edited when editing data?
A: In CakePHP, as long as you use the form assistant to generate a form, your own value is automatically extracted from the controller's $ this-> data.
Function edit ($ id = null)
{
If (! Empty ($ this-> data ))
{
If ($ this-> Model-> create ($ this-> data) & $ this-> Model-> save ())
{
$ This-> flash ('MSG ','/controller/action/param ');
Exit ();
}
}
$ This-> Post-> id = intval ($ id );
$ This-> data = $ this-> Post-> read ();
}
Q: How can I reuse an action?
A: In the past, an article in bakery introduced $ this-> params ['requested'] to determine whether to return and then use requestAction for reuse, the actual determination method has a bug. when you use a call with the return parameter such as $ this-> requestAction ('/controller/action', array ('Return, an error occurs. A better judgment method is as follows:
Function view ($ id = null)
{
$ This-> Model-> id = intval ($ id );
$ This-> Model-> read ();
If (! Empty ($ this-> params ['Return '])
{
Return $ this-> Model-> data;
}
$ This-> set ('model', $ this-> model-> data );
}
Q: How do I implement most actions that use the Auth component to control permissions?
A: Because most actions require permission control using the Auth component, it is best to put it in AppController. the code is as follows:
Class AppController extends Controller
{
Var $ components = array ('auth ');
In this way, the permissions are automatically determined during component initialization.
For a few exceptions, you only need to set allow in the corresponding sub-controller. for example, we want to set the index of the Threads controller and the view action can be freely accessed, do not use Auth to control permissions ):
Class ThreadsController extends AppController
{
Var $ name = 'threads ';
Function beforeFilter ()
{
$ This-> Auth-> allow ('index', 'view ');
}
Q: How can I implement theme functions and provide multiple style interfaces?
A: CakePHP1.2 already has the theme function. we only need to define var $ view = 'theme 'in app_controller.php and load Theme. php file, and then locate the specific topic directory based on our settings (var $ theme = 'themename';). If you do not define the directory, the default topic directory name is themed, then we create a new directory (such as css and js) and it will be OK.
Q: How do I set and access the named GET variable?
A: Generally, in CakePHP, The GET variable is not named, in the form of/users/view/123. then, the parameter is automatically included in the method parameter, however, in some cases, we still need to use the named GET variable. in this case, the general form is/users/view/name: value, then, we can access the named GET variable through the $ this-> namedArgs array in the controller, such as $ this-> namedArgs ['name'].
Q: How do I set the data that can be accessed in the view?
A: simply put, we only need to perform $ this-> set ('name', 'value'); and other operations in the controller, but in fact, if you just want to set the variable in the controller $ this-> data, the set code can be completely omitted, because the controller's $ this-> data variable will be copied to the $ this-> data variable in the view, you do not need to set such a variable at all, you only need to directly echo $ this-> data ['model'] [...] In the view file. you can. Of course, sometimes we have to set some variables. at this time, we still have a skill to make the code simpler:
$ Var1 = 'text ';
$ Var2 = 'text ';
$ Var3 = 'text ';

$ This-> set (compact ('var1', 'var2', 'var3 '));
Haha, with the compact function, we can use a set statement to complete operations that seem to require multiple entries.
Q: How to implement the responsibility chain in the controller?
A: There is a beforeFilter attribute in the controller. if we want to perform several operations (such as aaa, bbb, ccc) before an action is executed ), then we only need to declare var $ beforeFilter in the controller class:

Copy the content to the clipboard code:
Class AppController extends Controller
{
Var $ beforeFilter = array ('AAA', 'BBB ', 'CCC ');
Function aaa ()
{
//...
}
Function bbb ()
{
//...
}
Function ccc ()
{
//...
}}

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.