# CakePHP 2.4.3 learning
## Controller
### Set
Set biến truyền qua view ở Controller
```php=
$data = array(
'color' => 'pink',
'type' => 'sugar',
'base_price' => 23.95
);
// make $color, $type, and $base_price
// available to the view:
$this->set($data);
```
Bên view sử dụng
```htmlmixed=
You have selected <?php echo $color; ?> icing for the cake.
```
### Render
Mặc định Cake render theo tên controller và function, có thể tuỳ chỉnh render bằng cách
```php=
// Render the element in /View/Elements/ajaxreturn.ctp
$this->render('/Elements/ajaxreturn');
```
Nếu chỉ muốn chỉ định file cùng thư mục thì chỉ cần truyền tên file trong thư mục đó, ví dụ PostsController
```php=
// Render the element in /View/Posts/custom_page.ctp
$this->render('custom_page');
```
### Flash session
```php=
$this->Session->setFlash(__('Your post has been saved.'));
```
### Redirect
Có thể redirect tới function controller
```php=
public function place_order() {
return $this->redirect(
array('controller' => 'orders', 'action' => 'confirm')
);
}
```
Redirect tới url
```php=
$this->redirect('/orders/thanks');
$this->redirect('http://www.example.com');
```
Redirect tới function ngay trong controller của nó
```php=
$this->redirect(array('action' => 'edit', $id));
```
### Load Model
Dùng để gọi một model mà nó không liên quan tới controller đó, gọi trong từng function.
```php=
$this->loadModel('Article');
$recentArticles = $this->Article->find(
'count',
array('limit' => 5, 'order' => 'Article.created DESC')
);
```
Load cho toàn bộ controller
```php=
// đầu file
App::uses('Article', 'Model');
// trong function sử dụng
$articleModel = new Article();
$totalArticle = $articleModel->find('count');
```
### Request
Những cách lấy parameters từ request
```php=
$this->request->controller;
$this->request['controller'];
$this->request->params['controller'];
```
Lấy Querystring parameters
```php=
// URL is /posts/index?page=1&sort=title
$this->request->query['page'];
```
Đối với request là kiểu Post
```php=
// An input with a name attribute equal to 'data[MyModel][title]'
// is accessible at
$this->request->data['MyModel']['title'];
```
Kiểm tra request
```php=
$this->request->is('post');
```
## View
### Layouts
Trong controller set layout cho view, ví dụ tạo một layout tại `app/View/Layouts/Common/base.ctp`
Trong Controller ở function index gọi.
```php=
public function index() {
$this->layout = 'Common/base';
}
```
### Include elements
Khi muốn tách các element trong layouts để dùng chung, ví dụ `sidebar`, `nav`....v...v...
Ví dụ: tạo 1 file element tại `app/View/Elements/Common/sidebar.ctp` lúc này trong file `base.ctp` ta gọi:
```php=
<?php echo $this->element('Common/sidebar');?>
```
### Extending views
Tương tự Laravel, CakePHP cũng có chế độ Extend, ngoài `start` có thể dùng thêm `append`,`assign`,`prepend`
Ví dụ trang `base.php`
```htmlmixed=
// app/View/Layouts/base.ctp
<h1><?php echo $this->fetch('title'); ?></h1>
<?php echo $this->fetch('content'); ?>
<div class="actions">
<h3>Related actions</h3>
<ul>
<?php echo $this->fetch('sidebar'); ?>
</ul>
</div>
```
```htmlmixed=
// app/View/Posts/view.ctp
$this->extend('/Layouts/base');
$this->assign('title', $post);
$this->start('sidebar');
?>
sidebar
<?php $this->end(); ?>
```
Custom Flash message
```php=
$this->Session->setFlash('Success message!', 'success_alert');
$this->Session->setFlash('Error message!', 'error_alert');
```
Tạo 2 file tương ứng ví dụ: `/View/Elements/error_alert.ctp` và `/View/Elements/success_alert.ctp`
```php=
<?php echo $message ?>
```
## Model
Cập nhật, nếu không có trường ID có nghĩa là tạo mới
```php=
$data = array('id' => 10, 'title' => 'My new title');
// This will update Recipe with id 10
$this->Recipe->save($data);
```
```php=
$data = array(
'ModelName' => array(
'foo' => $foo
)
)
// prepare the model for adding a new entry
$this->ModelName->create();
// save the data
$this->ModelName->save($data);
```
Kết nối table đặt tên không theo quy chuẩn của cake
```php=
class Example extends AppModel {
public $useTable = 'exmp'; // This model uses a database table 'exmp'
}
```
## Route
Có thể tuỳ chỉnh route trong file `application/app/Config/routes.php`
```php=
Router::connect(
'/government',
array('controller' => 'pages', 'action' => 'display', 5)
);
```