From faaf5e1167efe2ffbf6942c44a777ce9019da7ad Mon Sep 17 00:00:00 2001 From: John Levermore Date: Mon, 2 Jul 2018 12:57:04 +0100 Subject: [PATCH 1/2] Add "Example" module, including a single "index" action and "index" template, accessed via the "/example/index" route. --- composer.json | 6 ++- config/modules.config.php | 1 + module/Example/config/module.config.php | 37 +++++++++++++++++++ .../src/Controller/IndexController.php | 14 +++++++ module/Example/src/Module.php | 11 ++++++ .../test/Controller/IndexControllerTest.php | 36 ++++++++++++++++++ module/Example/view/example/index/index.phtml | 1 + 7 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 module/Example/config/module.config.php create mode 100644 module/Example/src/Controller/IndexController.php create mode 100644 module/Example/src/Module.php create mode 100644 module/Example/test/Controller/IndexControllerTest.php create mode 100644 module/Example/view/example/index/index.phtml diff --git a/composer.json b/composer.json index a65fe86..9e2a50f 100644 --- a/composer.json +++ b/composer.json @@ -10,12 +10,14 @@ }, "autoload": { "psr-4": { - "Application\\": "module/Application/src/" + "Application\\": "module/Application/src/", + "Example\\": "module/Example/src/" } }, "autoload-dev": { "psr-4": { - "ApplicationTest\\": "module/Application/test/" + "ApplicationTest\\": "module/Application/test/", + "ExampleTest\\": "module/Example/test/" } }, "extra": [], diff --git a/config/modules.config.php b/config/modules.config.php index 81b5749..30eaeb2 100644 --- a/config/modules.config.php +++ b/config/modules.config.php @@ -9,4 +9,5 @@ 'Zend\Router', 'Zend\Validator', 'Application', + 'Example', ]; diff --git a/module/Example/config/module.config.php b/module/Example/config/module.config.php new file mode 100644 index 0000000..1b4754d --- /dev/null +++ b/module/Example/config/module.config.php @@ -0,0 +1,37 @@ + [ + 'routes' => [ + 'example' => [ + 'type' => Segment::class, + 'options' => [ + 'route' => '/example[/:action]', + 'defaults' => [ + 'controller' => Controller\IndexController::class, + 'action' => 'index', + ], + ], + ], + ], + ], + 'controllers' => [ + 'factories' => [ + Controller\IndexController::class => InvokableFactory::class, + ], + ], + 'view_manager' => [ + 'template_map' => [ + 'example/index/index' => __DIR__ . '/../view/example/index/index.phtml', + ], + 'template_path_stack' => [ + __DIR__ . '/../view', + ], + ], +]; diff --git a/module/Example/src/Controller/IndexController.php b/module/Example/src/Controller/IndexController.php new file mode 100644 index 0000000..31413eb --- /dev/null +++ b/module/Example/src/Controller/IndexController.php @@ -0,0 +1,14 @@ +setApplicationConfig(ArrayUtils::merge( + include __DIR__ . '/../../../../config/application.config.php', + $configOverrides + )); + + parent::setUp(); + } + + public function testIndexActionCanBeAccessed() + { + $this->dispatch('/', 'GET'); + $this->assertResponseStatusCode(200); + $this->assertModuleName('example'); + $this->assertControllerName(IndexController::class); // as specified in router's controller name alias + $this->assertControllerClass('IndexController'); + $this->assertMatchedRouteName('home'); + } +} diff --git a/module/Example/view/example/index/index.phtml b/module/Example/view/example/index/index.phtml new file mode 100644 index 0000000..3cf952a --- /dev/null +++ b/module/Example/view/example/index/index.phtml @@ -0,0 +1 @@ +

Example

From 78a1b0aa24c0c4556b760c4f14ab19210948dbff Mon Sep 17 00:00:00 2001 From: John Levermore Date: Sun, 8 Jul 2018 17:10:56 +0100 Subject: [PATCH 2/2] Add "ExampleService" service class, including configuration to inject service into IndexController. --- module/Example/config/module.config.php | 7 ++++++- .../Factory/IndexControllerFactory.php | 18 ++++++++++++++++++ .../Example/src/Controller/IndexController.php | 15 ++++++++++++++- module/Example/src/Service/ExampleService.php | 11 +++++++++++ module/Example/view/example/index/index.phtml | 2 ++ 5 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 module/Example/src/Controller/Factory/IndexControllerFactory.php create mode 100644 module/Example/src/Service/ExampleService.php diff --git a/module/Example/config/module.config.php b/module/Example/config/module.config.php index 1b4754d..0c109d1 100644 --- a/module/Example/config/module.config.php +++ b/module/Example/config/module.config.php @@ -23,7 +23,12 @@ ], 'controllers' => [ 'factories' => [ - Controller\IndexController::class => InvokableFactory::class, + Controller\IndexController::class => Controller\Factory\IndexControllerFactory::class, + ], + ], + 'service_manager' => [ + 'factories' => [ + Service\ExampleService::class => InvokableFactory::class, ], ], 'view_manager' => [ diff --git a/module/Example/src/Controller/Factory/IndexControllerFactory.php b/module/Example/src/Controller/Factory/IndexControllerFactory.php new file mode 100644 index 0000000..7d14620 --- /dev/null +++ b/module/Example/src/Controller/Factory/IndexControllerFactory.php @@ -0,0 +1,18 @@ +get('Example\Service\ExampleService'); + + return new IndexController($exampleService); + } +} diff --git a/module/Example/src/Controller/IndexController.php b/module/Example/src/Controller/IndexController.php index 31413eb..d4ef807 100644 --- a/module/Example/src/Controller/IndexController.php +++ b/module/Example/src/Controller/IndexController.php @@ -5,10 +5,23 @@ use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; +use Example\Service\ExampleService; + class IndexController extends AbstractActionController { + private $exampleService; + + public function __construct(ExampleService $exampleService) + { + $this->exampleService = $exampleService; + } + public function indexAction() { - return new ViewModel(); + $view = new ViewModel(); + + $view->something = $this->exampleService->returnSomething(); + + return $view; } } diff --git a/module/Example/src/Service/ExampleService.php b/module/Example/src/Service/ExampleService.php new file mode 100644 index 0000000..9e3bb38 --- /dev/null +++ b/module/Example/src/Service/ExampleService.php @@ -0,0 +1,11 @@ +Example

+ +something ?>