2014年3月19日 星期三

cakePHP 心得

使用cookie
http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html
在你的Controller加入
public $components = array('Cookie');
public function beforeFilter() {
parent::beforeFilter();
$this->Cookie->name = 'baker_id';
$this->Cookie->time = 3600;  // or '1 hour'
$this->Cookie->path = '/';
$this->Cookie->domain = 'cake.localhost';
//$this->Cookie->secure = true;  // i.e. only sent if using secure HTTPS
//$this->Cookie->key = 'qSI232qs*&sXOw!adre@34SAv!@*(XSL#$%)asGb$@11~_+!@#HKis~#^';
$this->Cookie->httpOnly = false;  //設為false,才能使用js去讀他
}

$this->Cookie->write('name', 'Larry', false); //設為 false 在cookie前端才不會編碼,cakePHP預設為編碼

使用jQuery存取cookie
https://developers.google.com/speed/libraries/devguide#jquery
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
jQuery 1.x 兼容IE
jquery.cookie
http://cdnjs.com/libraries/jquery-cookie/
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.0/jquery.cookie.js"></script> //速度沒google cdn快
使用方法:
$.cookie('baker_id[name]')

在View中使用cookie
http://stackoverflow.com/questions/7525428/cakephp-cookie-does-not-read-at-view
There is no Cookie helper in CakePHP, neither a method to access it in View.
所以只能在Controller用set(),然後在view讀取那個變數
Controller:
$this->set('myValue', $this->Cookie->read('cookieValue'));
View:
<?php echo $myValue; ?>
或使用$_COOKIE ( break the MVC pattern )  => 使用$_COOKIE叫時,cakePHP的Cookie不能加密 - $this->Cookie->write('myValue', $value, false);

http://stackoverflow.com/questions/19741075/cakephp-reaching-cookies-from-the-view-files
在你的Controller的beforeFilter() 加入這行
$this->set('cookie', $this->Cookie->read('cookie_key'));
但是該cookie_key在controller後面有被改,該值會是舊的

http://stackoverflow.com/questions/3264498/how-to-read-cookie-value-in-cakephp-view-file ( 採用 )
Use Cookie components in AppController:
$components = array('Cookie');
Define following in AppController's beforeFilter():
$this->set('cookieHelper', $this->Cookie);
So that you could use it in view:
$cookieHelper->read('something');
用上面這方法cookie在view取會lag,還是使用傳統方法:
$this->set('myValue', $this->Cookie->read('cookieValue'));


取目前domain
http://stackoverflow.com/questions/4298552/base-url-in-cakephp
Router::fullbaseUrl();  // http://one.com
Router::url('/', true);  // http://one.com
$_SERVER['SERVER_NAME'];  //one.com
$pieces = parse_url(Router::url('/', true));
echo $pieces['host']; //one.com

在view中抓現在的網址
http://stackoverflow.com/questions/13034267/in-viewcakephp-the-proper-way-to-get-current-controller
$this->params['controller']; //抓現在的controller
echo $this->here; //  抓現在的位置,/Users/login
echo Router::url( $this->here, true );  // true或false 選擇是否為完整網址

取最後五筆資料
http://stackoverflow.com/questions/10150717/output-last-5-rows-in-database-cakephp
Controller:
$alltext = $this->Text->find('all', array('limit' => 5,'order'=>array('id DESC')));

Controller 裡面的 $this->data 是表單post的資料,注意不能設這邊的值

recursive
http://book.cakephp.org/2.0/en/models/model-attributes.html#recursive
※ -1 CakePHP fetches Group data only, no joins.  // 建議的值,可以在AppModel裡設置public $recursive = -1; ,在pagiante時要用joins必須設-1
※ 0 CakePHP fetches Group data and its domain
※ 1 CakePHP fetches a Group, its domain and its associated Users //預設值
※ 2 CakePHP fetches a Group, its domain, its associated Users, and the Users’ associated Articles

cakePHP表單裡用echo $this->Form->create();產生的隱藏input :  _method
<input type="hidden" value="POST" name="_method">
官網文件: Since this is an edit form, a hidden input field is generated to override the default HTTP method.
測試結果: 只對 $this->request->is('post') 和 $this->request->is('get') 影響,$_GET , $_POST, $this->reuqest->query (GET),$this->request->data (POST) 沒影響

移除表單Helper input的label和外層div
$form->input('Project/name', array('label' => false, 'div' => false));
或在Form->create裡設定
$this->Form->create('Device', array(
'inputDefaults' => array(
'label' => false,
'div' => false
)
));
或 在表單之前設定
$this->Form->inputDefaults(array(
'label' => false,
'div' => false,
));

必須設定對應的View,即使在Layouts/default.ctp裡extend成另一個view
不然雖然畫面正常,但在view或elements裡面的變數叫不出來
在$this->viewVars['name'] 會顯示 Missing ...xxx.ctp
在ajax request可以使用 $this->autoRender = false; 讓cakephp停止叫view



沒有留言:

張貼留言