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..0c109d1 --- /dev/null +++ b/module/Example/config/module.config.php @@ -0,0 +1,42 @@ + [ + 'routes' => [ + 'example' => [ + 'type' => Segment::class, + 'options' => [ + 'route' => '/example[/:action]', + 'defaults' => [ + 'controller' => Controller\IndexController::class, + 'action' => 'index', + ], + ], + ], + ], + ], + 'controllers' => [ + 'factories' => [ + Controller\IndexController::class => Controller\Factory\IndexControllerFactory::class, + ], + ], + 'service_manager' => [ + 'factories' => [ + Service\ExampleService::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/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 new file mode 100644 index 0000000..d4ef807 --- /dev/null +++ b/module/Example/src/Controller/IndexController.php @@ -0,0 +1,27 @@ +exampleService = $exampleService; + } + + public function indexAction() + { + $view = new ViewModel(); + + $view->something = $this->exampleService->returnSomething(); + + return $view; + } +} diff --git a/module/Example/src/Module.php b/module/Example/src/Module.php new file mode 100644 index 0000000..f5bab3f --- /dev/null +++ b/module/Example/src/Module.php @@ -0,0 +1,11 @@ +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..c14cd65 --- /dev/null +++ b/module/Example/view/example/index/index.phtml @@ -0,0 +1,3 @@ +
Example
+ += $this->something ?>