phpcl / laminas-tools
Create modules, controllers, controller-plugins, view helpers and factories for Laminas/ZF3 projects
Requires
- php: >=7.1
README
Tools to facilitate rapid app development using Laminas MVC.
Installation
To get the latest version add the --dev
flag.
composer require --dev phpcl/laminas-tools
Otherwise, specify your preferred version in the composer.json
file:
{
"require" : {
"phpcl/laminas-tools" : "^1.0"
... other config not specified
}
... other config not specified
}
and then run composer install
.
Followup
Have a look at the vendor/bin
folder:
ls -l /path/to/project/vendor/bin
If you notice there are no links for phpcl-laminas-tools
, run the following command:
cd /path/to/project
php vendor/phpcl/laminas-tools/utils/create_links.php
Usage
From a command prompt or terminal window, change to the project root directory of your ZF 3 or Laminas MVC project.
Linux
vendor/bin/phpcl-laminas-tools WHAT PATH NAME
Windows
vendor/bin/phpcl-laminas-tools WHAT PATH NAME
Params
Examples
These examples assume you are running from a command prompt / terminal window, and have changed to the root directory of your project.
Creating a Module
As an example, to create a module "Test" on a Linux server:
vendor/bin/phpcl-laminas-tools module `pwd` Test
Here is what the tool does:
- Creates the module directory structure
- Creates a file
module/Test/src/Module.php
- Create a controller
module/Test/src/Controller/IndexController.php
- Creates a view template
/module/Test/view/test/index/index.phtml
- Creates a config file
module/Test/config/module.config.php
- Adds a route
/test[/:action]
(whereaction
is the name of any additionalxxxAction()
methods created in the controller) - Registers the controller with the framework
- Adds a route
Creating a Controller
As an example, to create a controller "Test\Controller\ListController" on a Windows server:
vendor/bin/phpcl-laminas-tools controller "C:\path\to\project" "Test\\Controller\\ListController"
Here is what the tool does:
- Creates a file
C:\path\to\project\module\Test\src\Controller\ListController.php
- Creates a view template
C:\path\to\project\module\Test\view\test\list\index.phtml
- Creates a config file
C:\path\to\project\module\Test\config\module.config.php
- Adds a route
/test-list[/:action]
(whereaction
is the name of any additionalxxxAction()
methods created in the new controller) - Registers the new controller with the framework
- Adds a route
Creating a Factory
As an example, to create a factory "Test\Factory\ListServiceFactory" on Linux:
vendor/bin/phpcl-laminas-tools factory `pwd` "Test\\Factory\\ListServiceFactory"
The tool will then directly output the code for a generic factory named ListServiceFactory
. If you wish to pipe the output into a file, do this:
mkdir module/Test/src/Factory
vendor/bin/phpcl-laminas-tools.sh factory `pwd` "Test\\Factory\\ListServiceFactory" >module/Test/src/Factory/ListServiceFactory.php
Creating a Controller Plugin
As an example, to create a controller plugin "Test\Controller\Plugin\ReallyCoolPlugin" on Linux:
vendor/bin/phpcl-laminas-tools controller-plugin `pwd` "Test\\Controller\\Plugin\\ReallyCoolPlugin"
Here is what the tool does:
- Creates a file
path\to\project\module\Test\src\Controller\Plugin\ReallyCoolPlugin.php
- Adds to the module config file
\path\to\project\module\Test\config\module.config.php
- Registers the new controller plugin with the framework under the
controller_plugins => factories
key - Adds an alias
reallyCool
under thecontroller_plugins => aliases
key
- Registers the new controller plugin with the framework under the
Creating a View Helper
As an example, to create a view helper "Test\View\Helper\ReallyCoolHelper" on Linux:
vendor/bin/phpcl-laminas-tools view-helper `pwd` "Test\\View\\Helper\\ReallyCoolHelper"
Here is what the tool does:
- Creates a file
path\to\project\module\Test\src\View\Helper\ReallyCoolHelper.php
- Adds to the module config file
\path\to\project\module\Test\config\module.config.php
- Registers the new view helper with the framework under the
view_helpers => factories
key - Adds an alias
reallyCool
under theview_helpers => aliases
key
- Registers the new view helper with the framework under the
Routes
When you create a new module:
- This route is defined for you:
/module
wheremodule
is the lower case name of the new module.- Example: you add a new module
Test
. The new route will be/test
.
- Example: you add a new module
- Any action methods added to the default controller
MODULE\Controller\IndexController
can be referenced using the name of the action method minus the suffixAction
, all lowercase.- Example: you add a method
public function demoAction() {}
toIndexController
in theTest
module. The new route will be:/test/demo
. When you create a new controller:
- Example: you add a method
- This route is defined for you:
/MODULE-CTRL_SHORT
whereMODULE
is the lower case name of the new module andCTRL_SHORT
is the "short" name of the controller (class name minus the suffixController
, all lower case)- Example: you add a new controller
DemoController
to theTest
module. The new route will be:/test-demo
.
- Example: you add a new controller
- Any action methods added to the new controller can be referenced using the name of the action method minus the suffix
Action
, all lowercase.- Example: you add a method
public function whateverAction() {}
toDemoController
. The new route will be:/test-demo/whatever
.
- Example: you add a method
IMPORTANT
- If you wish to generate a factory for a specific class, use the already-existing Laminas CLI tool
vendor/bin/generate-factory-for-class
instead. - The PHP-CL Laminas Tools can be used to create a factory if the
generate-factory-for-class
command fails, or if the factory class you wish to create does not have resolvable type-hints. - If you prefer, you can also simply download the file
laminas-tools.phar
- Usage is the same: follow the examples above, but substitute
php laminas-tools.phar
in place ofvendor/bin/phpcl-laminas-tools
- Usage is the same: follow the examples above, but substitute