[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

How to use phalcon-devtools to accelerate Phalcon development

Hello.
I'm Mandai, in charge of Wild on the development team.

phalcon is a very rare framework in which phalcon.so is loaded into PHP itself and the core part of the framework does not exist as a PHP file.
This format is the reason why the framework is the fastest, but as someone who is familiar with open source, I found it difficult to get used to at first because it lacks substance.

The phalcon-devtools introduced here provides commands like bake in CakePHP and oil in FuelPHP to phalcon.

Installing phalcon-devtools

Installing phalcon-devtools is very easy.
Please check the previous blog for a summary of how to install phalcon-devtools.

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

 

What you can do with the phalcon command

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

Let's take a look at them one by one!

 

phalcon info command

This command displays phalcon execution environment information.
When I executed 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.
When creating a new system, start with this command.

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

  • cli
  • micro
  • modules
  • simple (default)
--template-path=s If there are no options, use the template included with phalcon-devtools to build the initial files.

When creating an original template, it is recommended to use the included one as a base.

--use-config-ini When added, app/config/config.php becomes config.ini
--trace not clear
--help Show option list

If --type=modules, it will be created with the combination front + cli module configuration by default.

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

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

To be used for this determination, the project created with the "phalcon project" command contains a directory called ".phalcon".

 

phalcon module command

Unlike phalcon project, this command does not have any settings such as a base directory path, so it must be executed within the phalcon project directory.

--name Newly added module name
--namespace=s Add namespace for Module.php
--output=s Specify the path of the modules directory. It is treated as an option, but if you do not specify it, an error will occur, so I think it can be considered mandatory.

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

--config-type=s This is the file type setting for the configuration file. Default is php, but other formats are also available

  • ini
  • json
  • php
  • yaml
--template-path=s Specify the directory path containing the template file when creating the module. The basic template is in the devtools module directory
--help Show option list

 

phalcon controller command

Even if I run this command directly under the project directory, the error "Error: This command should be invoked inside a Phalcon project directory." occurs, so I feel like the --output option is required.

--name=s Specify the controller name. If you create a project with type=simple (or without specifying it), you can create a controller using only this option.
--namespace=s Specify the controller namespace
--directory=s Enter the path to the project that contains your controller. If you forget the last slash, it doesn't seem to work properly.
--output=s Specify the directory path where the controller will be placed. Required for module configuration
--base-class=s Set the inheritance source class. Since the use operator is not used, the namespace must also be included in the description.
--force Overwrites and creates the specified controller even if it already exists
--help Show option list

 

phalcon model command

These commands contain functions related to various models.
Before running this command, you need to complete the DB access settings in config/config.php.

If you create a table model that has a field with a hyphen in it, you can create it, but as a rule on the PHP side, an error will occur stating that you cannot use a hyphen in the name of a class property.

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

Please be careful when designing the DB.

--name=s Specify the table name to create the model
--schema=s Specify the schema to obtain table information required for model creation. Required when retrieving from a schema other than the one written in config.php
--namespace=s Set the namespace of the model to be created
--get-set Columns in a table are usually created as public properties of a class, but make this a protected property 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 Create with comments in PHPDoc format
--directory=s Enter the path to the model you want to create. This option does not work well in a module configuration (of course, since there is a module directory in between)
--output=s Specify the output destination directory. Required for module configuration
--force This is an option to add when creating an existing model by overwriting it.
--camelize If the column name is snake case, create the property using camel notation. Default is left as is
--trace Shows the trace of the framework in case of exception [optional]
--mapcolumn Add a columnMap method that allows you to get a list of field names in the table in code.
--abstract Create an abstract class to model the target table. A class that inherits this will not be generated, so you need to create it separately using the "phalcon model" command.
--annotate It seems to be an option to add annotations, but since it is attached by default, there was no change even if I took a diff.
--help Show option list

 

phalcon all-models command

If the DB design for the application you are about to create has been completed and the tables have been built on the DB, you can quickly create models for all existing tables by running the "phalcon all-models" command without any arguments. Masu.
It is basically the same as the "phalcon model" command.

After this is completed, it may be a good idea to recreate the models that require individual settings using the "phalcon model" command with the --force option added.

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

Even if it is specified here, if the schema is not set in config.php, an 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 source class. Since the use operator is not used, the namespace must also be included in the description.
--force Overwrites even if a file with the same name already exists
--get-set Columns in a table are usually created as public properties of a class, but make this a protected property and provide getters/setters.
--doc Create with comments in PHPDoc format
--relations If the target table has a foreign key constraint, relation settings will be added.
--fk If the target table has a foreign key constraint, relation settings will be added.
--validations Define possible domain validation according to conventions
--directory=s Set the project root path. This setting is valid only if the project type is simple
--mapcolumn Add a columnMap method that allows you to get a list of field names in the table in code.
--abstract Create your model as an abstract class. It creates an abstract class for all tables, so it may be a useful option depending on your design philosophy.
--help Show option list

 

phalcon migration command

This is a command that can perform processing related to DB migration.

Even if the number of columns decreases due to a version downgrade, etc., the columns are deleted using ALTER TABLE instead of recreating the table, so it is unlikely that the table will become empty.

If you run "phalcon migration generate" without any arguments, version control will be performed using version numbers.
If you use the --descr option with generate, it will switch to timestamp-based version control.

What's confusing is that both types of migration can be mixed.

After trying various things, it seems to be better to use version number management for normal migration, and to run migration with the --descr option when you want to create a special migration file for a specific timing. .

For example, create migration A created with version 1.0.0, and create migration B with the same content but with the --descr option.
Even if migration B is executed with migration A applied, the message "Info: Version 1.0.0 was already applied" will be displayed, indicating that migration A and migration B are internally the same. Looks like it's been confirmed.

--action=s Create with generate, execute with run, and display a list of all migrations with list.

If no version is specified, all migrations will be executed. It can also be executed with phalcon migration [generate | run]

--config=s Specify the path of config.php
--migrations=s Specify the path to the migrations directory. It seems to be needed only if 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 Add a migration description to the migration. This option is valid only when generating, and only when this option is specified, the migration file will be timestamp-based.
--data=s Export the data and save it in a dat file (the contents are CSV patterns). Select two types: always and oncreate, and a migration file will be created to be inserted at the time of upload.
--force Even if a migration file exists, it will be overwritten and created. This option is valid only if the version is specified with the --version option; if not specified, the highest version number will be assigned.
--ts-based Work with timestamp-based versioned migrations. This option is valid only when the --action option is run, list, and ignored when it is 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 version control within the DB.
--no-auto-increment Prevents saving the current value of the increment
--help Show option list

 

phalcon scaffold command

Executing this command will create models/views/controllers in bulk.

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

--table-name=Specify table to create sscaffold
--schema=s Setting the schema where the table to be created exists
--get-set Normally, columns in a table are created as public properties of a class in a model, but make this a protected property 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 of the scaffold, set the path with this option.
--template-engine=s Specify the template engine to use. Default is PHP, other option is volt
--force A set of files created with scaffol that overwrites and recreates any files that already exist.
--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 Show option list

 

phalcon webtools command

Incorporate into your project a module that makes it easy to use Phalcon commands on the browser, laid out with Twiiter Bootstrap.
You can make it unavailable later, so you can make it available during development and disable it at release.

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

--action=s There are two types of arguments: [enable|disable], which enable you to use the GUI to execute various Phalcon commands from the browser.
--help Show option list

 

Included items

There is a directory called ide directly under the phalcon-devtools directory.
It mainly contains tools that make it easier to implement Phalcon in an IDE.

 

This is a script for creating a stub.
Rewrite the CPHALCON_DIR constant that appears at the beginning of the program to the path where Phalcon.so is installed.

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

After rewriting, when you run it, a directory named after the Phalcon version will be created and a stub for the current Phalcon environment will be generated.
However, the latest version of the stub is included, so there is no need to run it yourself.

 

Inside is a command line tool that allows you to run the phalcon command from the phpstorm console.
The contents are called phalcon.sh, which does not check the installation, but it may be useful to register it.

 

Contains stubs of functions implemented in the latest version of Phalcon.
If you want to use the latest version, it is easier to use this instead of running gen-stubs.php.

 

Modification of template

I understand that projects, models, and controllers can be automatically generated using the phalcon command, but if you create several projects, there will probably be parts that need to be modified in the same way each time.
Phalcon commands use templates when automatically generating various types, so if you rewrite the templates, you can be freed from such complicated work!
Templates are used by projects, modules, and scaffolds, and the parts enclosed in "@@" are replaced, so the base part can be modified to make it easier to use. I think it is better.

Once phalcon-devtools has been installed, you can copy a copy of the template to ~/Documents with the following command.

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

 

That's it.

If you found this article helpful , please give it a like!
0
Loading...
0 votes, average: 0.00 / 10
2,976
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

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 fortunate to be able to do a lot of other work, including marketing.
Furthermore, my portrait rights in Beyond are treated as CC0 by him.