🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] # Phalcon开发者工具 这些工具是生成框架代码的有用脚本的集合。可以使用简单的命令生成应用程序的核心组件,从而可以使用Phalcon轻松开发应用程序。 >[danger] 如果您更喜欢使用Web版本而不是控制台,则此[博客文章](https://blog.phalconphp.com/post/dont-like-command-line-and-consoles-no-problem)提供了更多信息。 ## 下载 您可以从[Github](https://github.com/phalcon/phalcon-devtools)下载包含开发人员工具的跨平台软件包。 ## 安装 `Phalcon开发者工具的安装`章节有有关如何在不同平台上安装开发人员工具的详细说明。 ## 可用命令 您可以通过输入`phalcon commands`获取Phalcon工具中可用命令的列表 ```bash $ phalcon commands Phalcon DevTools (3.0.0) Available commands: 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) ``` ## 生成项目架构 您可以使用Phalcon工具为Phalcon框架的应用程序生成预定义的项目框架。默认情况下,项目框架生成器将使用`mod_rewrite`用于Apache。在Web服务器文档根目录中键入以下命令: ```bash $ pwd /Applications/MAMP/htdocs $ phalcon create-project store ``` 生成了以下推荐的项目结构: ![](https://docs.phalconphp.com/images/content/devtools-usage-01.png) 您可以添加参数`--help`以获取有关某个脚本用法的帮助: ```bash $ phalcon project --help Phalcon DevTools (3.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 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] --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 ``` 从Web服务器访问项目将显示: ![](https://docs.phalconphp.com/images/content/devtools-usage-02.png) ## 生成控制器 命令`create-controller` 生成控制器骨架结构。在已有Phalcon项目的目录中调用此命令很重要。 ```bash $ phalcon create-controller --name test ``` 脚本生成以下代码: ```php <?php use Phalcon\Mvc\Controller; class TestController extends Controller { public function indexAction() { } } ``` ## 数据库预设置 使用开发人员工具生成项目时。配置文件可以在`app/config/config.php` 中找到。要生成模型或脚手架,您需要更改用于连接数据库的设置。 更改`config.php`文件中的数据库部分: ```php <?php 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', 'host' => 'localhost', 'username' => 'root', 'password' => 'secret', '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/', // This allows the baseUri to be understand project paths that are not in the root directory // of the webpspace. This will break if the public/index.php entry point is moved or // possibly if the web server rewrite rules are changed. This can also be set to a static path. 'baseUri' => preg_replace('/public([\/\\\\])index.php$/', '', $_SERVER["PHP_SELF"]), ] ]); ``` <a name='generating-models'></a> ## 生成模型 有几种方法可以创建模型。您可以从默认数据库连接或某些选择性创建所有模型。模型可以具有字段表示的公共属性,也可以使用setter/getter。 ```bash Options: --name=s Table name --schema=s Name of the schema. [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 will be created [optional] --force Rewrite the model. [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] ``` 生成模型的最简单方法是: ```bash $ phalcon model products ``` ```bash $ phalcon model --name tablename ``` 所有表字段都声明为public,以便直接访问。 ```php <?php use Phalcon\Mvc\Model; class Products extends Model { /** * @var integer */ public $id; /** * @var integer */ public $typesId; /** * @var string */ public $name; /** * @var string */ public $price; /** * @var integer */ public $quantity; /** * @var string */ public $status; } ``` 通过添加 `--get-set` ,您可以生成带有受保护变量和公共setter/getter方法的字段。这些方法可以帮助setter/getter方法中的业务逻辑实现。 ```php <?php use Phalcon\Mvc\Model; class Products extends Model { /** * @var integer */ protected $id; /** * @var integer */ protected $typesId; /** * @var string */ protected $name; /** * @var string */ protected $price; /** * @var integer */ protected $quantity; /** * @var string */ protected $status; /** * Method to set the value of field id * * @param integer $id */ public function setId($id) { $this->id = $id; } /** * Method to set the value of field typesId * * @param integer $typesId */ public function setTypesId($typesId) { $this->typesId = $typesId; } // ... /** * Returns the value of field status * * @return string */ public function getStatus() { return $this->status; } } ``` 模型生成器的一个很好的特性是它保持开发人员在代码生成之间做出的更改。这允许添加或删除字段和属性,而不必担心丢失对模型本身所做的更改。以下截屏视频将向您展示它的工作原理: <div align="center"> <iframe src="https://player.vimeo.com/video/39213020" width="500" height="266" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> </div> ## 脚手架CRUD 脚手架是生成应用程序的一些主要部分的快速方法。如果要在单个操作中为新资源创建模型,视图和控制器,脚手架就是工作的工具。 生成代码后,必须对其进行自定义以满足您的需求。许多开发人员完全避免使用脚手架,选择从头开始编写所有或大部分源代码。生成的代码可以作为更好地理解框架如何工作或开发原型的指南。下面的代码显示了基于表`products`的脚手架: ```bash $ phalcon scaffold --table-name products ``` 脚手架生成器将在您的应用程序中构建多个文件以及一些文件夹。以下是将要生成的内容的快速概述: | 文件 | 目的 | | ---------------------------------------- | ------------------------------ | | `app/controllers/ProductsController.php` | Products 控制器 | | `app/models/Products.php` | Products 模型 | | `app/views/layout/products.phtml` | Products 控制器布局 | | `app/views/products/new.phtml` | `new`动作视图 | | `app/views/products/edit.phtml` | `edit`动作视图 | | `app/views/products/search.phtml` | `search`动作视图 | 浏览最近生成的控制器时,您将看到一个搜索表单和一个用于创建新产品的链接: ![](https://docs.phalconphp.com/images/content/devtools-usage-03.png) `创建页面`允许您创建在Products模型上应用验证的产品。如果需要任何字段,Phalcon将自动验证非空字段产生警告。 ![](https://docs.phalconphp.com/images/content/devtools-usage-04.png) 执行搜索后,可以使用寻呼机组件显示分页结果。使用每个结果前面的“编辑”或“删除”链接来执行此类操作。 ![](https://docs.phalconphp.com/images/content/devtools-usage-05.png) ## 工具的Web界面 此外,如果您愿意,可以从Web界面使用Phalcon Developer Tools。查看以下截屏视频,了解其工作原理: <div align="center"> <iframe src="https://player.vimeo.com/video/42367665" width="500" height="266" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen mark="crwd-mark"></iframe> </div> ## 将工具与PhpStorm IDE集成 下面的截屏视频演示了如何将开发人员工具与[PhpStorm IDE](http://www.jetbrains.com/phpstorm/)集成。配置步骤可以很容易地适应PHP的其他IDE。 <div align="center"> <iframe width="560" height="315" src="https://www.youtube.com/embed/UbUx_6Cs6r4" frameborder="0" allowfullscreen mark="crwd-mark"></iframe> </div> <a name='conclusion'></a> ## 结论 Phalcon开发人员工具提供了一种为应用程序生成代码的简便方法,减少了开发时间和潜在的编码错误。