Thứ Sáu, 28 tháng 9, 2018

Đa ngôn ngữ trong Phalcon

1. Tạo thư mục mesages trong thư mục app


2. Trong Controller Base thêm function

protected function getTranslation(){    // Ask browser what is the best language    $language = $this->request->getBestLanguage();    $messages = [];
    $translationFile = APP_PATH . '/messages/' . $language . '.php';

    // Check if we have a translation file for that lang    if (file_exists($translationFile)) {        require $translationFile;    } else {        // Fallback to some default        require APP_PATH . '/messages/en.php';    }
    // Return a translation object $messages comes from the require    // statement above    return new NativeArray(        [            'content' => $messages,        ]    );}

3. Tạo file ngôn ngữ trong thư mục message, ví dụ en.php cho ngôn ngữ tiếng Anh, vi.php cho ngôn ngữ tiếng Việt

en.php
<?php
$messages = [
    'index' =>'Index',    'create' =>'Crate',    'add' => 'Add',    'edit' => 'Edit',    'delete' => 'Delete',   
];

vi.php

<?php

$messages = [    /************* CRUD **************/    'index' =>'Danh sách',    'create' =>'Tạo Mới',    'add' => 'Thêm Mới',    'edit' => 'Chỉnh Sữa',    'delete' => 'Xóa',
];


Note: biến $messages trong docs của phalcon để là $mesagesContent nếu để theo như vậy sẽ không chạy

4. Trong Controller cần xài:

class CityController extends ControllerBase{

    public function IndexAction(){   
        $this->view->cities = $cities;
        $this->view->t      = $this->getTranslation();

    }
}

4. Chỉnh ngôn ngữ của trình duyệt web trùng với ngôn ngữ muốn hiển thị là được


Note: còn 1 cách multi language theo routing nữa

Thứ Ba, 25 tháng 9, 2018

Cách sử dụng form trong module

Giả sử project taydo_trips có 1 module là Backend. Trong module này có 1 thư mục forms chứa file form CityForm. Để sủ dụng form này trong controller của module Backend thì ta làm như sau

Mở file Module.php trong module Backend tìm đến dòng code đăng kí namespace

$loader->registerNamespaces([    'Taydo_trips\Modules\Backend\Controllers' => __DIR__ . '/controllers/',    'Taydo_trips\Modules\Backend\Models' => __DIR__ . '/models/',
]);

Thêm dòng code

'Taydo_trips\Modules\Backend\Form' => __DIR__ . '/forms/',


Mở file CityForm thêm namespace là

namespace Taydo_trips\Modules\Backend\Form;

Thứ Hai, 24 tháng 9, 2018

Cách sử dụng Model trong thư mục common/models trong controller của modules

Gỉa sử có 1 project tên là taydo_trips có module backend.

Để sử dụng model trong thư mục common/models trong controller của module backend thì ta đăng kí thêm namespace trong file Module.php

'Taydo_trips\Models' => APP_PATH . '/common/models/',

Gỉa sử trong thư mục common/model có 1 model là user.php thì trong model này phải có dòng

namespace Taydo_trips\Models;


Bài 4- Volt Filter -phalcon

  1/ Volt Filter Đây cũng là một bộ lọc giống như phần Filter ở bài 3 , tuy nhiên có bổ sung thêm một số phương thức mới để áp dụng vào ứng ...