cakephp 学习小结 6

cakephp 学习小结 6

1 把运行时,cake下部的debug信息改掉,可以这样

   把app\config\core.php的第43行
   * 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);
  改为Configure::write('debug', 0);

2 把比如http://localhost:8082/myphp5/quickwall/home改
为http://localhost:8082/myphp5/quickwall即可访问,
更改app\config\route.php里,改为
  Router::connect('/', array('controller' => 'questions', 'action' => 'home'));
   
 

3 比如自定义验证器的编写
    在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');
}
  该filter在各个action前先执行,象JAVA中的过滤器了。allow则允许哪些action是可以不经过auth验证就执行的。

 


 在app目录下写个app_controller.php,其他类继承它
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('loggedIn', $this->Auth->user('id'));
}


function isAuthorized() {
return true;
}

  其中$this->Auth->loginRedirect,$this->Auth->logoutRedirect指出登陆成功,退出后的路径了。
userScope指明当某个条件成立时,才允许登陆,否则不允许
$this->set('loggedIn', $this->Auth->user('id'));把登陆后的用户id放到loggedin,以方便VIEW层去用。
比如可以这样,在模版里

  
    。。。。。
 
  .....


isAuthorized()是必须有的,这里可以写些成功验证后的函数,无的话这里可以return true

   然后在user_controll里的login,logout可以这样写。
 function login() {
}


function logout() {
$this->Session->setFlash('Logout');
$this->redirect($this->Auth->logout());
}


6 使用cookie
   class AppController extends Controller {
var $components = array('Auth', 'Cookie');
  ...
   在appcontrol中,设置
   $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默认为true,这里为false,即代表要登陆成功后,
要到login中去,就是
  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 使用javascript
   增加helper,比如在appcontroller中:
    var $helpers = array('Html', 'Form', 'Javascript');
  在layout中增加
    link('prototype-1.6.0.2')); ?>
 
  把prototype.js等放到app/webroot/js目录下

你可能感兴趣的:(cakephp,cakephp)