ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 帮助 <details> <summary>phalcon</summary> ``` $ phalcon Phalcon DevTools (4.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) ``` </details> <br /> ## 初始化项目 ``` phalcon create-project store ``` ## 控制器 <details> <summary>main.go</summary> ``` $ phalcon project --help Phalcon DevTools (4.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] ``` </details> <br /> ### 创建控制器 ``` $ phalcon create-controller --name test ``` 使用 ``` <?php declare(strict_types=1); class TestController extends \Phalcon\Mvc\Controller { public function indexAction() { } } ``` ## Model `$ phalcon model users` ``` CREATE TABLE `users` ( `id` int(10) UNSIGNED NOT NULL, `name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `password` char(60) NOT NULL, `active` tinyint(1) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `users` ADD PRIMARY KEY (`id`), ADD KEY `email` (`email`); ALTER TABLE `users` MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT; COMMIT; ``` <details> <summary>user.php</summary> ``` <?php use Phalcon\Validation; use Phalcon\Validation\Validator\Email as EmailValidator; class Users extends \Phalcon\Mvc\Model { /** * * @var integer */ public $id; /** * * @var string */ public $name; /** * * @var string */ public $email; /** * * @var string */ public $password; /** * * @var string */ public $active; /** * Validations and business logic * * @return boolean */ public function validation() { $validator = new Validation(); $validator->add( 'email', new EmailValidator( [ 'model' => $this, 'message' => 'Please enter a correct email address', ] ) ); return $this->validate($validator); } /** * Initialize method for model. */ public function initialize() { $this->setSchema("test"); $this->setSource("users"); } /** * Allows to query a set of records that match the specified conditions * * @param mixed $parameters * @return Users[]|Users|\Phalcon\Mvc\Model\ResultSetInterface */ public static function find($parameters = null): \Phalcon\Mvc\Model\ResultsetInterface { return parent::find($parameters); } /** * Allows to query the first record that match the specified conditions * * @param mixed $parameters * @return Users|\Phalcon\Mvc\Model\ResultInterface */ public static function findFirst($parameters = null) { return parent::findFirst($parameters); } } ``` </details> <br /> ## CURD `$ phalcon scaffold --table-name users` | File | 目的 | | --- | --- | | `app/controllers/UsersController.php` | 产品控制器 | | `app/models/Users.php` | Products 模型 | | `app/views/layout/users.phtml` | Controller layout for Users | | `app/views/products/search.phtml` | View for the action`search` | | `app/views/products/new.phtml` | View for the action`new` | | `app/views/products/edit.phtml` | View for the action`edit` |