How to use phalcon-devtools to accelerate Phalcon development

Hello,
I'm Mandai, the Wild Team member of the development team.

Phalcon is a rather unusual framework in that the core of the framework does not exist as a PHP file, but is instead loaded and used by PHP itself.
This structure is what makes it the fastest framework, but as someone familiar with open source, I found it difficult to get started at first because it has no substance.

The phalcon-devtools that we will introduce this time provides phalcon with commands similar to bake in CakePHP and oil in FuelPHP

Installing phalcon-devtools

Installing phalcon-devtools is very easy.
Please see the previous blog post for instructions on how to install phalcon-devtools.

The version of devtools tested this time is Phalcon DevTools (3.0.4)

 

What you can do with phalcon commands

With the phalcon command, you can do various things by setting the following string as the first argument

Let's take a look at each one!

 

phalcon info command

This command displays information about the phalcon runtime environment.
When I ran it, the result was as follows:

Environment:: OS: Linux localhost.localdomain 3.10.0-514.6.1.el7.x86_64 #1 SMP Wed Jan 18 13:06:36 UTC 2017 x86_64 PHP Version: 7.0.15 PHP SAPI: cli PHP Bin: /usr/bin/php PHP Extension Dir: /usr/lib64/php/modules PHP Bin Dir: /usr/bin Loaded PHP config: /etc/php.ini Versions:: Phalcon DevTools Version: 3.0.4 Phalcon Version: 3.0.3 AdminLTE Version: 2.3.6

 

phalcon project command

Create a new project.
If you are creating a new system, you should start with this command.

--name Project Name
--enable-webtools Whether to enable webtools. More on webtools later
--directory=s Create the directory specified by --name in the specified path and extract the project (default ./)
--type=s Select the phalcon application type

  • cli
  • micro
  • modules
  • simple (default)
--template-path=s If no options are given, the initial files will be created using the templates provided with phalcon-devtools

When creating an original template, it seems like it would be a good idea to use the included template as a base

--use-config-ini If you add it, app/config/config.php becomes config.ini
--trace not clear
--help Viewing the options list

If --type=modules is selected, a module configuration of front + cli will be created by default

If you run commands other than the phalcon project outside the phalcon project (more precisely, in the path after being rewritten with --directory), the following error will occur

Error: This command should be invoked inside a Phalcon project directory.

To aid in this determination, projects created with the "phalcon project" command contain a directory called ".phalcon."

 

phalcon module command

This command does not have a base directory path or other settings like the phalcon project, so it must be run within the phalcon project directory

--name New module name to add
--namespace=s Add namespace in Module.php
--output=s Specify the path to the modules directory. This is treated as an option, but if you do not specify it, an error will occur, so it can be considered essential

phalcon module --name module1 --output=./app/modules

--config-type=s This is the file type of the configuration file. The default is php, but you can also select other formats

  • ini
  • json
  • php
  • yaml
--template-path=s Specify the directory path that contains the template file when creating a module. The basic template is in the devtools module directory
--help Viewing the options list

 

phalcon controller command

Even if you run this command directly under the project directory, an error occurs saying "Error: This command should be invoked inside a Phalcon project directory.", so it seems that the --output option is essential

--name=s Specify the controller name. If you created the project with type=simple (or no specification), you can create a controller with only this option
--namespace=s Specifies the namespace of the controller
--directory=s Enter the path to the project containing the controller. If you forget the final slash, it may not work
--output=s Specify the directory path where the controller is located. Required for module configuration
--base-class=s Set the inheritance class. Since the use operator is not used, it is necessary to include the namespace
--force If the specified controller already exists, it will be overwritten and created
--help Viewing the options list

 

phalcon model command

This command contains functions related to various models.
Before executing this command, you must complete the DB access settings in config/config.php.

If you create a model for a table that has a field with a hyphen, the creation will work, but you will get an error because PHP rules state that hyphens cannot be used in class property names

PHP Parse error: syntax error, unexpected '-', expecting ',' or ';' in /path/to/project/app/models/users.php on line 34

Be careful when designing your database

--name=s Specify the table name to create the model for
--schema=s Specify the schema to retrieve table information required for model creation. This is necessary when retrieving from a schema other than that written in config.php
--namespace=s Set the namespace for the model you are creating
--get-set Normally, columns in a table are created as public properties of a class, but we will make them protected properties and provide getters/setters
--extends=s Create a model class by inheriting the class specified here
--excludefields=l You can specify comma-separated fields for which you do not want to create properties
--doc Created with PHPDoc-style comments
--directory=s Enter the path to the model you want to create. This option doesn't work with modules (because there is a module directory between them)
--output=s Specifies the output directory. Required for module configuration
--force This is an option to overwrite an existing model
--camelize If the column name is snake case, create the property in camel case. Default is as is
--trace Shows the trace of the framework in case of exception [optional]
--mapcolumn Add a columnMap method to get a list of field names in a table in your code
--abstract Create an abstract class to model the target table. Classes inheriting from this class will not be generated, so you will need to create them separately using the "phalcon model" command
--annotate It seems to be an option to add annotations, but it's on by default, so there was no change when I took a diff
--help Viewing the options list

 

phalcon all-models command

If you have already designed the database for your application and the tables have been created in the database, you can run the "phalcon all-models" command without any arguments to quickly create models for all existing tables. This
is essentially the same as the "phalcon model" command.

Once this is done, it may be a good idea to recreate any models that require individual configuration using the "phalcon model" command with the --force option

--config=s Configuration file [optional]
--models=s Models directory [optional]
--schema=s Switches the schema for model creation. By default, it works as described in config.php, but this overrides the destination

Even if you specify it here, if the schema is not set in config.php, the error "ERROR: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected" will occur

