Chủ Nhật, 25 tháng 10, 2020

Cách tạo scaffold (thêm, sữa xóa) cho thấy table sử dụng template volt trong phalcon

 Ở thư mục gốc của project vào cmd hay Window PowerShell gõ lệnh 

 phalcon scaffold table-name --template-engine=volt

Với table-name là tên table trong database cần tạo scaffold

Phalcon sẽ tự tạo Controller, Model, View phục vụ cho việc thêm, sửa, xóa table


Thứ Bảy, 17 tháng 10, 2020

Cách cài Phalcon Devtools 4.0 bằng Composer

Giữ phím Shift rồi click chuột phải để mở Windows PowerShell rồi gõ lệnh

composer global require phalcon/devtools


Rồi gõ thử lệnh phalcon nếu hiện ra như hình dưới là thành công





Thứ Hai, 27 tháng 7, 2020

Hướng dẫn cài đặt Phalcon



- Để cài cài phalcon bắt buộc phải cài PSR extension cho PHP
- Chú ý phiên bản PHP mà bản phalcon muốn cài hỗ trợ (Xampp chỉ có bản 32 bit mà thôi)
- Check phiên bản PHP  threadsafe hay nonthreadsafe bằng lệnh php -i|findstr "Thread"  để chọn bản phalcon và psr hỗ trợ (sau khi cài đặt biến môi trường cho PHP)
- Chú ý các cấu hình sau trong phpinfo
   + Architecture 64 hay 32
   + Compiler MSVC15 (Visual C++ 2017)'

Sữa lỗi load Controller khi mới tạo project ở Phalcon 4

StoreController handler class cannot be loaded
#0 [internal function]: Phalcon\Mvc\Dispatcher->throwDispatchException('StoreController...', 2)
#1 [internal function]: Phalcon\Dispatcher\AbstractDispatcher->dispatch()
#2 E:\wamp64\www\store\public\index.php(43): Phalcon\Mvc\Application->handle('/store/')
#3 {main}
Vào file /public/index.php

Nếu là project có nhiều modules thì vào thư mục app mở file bootstrap_web.php
sữa 
echo $application->handle($_SERVER['REQUEST_URI'])->getContent();
thành
echo $application->handle($_GET['_url'] ?? '/')->getContent();
Chi tiết sữa lỗi này ở

Chủ Nhật, 4 tháng 11, 2018

Các tạo multi modules cho phalcon framework

.Dùng lệnh

phalcon project --name=taydo_trips --type=modules. Trong nó taydo_trips sẽ là tên project

khi chạy lệnh này thành công sẽ tạo ra 1 project phalcon có sẵn 1 module là frontend.

Copy thư mục frontend này và paste lại với tên khác vị dụ là backend.

Nếu dùng devltools để tạo module thì ở thự mục project có chứa thư mục app giữ Shift click chuột phải mở Window PowerShell chạy lệnh sau (cách này chưa thực hiện thành công)

phalcon module modulename --output="app/modules"

Với modulename là tên module bạn muốn tạo

Trong file loader.php (app/config) trong đoạn này

$loader->registerClasses([    'Taydo_trips\Modules\Frontend\Module' => APP_PATH . '/modules/frontend/Module.php',    'Taydo_trips\Modules\Cli\Module'      => APP_PATH . '/modules/cli/Module.php',    
]);

Thêm đoạn code

'Taydo_trips\Modules\Backend\Module' => APP_PATH . '/modules/backend/Module.php',

File Module.php (modules/backend) tìm kiếm từ Frontend và thay thế thành Backend


File Bootstrap_web đoạn code

$application->registerModules([    'frontend' => ['className' => 'Taydo_trips\Modules\Frontend\Module'],    
]);


thêm đoạn code

'backend' => ['className' => 'Taydo_trips\Modules\Backend\Module'],

Mấy cái namespace của Controller của Backend sẽ sử dụng

Taydo_trips\Modules\Backend\Controllers;






Thứ Hai, 22 tháng 10, 2018

Cách update document mongo có chứa nested document ko bị lỗi thêm các field thừa

Trước tiên tải PHP Library for MongoDB (PHPLIB) bằng composer bằng lệnh

 composer require mongodb/mongodb

Hoặc tải tại địa chỉ

https://github.com/mongodb/mongo-php-library

Để  PHP Library for MongoDB vào thưc mục common/vender

Mở file index.php hoặc bootstrap_web.php(đối với ứng dụng multi modules) thêm đoạn code

require APP_PATH . '/common/vendor/autoload.php';


Hàm kết nối csdl mongo trong file services.php

/* *  new MongoDB\Client và new Client() là tương đương nhau * new MongoDB\Client nằm trong library mongo for php 7. * new Client nằm trong incubator * Nội dụng 2 Client này tương đương nhau */
$di->set('mongo', function () use ($di) {   // $config  = $di->get('config')->mongodb;    $client = new MongoDB\Client('mongodb://' . $this->getShared('config')->database->host . ':' . $this->getShared('config')->database->port);    return $client->selectDatabase($this->getShared('config')->database->dbname);}, true);//MongoDB Database/*$di->setShared('mongo',function(){    $mongo = new Client();
   return $mongo->selectDatabase($this->getShared('config')->database->dbname);
});*/


Giả sử ta có class Places chứa thuộc tính Location, Location là 1 class có chứa 2 thuộc tính lat và lng như sau

