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層去用。
比如可以這樣,在模版裏

   <?php if($loggedIn): ?>
    。。。。。
 <?php else?>
  .....
<?php end if;?>


isAuthorized()是必須有的,這裏可以寫些成功驗證後的函數,無的話這裏可以return true

   然後在user_controll裏的login,logout可以這樣寫。
 function login() {
}


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


6 使用cookie
   <?php
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中增加
    <?php e($javascript->link('prototype-1.6.0.2')); ?>
 <?php e($scripts_for_layout); ?>
  把prototype.js等放到app/webroot/js目錄下

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章