--namespace=s Set the namespace for the model you create
--extends=s Set the inheritance class. Since the use operator is not used, it is necessary to include the namespace
--force Overwrites the file even if it already exists
--get-set Normally, columns in a table are created as public properties of a class, but we will make them protected properties and provide getters/setters
--doc Created with PHPDoc-style comments
--relations If the target table has a foreign key constraint, a relation setting will be added
--fk If the target table has a foreign key constraint, a relation setting will be added
--validations Define possible domain validation according to conventions
--directory=s Sets the path to the project root. This setting is only valid if the project type is simple
--mapcolumn Add a columnMap method to get a list of field names in a table in your code
--abstract Create your model as an abstract class. This creates an abstract class for all tables, which may be a useful option depending on your design philosophy
--help Viewing the options list

 

phalcon migration command

This is a command that can handle DB migration

Even if the number of columns is reduced due to a version downgrade, the table is not recreated but the columns are deleted using ALTER TABLE, so it is unlikely that the table will become empty

If you run "phalcon migration generate" without any arguments, it will use version number based versioning.
If you use the --descr option with generate, it will switch to timestamp based versioning.

What makes it confusing is that you can mix both types of migrations

After trying various things, it seems that it is best to perform normal migrations using version number management, and when you want to create a special migration file for a specific timing, perform the migration with the --descr option

For example, create migration A created with version 1.0.0, and then create migration B with the same content but with the --descr option.
Even if you run migration B while migration A is applied, you will get the message "Info: Version 1.0.0 was already applied", so it seems that migration A and migration B are internally confirmed to be the same.

--action=s Use generate to create, run to execute, and list to display a list of all migrations

If you do not specify a version, all migrations will be run. You can also run it with phalcon migration [generate | run]

--config=s Specify the path to config.php
--migrations=s Specify the path to the migrations directory. This is only required when project=modules
--directory=s Set the path to the project root
--table=s Set the table name to be migrated
--version=s Specify the migration version you want to change
--descr=s Adds a migration description to the migration. This option is only valid when using generate, and only with this option will the migration file be timestamp-based
--data=s Export the data and save it in a dat file (the contents appear to be CSV). Choose between always and oncreate, and a migration file will be created so that it will be inserted when uploading
--force This option is only valid when a version is specified with the --version option. If not specified, the highest version number will be assigned
--ts-based Work on timestamp-based versioned migrations. This option is only valid when the --action option is run or list, and is ignored when using generate
--log-in-db Normally, the current migration version is written in "./.phalcon/migration-version", but by adding this option, you can change it to manage the version in the DB
--no-auto-increment Do not save the current value of the increment
--help Viewing the options list

 

phalcon scaffold command

Running this command will create the model/view/controller all at once

We will also create screens with search, new creation, and update functions.
Each function is created with its own implementation in the controller, so if you are creating a page that only performs simple CRUD, it will be much faster to implement it by modifying what was created in the scaffold.

--table-name=Specify the table to create sscaffold for
--schema=s Specify the schema in which the table to be created exists
--get-set Normally, in a model, columns in a table are created as public properties of a class, but we will make them protected properties and provide getters/setters
--directory=s Set the project root
--template-path=s If you want to specify a separate template file that will be the base for scaffolding, set the path with this option
--template-engine=s Specify the template engine to use. The default is PHP, but you can also select Volt
--force A set of files created by scaffolding. If a file already exists, it will be overwritten and recreated
--trace Shows the trace of the framework in case of exception [optional]
--ns-models=s Option to specify the namespace of the model
--ns-controllers=s This option specifies the namespace of the controller
--help Viewing the options list

 

phalcon webtools commands

This module is laid out with Twitter Bootstrap and allows you to easily use Phalcon commands in the browser.
It can be disabled later, so you can make it available during development and disable it when releasing.

Phalcon commands have many functions, so it's great to be able to easily configure them using the GUI

--action=s There are two arguments: [enable|disable], which allows you to use the GUI to execute various Phalcon commands from a browser
--help Viewing the options list

 

Included items

Directly under the phalcon-devtools directory is a directory called ide, which
contains tools that make it easier to implement Phalcon in an IDE.

 

This is a script to create a stub.
Replace the CPHALCON_DIR constant at the beginning of the program with the path where Phalcon.so is installed.

// Example define('CPHALCON_DIR' , '/usr/lib64/php/modules/');

After rewriting, when you run it, it will create a directory named after the Phalcon version and generate stubs for the current Phalcon environment.
However, the latest version of stubs is included, so there is little need to run it yourself.

 

It contains a command line tool for running phalcon commands from the phpstorm console.
It is called phalcon.sh and does not check the installation, but it may be useful to register it.

 

This contains stubs for functions implemented in the latest version of Phalcon.
If you are using the latest version, it is easier to use this instead of running gen-stubs.php.

 

Template Modification

We now know that the phalcon command can automatically generate projects, models, and controllers, but if you create several projects, you'll probably find yourself making the same changes every time.
The Phalcon command uses templates for various automatic generation tasks, so if you rewrite the templates, you'll be freed from such tedious work!
Templates are used for projects, modules, and scaffolds, and the parts surrounded by "@@" are replaced, so it's a good idea to keep modifying the base parts to make them easier to use.

Once you have installed phalcon-devtools, you can copy the template to ~/Documents using the following command:

lang=bash cp -a $PTOOLSPATH/templates ~/Documents

 

That's all

If you found this article useful, please click [Like]!
0
Loading...
0 votes, average: 0.00 / 10
3,367
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Yoichi Bandai

My main job is developing web APIs for social games, but I'm also grateful to be able to do a variety of other work, including marketing.
My portrait rights within Beyond are CC0.