Phalcon Devtools¶
概览¶
这些工具帮助你生成骨架代码,维护数据库结构,并加速开发。应用的核心组件可以通过简单的命令生成,使你能够轻松使用 Phalcon 开发应用程序。
您可以从命令行(终端)或Web界面使用Phalcon开发工具。
安装¶
Phalcon Devtools 可以通过以下方式安装composer. 确保您已先安装它。
全局安装Phalcon Devtools
或仅在您的项目内安装测试您的安装,输入以下内容:phalcon
$ phalcon
Phalcon DevTools (5.0.0)
Available commands:
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)
serve (alias of: server)
console (alias of: shell, psysh)
devtools也可以从我们的GitHub上以phar下载的形式获取仓库.
使用¶
可用命令¶
键入以下命令可以获取Phalcon工具中可用命令的列表:phalcon commands
$ phalcon commands
Phalcon DevTools (5.0.0)
Available commands:
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)
serve (alias of: server)
console (alias of: shell, psysh)
生成项目骨架¶
您可以使用Phalcon工具为您的应用程序生成预定义的项目骨架,这些骨架基于Phalcon框架。默认情况下,项目骨架生成器将使用mod_rewrite为Apache进行配置。在您的Web服务器文档根目录下输入以下命令:
生成了以下推荐的项目结构:
您可以添加参数--help
获取某个脚本使用的帮助信息:
$ phalcon project --help
Phalcon DevTools (5.0.0)
Help:
Creates a project
Usage:
project [name] [type] [directory] [enable-webtools]
Arguments:
help Shows this help text
Example
phalcon project store simple
Options:
--name=s Name of the new project
--enable-webtools Determines if webtools should be enabled [optional]
--directory=s Base path on which project will be created [optional]
--type=s Type of the application to be generated (cli, micro, simple, modules)
--template-path=s Specify a template path [optional]
--template-engine=s Define the template engine, default phtml (phtml, volt) [optional]
--use-config-ini Use a ini file as configuration file [optional]
--trace Shows the trace of the framework in case of exception [optional]
--help Shows this help [optional]
从Web服务器访问该项目,您将看到:
生成控制器¶
命令create-controller
生成控制器骨架结构。重要的是,此命令需要在一个已经包含Phalcon项目的目录内执行。
脚本生成以下代码:
<?php
declare(strict_types=1);
class TestController extends \Phalcon\Mvc\Controller
{
public function indexAction()
{
}
}
准备数据库设置¶
当使用开发者工具生成项目时,可以在app/config/config.php
找到配置文件。为了生成模型或脚手架,您需要更改用于连接到数据库的设置。
修改您的config.php文件中的数据库部分:
<?php
/*
* Modified: prepend directory path of current file, because of this
* file own different ENV under between Apache and command line.
* NOTE: please remove this comment.
*/
defined('BASE_PATH') || define('BASE_PATH', getenv('BASE_PATH') ?: realpath(dirname(__FILE__) . '/../..'));
defined('APP_PATH') || define('APP_PATH', BASE_PATH . '/app');
return new \Phalcon\Config([
'database' => [
'adapter' => 'Mysql',
'options' => [
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'test',
'charset' => 'utf8',
],
],
'application' => [
'appDir' => APP_PATH . '/',
'controllersDir' => APP_PATH . '/controllers/',
'modelsDir' => APP_PATH . '/models/',
'migrationsDir' => APP_PATH . '/migrations/',
'viewsDir' => APP_PATH . '/views/',
'pluginsDir' => APP_PATH . '/plugins/',
'libraryDir' => APP_PATH . '/library/',
'cacheDir' => BASE_PATH . '/cache/',
'baseUri' => '/',
]
]);
生成模型¶
创建模型有多种方法。您可以从默认数据库连接创建所有模型或选择性创建一些模型。模型可以具有字段表示的公共属性,或者使用setter/getter。
Options:
--name=s Table name
--schema=s Name of the schema [optional]
--config=s Configuration file [optional]
--namespace=s Model's namespace [optional]
--get-set Attributes will be protected and have setters/getters [optional]
--extends=s Model extends the class name supplied [optional]
--excludefields=l Excludes fields defined in a comma-separated list [optional]
--doc Helps to improve code completion on IDEs [optional]
--directory=s Base path on which project is located [optional]
--output=s Folder where models are located [optional]
--force Rewrite the model [optional]
--camelize Properties is in camelCase [optional]
--trace Shows the trace of the framework in case of exception [optional]
--mapcolumn Get some code for map columns [optional]
--abstract Abstract Model [optional]
--annotate Annotate Attributes [optional]
--help Shows this help [optional]
为名为customers
的表生成模型的最简单方法是:
如果您的数据库如下所示:
create table customers
(
`cst_id` int(10) auto_increment primary key,
`cst_status_flag` tinyint(1) null,
`cst_name_last` varchar(100) null,
`cst_name_first` varchar(50) null
);
create index customers_cst_status_flag_index
on `customers` (`cst_status_flag`);
create index customers_cst_name_last_index
on `customers` (`cst_name_last`);
create index customers_cst_name_first_index
on `customers` (`cst_name_first`);
它将生成
<?php
/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Phalcon\Tests\Models;
use Phalcon\Mvc\Model;
use Phalcon\Filter\Validation;
use Phalcon\Filter\Validation\Validator\PresenceOf as EmailValidator;
/**
* @property int $cst_id
* @property int $cst_status_flag
* @property string $cst_name_last
* @property string $cst_name_first
* @property array $cst_data;
*/
class Customers extends Model
{
/**
* @var int
*/
public $cst_id;
/**
* @var int
*/
public $cst_status_flag;
/**
* @var string
*/
public $cst_name_last;
/**
* @var string
*/
public $cst_name_first;
public function initialize()
{
$this->setSource('customers');
}
/**
* @return bool
*/
public function validation()
{
$validator = new Validation();
$validator->add(
'cst_name_last',
new PresenceOf(
[
'model' => $this,
'message' => 'Please enter a valid last name',
]
)
);
return $this->validate($validator);
}
}
使用
构建一个CRUD¶
脚手架是快速生成应用程序主要部分的一种方法。如果您希望通过单一操作创建新资源的模型、视图和控制器,那么脚手架就是完成这项任务的工具。
一旦代码生成,就需要根据需求进行自定义。许多开发者完全避免使用脚手架,选择从头编写全部或大部分源代码。生成的代码可以用作了解框架如何工作的指南,或用于开发原型。下面的代码显示了一个基于表的脚手架customers
:
脚手架生成器将在您的应用程序中构建多个文件以及一些文件夹。以下是将生成的内容概述:
文件 | 目的 |
---|---|
app/controllers/CustomersController.php | Customers 控制器 |
app/models/Customers.php | Customers 模型 |
app/views/layout/customers.phtml | Users 的控制器布局 |
app/views/products/search.phtml | 动作的视图search |
app/views/products/new.phtml | 动作的视图new |
app/views/products/edit.phtml | 动作的视图edit |
浏览最近生成的控制器时,您将看到一个搜索表单和一个创建新产品的链接:
The create page
允许您通过在 Products 模型上应用验证来创建产品。Phalcon 将自动验证非空字段,如果其中任何一项是必需的,则会生成警告。
在执行搜索后,分页组件可用于显示分页结果。使用每个结果前的“编辑”或“删除”链接来执行此类操作。
工具的Web界面¶
此外,如果您更喜欢,也可以从Web界面使用Phalcon开发工具。请查看以下屏幕录像,了解其工作原理:
将工具与 PhpStorm IDE 集成¶
下面的屏幕录像展示了如何将开发工具与PhpStorm IDE集成。配置步骤可以轻松适应其他PHP IDE。
Phalcon 的 CodeLobster IDE 插件¶
Codelobster IDE 包括一个专用于 Phalcon 开发的插件