Practical Guide to Using Phalcon: Project Architecture, Entry File, Nginx Configuration, Controller Routing, and CRUD Operations
This tutorial walks through building a Phalcon-based PHP project, covering directory structure, the index entry file, Nginx server setup, controller routing, and detailed CRUD examples with code snippets, while also offering tips for code optimization and model management.
The article introduces a Phalcon tutorial series, starting with an overview of the project’s directory layout, which resembles typical MVC frameworks and includes a migrations folder similar to Laravel.
It then explains the essential index.php entry file, noting that Phalcon loads configuration files such as /config/services.php for database connections and /config/router.php for routing.
Next, a simple Nginx configuration is provided for a Windows environment using PhpStudy; the full server block is shown below:
server {
listen 80;
server_name www.kakaweb.com;
root "D:/phpstudy_pro/WWW/phalcon/public";
index index.php index.html error/index.html;
location / {
try_files $uri $uri/ /index.php?_url=$uri&$args;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}The guide then demonstrates controller routing by creating an additional method kaka in the IndexController , accessible via http://www.kakaweb.com/index/kaka , and shows the resulting HTML link output.
CRUD operations are illustrated using Phalcon’s model manager with raw PHQL statements. Insertion example:
public function holdAction() {
$user = new User();
$phql = "INSERT INTO User (name, age, sex) VALUES (:name:, :age:, :sex:)";
$status = $user->modelsManager->executeQuery($phql, array(
'name' => "咔咔1",
'age' => 24,
'sex' => 1
));
}Update example:
public function modifyAction() {
$user = new User();
$phql = "UPDATE User SET name = :name:, age = :age:, sex = :sex: WHERE id = :id:";
$status = $user->modelsManager->executeQuery($phql, array(
'id' => 20,
'name' => "咔咔2",
'age' => 25,
'sex' => 2
));
}Delete example:
public function deleteAction() {
$user = new User();
$phql = "DELETE FROM User WHERE id = :id:";
$status = $user->modelsManager->executeQuery($phql, array(
'id' => 20
));
}The article notes that find() retrieves all records, findFirst() returns the first record, and that PHQL can be used for conditional queries such as find("type = 'mechanical'") .
In the code optimization section, the author points out the repeated instantiation of the User model across methods and suggests a centralized model management approach to simplify future refactoring.
Finally, the summary emphasizes that while the tutorial uses raw SQL‑like PHQL for CRUD, the underlying SQL statements remain consistent across frameworks, and developers should not dismiss these fundamental operations.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.