Class Location

class Location{    public $lat;    public $lng;
}


Class Place

class Place extends MongoCollection{
    public $name;
    //private $description;
    public  $location;
}

Phương thức xử lí tạo mới và update thông tin Place là saveAction() có code như sau

$location = new Location();
$place->name     = $this->request->getPost('name',['striptags','trim']);
$place->address  = $this->request->getPost('address',['striptags','trim']);
$place->city_id  = $this->request->getPost('city_id',['striptags','trim']);
$location->lat   = $this->request->getPost('lat',['striptags', 'trim']);
$location->lng   = $this->request->getPost('lng',['striptags', 'trim']);
$place->location = $location;

if(isset($_id)){ // Xử lí update khi biến $_id đã có giá trị
 /*   Khi sử dụng new PlaceForm($place) thì nó sẽ tự động gán thêm 
2 thuộc tính lat và lng vào place   nếu ko unset hoặc dùng $this->mongo->Places->updateOne thì trong documanet place sẽ thêm 
2 trường lat và lng
    $this->mongo->Places->updateOne(         ["_id" => new ObjectID($_id)],         ['$set' =>[ 'name'     => $place->name,                     'address'  => $place->address,                     'city_id'  => $place->city_id,                     'location' => $location,                     'photos'   => $place->photos,             ]
         ]     );*/   unset($place->lat);   unset($place->lng); }$place->save();


Nếu ko dùng cách trên thì khi update Place sẽ có thêm 2 filed lat và lng trong document của Collection Place



Thứ Hai, 1 tháng 10, 2018

Cach hiển thị lỗi validation chi tiết đến từng thành phần của form

Ta có SignUpForm

use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Forms\Element\Password;
use Phalcon\Forms\Element\Submit;
use Phalcon\Forms\Element\Check;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Email;
use Phalcon\Validation\Validator\Identical;
use Phalcon\Validation\Validator\StringLength;
use Phalcon\Validation\Validator\Confirmation;
class SignUpForm extends Form
{
public function initialize($entity = null, $options = null)
{
$name = new Text('name');
$name->setLabel('Name');
$name->addValidators([
new PresenceOf([
'message' => 'The name is required'
])
]);
$this->add($name);
// Email
$email = new Text('email');
$email->setLabel('E-Mail');
$email->addValidators([
new PresenceOf([
'message' => 'The e-mail is required'
]),
new Email([
'message' => 'The e-mail is not valid'
])
]);
$this->add($email);
// Password
$password = new Password('password');
$password->setLabel('Password');
$password->addValidators([
new PresenceOf([
'message' => 'The password is required'
]),
new StringLength([
'min' => 8,
'messageMinimum' => 'Password is too short. Minimum 8 characters'
]),
new Confirmation([
'message' => 'Password doesn\'t match confirmation',
'with' => 'confirmPassword'
])
]);
$this->add($password);
// Confirm Password
$confirmPassword = new Password('confirmPassword');
$confirmPassword->setLabel('Confirm Password');
$confirmPassword->addValidators([
new PresenceOf([
'message' => 'The confirmation password is required'
])
]);
$this->add($confirmPassword);
// Remember
$terms = new Check('terms', [
'value' => 'yes'
]);
$terms->setLabel('Accept terms and conditions');
$terms->addValidator(new Identical([
'value' => 'yes',
'message' => 'Terms and conditions must be accepted'
]));
$this->add($terms);
// CSRF
$csrf = new Hidden('csrf');
$csrf->addValidator(new Identical([
'value' => $this->security->getSessionToken(),
'message' => 'CSRF validation failed'
]));
$csrf->clear();
$this->add($csrf);
// Sign Up
$this->add(new Submit('Sign Up', [
'class' => 'btn btn-success'
]));
}
/**
* Prints messages for a specific element
*/
public function messages($name)
{
if ($this->hasMessagesFor($name)) {
foreach ($this->getMessagesFor($name) as $message) {
$this->flash->error($message);
}
}
}
}


Note: chú ý function messages 

Trong code volt template signup.volt

<div align="center">
{{ form('class': 'form-search') }}
<div align="left">
<h2>Sign Up</h2>
</div>
<table class="signup">
<tr>
<td align="right">{{ form.label('name') }}</td>
<td>
{{ form.render('name') }}
{{ form.messages('name') }}
</td>
</tr>
<tr>
<td align="right">{{ form.label('email') }}</td>
<td>
{{ form.render('email') }}
{{ form.messages('email') }}
</td>
</tr>
<tr>
<td align="right">{{ form.label('password') }}</td>
<td>
{{ form.render('password') }}
{{ form.messages('password') }}
</td>
</tr>
<tr>
<td align="right">{{ form.label('confirmPassword') }}</td>
<td>
{{ form.render('confirmPassword') }}
{{ form.messages('confirmPassword') }}
</td>
</tr>
<tr>
<td align="right"></td>
<td>
{{ form.render('terms') }} {{ form.label('terms') }}
{{ form.messages('terms') }}
</td>
</tr>
<tr>
<td align="right"></td>
<td>{{ form.render('Sign Up') }}</td>
</tr>
</table>
{{ form.render('csrf', ['value': security.getToken()]) }}
{{ form.messages('csrf') }}
<hr>
</form>
</div>



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 ...