How to use phalcon-devtools to accelerate Phalcon development

table of contents
- 1 Installing phalcon-devtools
- 2 What you can do with phalcon commands
- 3 phalcon info command
- 4 phalcon project command
- 5 phalcon module command
- 6 phalcon controller command
- 7 phalcon model command
- 8 phalcon all-models command
- 9 phalcon migration command
- 10 phalcon scaffold command
- 11 phalcon webtools commands
- 12 Included items
- 13 Template Modification
Hello.
I'm Mandai, the Wild team member in charge of development.
Phalcon is a rather unusual framework in that it loads phalcon.so into PHP itself, meaning the core of the framework doesn't exist as a PHP file.
This form is the reason why it's one of the fastest frameworks, but for someone accustomed to open source, the lack of a physical form initially made it seem difficult to get used to.
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.
I've summarized the installation method in a previous blog post, so please check that out.
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
- info (alias of: i)
- commands (alias of: list, enumerate)
- controller (alias of: create-controller)
- module (alias of: create-module)
- model (alias of: create-model)
- all-models (alias of: create-all-models)
- project (alias of: create-project)
- scaffold (alias of: create-scaffold)
- migration (alias of: create-migration)
- webtools (alias of: create-webtools)
Let's take a look at each one!
phalcon info command
This command displays the execution environment information for phalcon.
The results when I ran it on my system were 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
This command creates a new project.
When creating a new system, you'll 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
|
| --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
|
| --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 the database design for the application you're about to create is complete and the tables have been built in the database, you can quickly create models for all existing tables by typing the "phalcon all-models" command without any arguments.
It's basically 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
Running "phalcon migration generate" without arguments will use version number-based version control.
Using the --descr option with generate will switch to timestamp-based version control.
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, if you create migration A using 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 already applied, you will get the message "Info: Version 1.0.0 was already applied," which suggests that the system internally verifies that migration A and migration B are identical.
| --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
Additionally, screens with search, create, and update functions will be created.
Since each function itself is implemented on the controller, if you are creating a page that only performs simple CRUD operations, it is overwhelmingly faster to modify what has been created with 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 integrates into your project a layout using Twitter Bootstrap, allowing you to easily use Phalcon commands in your browser.
It can be disabled later, so you can enable it during development and disable it upon release.
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
There is a directory called "ide" directly under the phalcon-devtools directory.
It mainly contains tools to make it easier to implement Phalcon in an IDE.
This is a script for creating 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 the file and running it, a directory named after the Phalcon version will be created, and stubs for the current Phalcon environment will be generated.
However, since the latest version of the stubs is included, you don't really need to run it yourself.
Among them is a command-line tool that allows you to execute the phalcon command from the PhpStorm console.
It's basically a phalcon.sh script that doesn't perform an installation check, but it might be convenient to register.
This file contains stubs for functions implemented in the latest version of Phalcon.
If you're using the latest version, it's easier to use this instead of running gen-stubs.php.
Template Modification
I understand that the phalcon command can automatically generate projects, models, and controllers, but when creating several projects, you'll likely encounter the same parts that need to be modified each time.
The Phalcon command uses templates when automatically generating various things, so you can be freed from such tedious work by rewriting the templates!
Templates are used for projects, modules, and scaffolds, and the parts enclosed in "@@" are replaced, so it's a good idea to modify 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
0
