divix1988 / laminas-cli-commands
Laminas项目的CLI命令
Requires
- php: ^7.1 || ^8.1
- laminas/laminas-cli: ^1.7
- laminas/laminas-code: ^4.7
README
CLI命令用于Laminas项目。 警告,仍在测试阶段。
官方网站: http://laminasstarterkit.omelak.com
安装
通过Composer
使用Composer安装库
$ composer require --dev divix1988/laminas-cli-commands
设置
将以下配置添加到您的config/local.php
文件中
'laminas-cli' => [ 'commands' => [ 'mvc:controller' => \Divix\Laminas\Cli\Command\ControllerCommand::class, 'mvc:rowset' => \Divix\Laminas\Cli\Command\RowsetCommand::class, 'mvc:model' => \Divix\Laminas\Cli\Command\ModelCommand::class, 'mvc:form' => \Divix\Laminas\Cli\Command\FormCommand::class, 'mvc:view' => \Divix\Laminas\Cli\Command\ViewCommand::class, 'mvc:crud' => \Divix\Laminas\Cli\Command\CrudCommand::class, 'mvc:crud_controller' => \Divix\Laminas\Cli\Command\CrudControllerCommand::class, 'mvc:crud_view' => \Divix\Laminas\Cli\Command\CrudViewCommand::class, 'mvc:crud_config' => \Divix\Laminas\Cli\Command\CrudConfigCommand::class, 'mvc:login_registration' => \Divix\Laminas\Cli\Command\LoginRegistrationCommand::class, 'mvc:admin' => \Divix\Laminas\Cli\Command\AdminPanelCommand::class, 'mvc:navigation' => \Divix\Laminas\Cli\Command\NavigationCommand::class, 'mvc:sitemap' => \Divix\Laminas\Cli\Command\SitemapCommand::class, 'mvc:mariadb_database_connect' => \Divix\Laminas\Cli\Command\MariaDbCommand::class ], ],
用法
$ vendor/bin/laminas [command-params] [command-name]
或者对于Windows用户
$ "vendor/bin/laminas.bat" [command-params] [command-name]
可用命令
控制器
生成带有可用动作列表的示例控制器
"vendor/bin/laminas.bat" mvc:controller --actions=<action1> --actions=<action2> --module=ModuleName <name>
新文件位于:[root]/module/[moduleName]/src/Controller/[name].php
示例输出
namespace ModuleName\Controller\ControllerNameController; class ControllerNameController extends \Laminas\Mvc\Controller\AbstractActionController { public function indexAction() { } public function action1Action() { } public function action2Action() { } }
模型
生成带有属性列表的示例模型
"vendor/bin/laminas.bat" mvc:model --properties=<property1> --properties=<property2> --module=ModuleName <name>
新文件位于:[root]/module/[moduleName]/src/Model/[name].php
示例输出
namespace ModuleName\Model; class ModelNameTable extends AbstractTable { protected $resultsPerPage = 10; public function getBy(array $params = []) { $select = $this->tableGateway->getSql()->select(); if (isset($params['id'])) { $select->where(['id' => $params['id']]); $params['limit'] = 1; } if (isset($params['property1'])) { $select->where(['property1' => $params['property1']]); } if (isset($params['property2'])) { $select->where(['property2' => $params['property2']]); } if (isset($params['limit'])) { $select->limit($params['limit']); } if (!isset($params['page'])) { $params['page'] = 0; } $result = (isset($params['limit']) && $params['limit'] == 1) ? $this->fetchRow($select) : $this->fetchAll($select, ['limit' => $this->resultsPerPage, 'page' => $params['page']]); return $result; } public function patch(int $id, array $data) { if (empty($data)) { throw new \Exception('missing data to update'); } $passedData = []; if (!empty($data['property1'])) { $passedData['property1'] = $data['property1']; } if (!empty($data['property2'])) { $passedData['property2'] = $data['property2']; } $this->tableGateway->update($passedData, ['id' => $id]); } public function getById(\Rowset\ModelName $rowset) { return parent::saveRow($rowset); } public function delete($id) { if (empty($id)) { throw new \Exception('missing comics id to delete'); } parent::deleteRow($id); } }
表单
生成带有属性列表的示例模型
"vendor/bin/laminas.bat" mvc:form --properties=<property1> --properties=<property2> --module=<ModuleName> <name>
新文件位于:[root]/module/[moduleName]/src/Form/[name]Form.php
示例输出
namespace ModuleName\Form; use \Laminas\Form\Element; class NewUserForm extends \Laminas\Form\Form implements \Laminas\InputFilter\InputFilterProviderInterface { const ELEMENT_PROPERTY1 = 'property1'; const ELEMENT_PROPERTY2 = 'property2'; public function __construct($name, array $params) { parent::__construct($name, $params); $this->setAttribute('class', 'styledForm'); $this->add([ 'name' => self::ELEMENT_PROPERTY1, 'type' => 'text', 'options' => [ 'label' => 'property1' ], 'attributes' => [ 'required' => true ] ]); $this->add([ 'name' => self::ELEMENT_PROPERTY2, 'type' => 'text', 'options' => [ 'label' => 'property2' ], 'attributes' => [ 'required' => true ] ]); $this->add([ 'name' => 'submit', 'type' => 'submit', 'attributes' => [ 'value' => 'Submit', 'class' => 'btn btn-primary' ] ]); } public function getInputFilterSpecification() { return []; } }
行集
生成带有参数列表的示例行集
"vendor/bin/laminas.bat" mvc:rowset --properties=<property1> --properties=<property2> --module=ModuleName <name>
新文件位于:[root]/module/[moduleName]/src/Model/Rowset/[name].php
示例输出
namespace ModuleName\Model; class RowsetName { public $property1 = null; public $property2 = null; public function getProperty1() { return $this->property1; } public function setProperty1($value) { $this->property1 = $value; return $this; } public function getProperty2() { return $this->property2; } public function setProperty2($value) { $this->property2 = $value; return $this; } }
视图
生成示例.phtml视图文件
"vendor/bin/laminas-cli.bat" mvc:view --module=ModuleName <controllerName> <name>
新文件位于:[root]/module/[moduleName]/view/[controllerName]/[name].phtml
示例输出
<p>ModuleName - ViewName</p>
CRUD
生成带有表单、行集、模型、视图和控制器的完整工作示例
"vendor/bin/laminas.bat" mvc:crud --properties=<property1> --properties=<property2> --module=ModuleName <name>
新文件位于
[root]/module/[moduleName]/src/Controller/[name]Controller.php
[root]/module/[moduleName]/src/Model/[name]Model.php
[root]/module/[moduleName]/src/Model/AbstractTable.php
[root]/module/[moduleName]/src/Form/[name]Form.php
[root]/module/[moduleName]/src/Model/Rowset/[name].php
[root]/module/[moduleName]/src/Model/Rowset/AbstractModel.php
[root]/module/[moduleName]/view/[name]/index.phtml
[root]/module/[moduleName]/view/[name]/pagination.phtml
[root]/module/[moduleName]/view/[name]/add.phtml
[root]/module/[moduleName]/view/[name]/delete.phtml
[root]/module/[moduleName]/view/[name]/edit.phtml
[root]/module/[moduleName]/config/generated.crud.php
配置在:config/generated.crud.php
模块:ModuleName
和名称:NewUsers
的示例输出
namespace ModuleName; use Laminas\Router\Http\Segment; use Laminas\Db\ResultSet\ResultSet; use Laminas\Db\TableGateway\TableGateway; use ModuleName\Model\Rowset; use ModuleName\Model; use ModuleName\Controller; return [ 'router' => [ 'routes' => [ 'newUsers' => [ 'type' => Segment::class, 'options' => [ 'route' => '/newusers[/:action[/:id]]', 'defaults' => [ 'controller' => Controller\NewUsersController::class, 'action' => 'index', ], ], 'may_terminate' => true, 'child_routes' => [ 'paginator' => [ 'type' => 'segment', 'options' => [ 'route' => '/[page/:page]', 'defaults' => [ 'page' => 1 ] ] ], ] ] ] ], 'controllers' => [ 'factories' => [ Controller\NewUsersController::class => function($sm) { $postService = $sm->get(Model\NewUsersTable::class); return new Controller\NewUsersController($postService); }, ] ], 'service_manager' => [ 'factories' => [ 'NewUsersTableGateway' => function ($sm) { $dbAdapter = $sm->get('Laminas\Db\Adapter\Adapter'); $config = $sm->get('Config'); $baseUrl = $config['view_manager']['base_url']; $resultSetPrototype = new ResultSet(); $identity = new Rowset\NewUser($baseUrl); $resultSetPrototype->setArrayObjectPrototype($identity); return new TableGateway('newusers', $dbAdapter, null, $resultSetPrototype); }, Model\NewUsersTable::class => function($sm) { $tableGateway = $sm->get('NewUsersTableGateway'); $table = new Model\NewUsersTable($tableGateway); return $table; }, ] ], 'view_manager' => [ 'template_map' => [ 'module-name/new-users/index' => __DIR__ . '/../view/NewUsers/index.phtml', 'module-name/new-users/edit' => __DIR__ . '/../view/NewUsers/edit.phtml', 'module-name/new-users/add' => __DIR__ . '/../view/NewUsers/add.phtml', 'module-name/new-users/pagination' => __DIR__ . '/../view/NewUsers/pagination.phtml', ], ] ];
用户登录与注册
生成带有自定义用户属性的注册和登录功能
"vendor/bin/laminas.bat" mvc:login-registration --properties=<property1> --properties=<property2> --module=ModuleName
新文件位于
[root]/module/[moduleName]/src/Controller/LoginController.php
[root]/module/[moduleName]/src/Controller/RegisterController.php
[root]/module/[moduleName]/src/Model/UserModel.php
配置在:config/module.config.php
src/Module.php
管理面板
将管理面板添加到您的Laminas MVC项目,并提供报告和用户管理工具。
"vendor/bin/laminas.bat" mvc:admin --module=ModuleName
新文件位于
[root]/module/[moduleName]/src/Controller/AbstractController.php
[root]/module/[moduleName]/src/Controller/AdminController.php
[root]/module/[moduleName]/src/view/admin/admin/index.phtml
[root]/module/[moduleName]/src/view/admin/_shared/footer.phtml
[root]/module/[moduleName]/src/view/admin/_shared/menu.phtml
[root]/module/[moduleName]/src/view/layout/admin.phtml
[root]/module/[moduleName]/src/Model.php
配置在:config/module.config.php
src/Module.php
导航
添加默认菜单导航,指定给定的项目。
"vendor/bin/laminas.bat" mvc:navigation --items=<item1> --items=<item2> --module=ModuleName <name>
新文件位于
[root]/module/[moduleName]/src/Controller/AbstractController.php
[root]/module/[moduleName]/src/Controller/AdminController.php
[root]/module/[moduleName]/src/view/[moduleName]/layout/layout.phtml
[root]/module/[moduleName]/src/view/_shared/menu.phtml
配置在:config/autoload/global.php
网站地图
添加提供XML格式文件的网站地图控制器。
"vendor/bin/laminas.bat" mvc:sitemap --module=ModuleName <name>
新文件位于
[root]/module/[moduleName]/src/Controller/SitemapController.php
[root]/module/[moduleName]/src/view/[moduleName]/sitemap/index.phtml
配置在:[root]/module/[moduleName]/config/module.config.php