CakePHP study summary 6

Source: Internet
Author: User

CakePHP study summary 6

1. Change the debug information in the lower part of the cake during running.
Set row 43rd of APP \ config \ core. php
* Development Mode:
* 1: errors and warnings shown, model caches refreshed, flash messages halted.
* 2: As in 1, but also with full debug messages and SQL output.
* 3: As in 2, but also with full controller dump.
*
* In production mode, flash messages redirect after a time interval.
* In development mode, you need to click the Flash message to continue.
*/
Configure: Write ('debug', 2 );
Change to configure: Write ('debug', 0 );

2. Change http: // localhost: 8082/myphp5/quickwall/home
Http: // localhost: 8082/myphp5/quickwall,
Change app \ config \ route. php
Router: connect ('/', array ('controller' => 'questions', 'Action' => 'home '));

3 For example, writing a user-defined validator
In usermodel
Function checkunique ($ data, $ fieldname ){
$ Valid = false;
If (isset ($ fieldname) & $ this-> hasfield ($ fieldname )){
$ Valid = $ this-> isunique (Array ($ fieldname => $ data ));
}
Return $ valid;
}

'Unique' => array (
'Rule' => array ('checkunique', 'username '),
'Message' => 'user name taken. Use another'
)

4 function beforefilter (){
$ This-> auth-> allow ('signup ');
}
The filter is executed before each action, as in Java. Allow allows actions to be executed without auth authentication.

 

5
Write an app_controller.php file in the app directory. Other classes inherit it.
Function beforefilter (){
$ This-> auth-> loginredirect = array ('controller'
=> 'Questions', 'Action' => 'home ');

$ This-> auth-> logoutredirect = array ('controller'
=> 'Questions', 'Action' => 'home ');

$ This-> auth-> allow ('signup', 'Confirm', 'home', 'show ');
$ This-> auth-> authorize = 'controller ';
$ This-> auth-> userscope = array ('user. Confirmed '=> '1 ');
$ This-> set ('loggein', $ this-> auth-> User ('id '));
}

Function isauthorized (){
Return true;
}

Here, $ this-> auth-> loginredirect, $ this-> auth-> logoutredirect indicates that the login is successful and the exit path is reached.
Userscope indicates that logon is allowed only when a condition is set. Otherwise, logon is not allowed.
$ This-> set ('loggein', $ this-> auth-> User ('id'); Put the user ID after logon to loggedin for ease of use at the view layer.
For example, in the template

<? PHP if ($ loggedin):?>
.....
<? PHP else?>
.....
<? PHP end if;?>

Isauthorized () is required. You can write some functions that have been successfully verified. If not, return true.

Login and logout in user_controll can be written in this way.
Function login (){
}

Function logout (){
$ This-> session-> setflash ('logout ');
$ This-> redirect ($ this-> auth-> logout ());
}

6. Use cookies
<? PHP
Class appcontroller extends controller {
VaR $ components = array ('auth ', 'cooker ');
...
In appcontrol, Set
$ This-> auth-> autoredirect = false;
$ This-> cookie-> name = 'quickwall ';

If (! $ This-> auth-> User ('id ')){
$ Cookie = $ this-> cookie-> Read ('user ');
If ($ cookie ){
$ This-> auth-> login ($ cookie );
}
}

 

$ This-> auth-> autoredirect defaults to true. Here, false indicates that after successful login,
To go to login, it is
Function login (){
If ($ this-> auth-> User ()){
If (! Empty ($ this-> data )){
If (empty ($ this-> data ['user'] ['Remember _ me']) {
$ This-> cookie-> Del ('user ');
} Else {
$ Cookie = array ();
$ Cookie ['username'] = $ this-> data ['user']
['Username'];
$ Cookie ['Password'] = $ this-> data ['user']
['Password'];
$ This-> cookie-> write ('user', $ cookie, true,
'+ 2 weeks ');
}
Unset ($ this-> data ['user'] ['Remember _ me']);
}
$ This-> redirect ($ this-> auth-> redirect ());

 

7. Use Javascript
Add helper, for example, in appcontroller:
VaR $ helpers = array ('html', 'form', 'javascript ');
Add
<? Php e ($ JavaScript-> Link ('prototype-1.6.0.2 ');?>
<? Php e ($ scripts_for_layout);?>
Put prototype. js and so on under the APP/webroot/JS directory

 

 

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.