Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down
1 change: 1 addition & 0 deletions config/modules.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
'Zend\Router',
'Zend\Validator',
'Application',
'Example',
];
42 changes: 42 additions & 0 deletions module/Example/config/module.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Example;

use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;

return [
'router' => [
'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',
],
],
];
18 changes: 18 additions & 0 deletions module/Example/src/Controller/Factory/IndexControllerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Example\Controller\Factory;

use Zend\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;

use Example\Controller\IndexController;

class IndexControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = NULL)
{
$exampleService = $container->get('Example\Service\ExampleService');

return new IndexController($exampleService);
}
}
27 changes: 27 additions & 0 deletions module/Example/src/Controller/IndexController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Example\Controller;

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()
{
$view = new ViewModel();

$view->something = $this->exampleService->returnSomething();

return $view;
}
}
11 changes: 11 additions & 0 deletions module/Example/src/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Example;

class Module
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
}
11 changes: 11 additions & 0 deletions module/Example/src/Service/ExampleService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Example\Service;

class ExampleService
{
public function returnSomething()
{
return 'something';
}
}
36 changes: 36 additions & 0 deletions module/Example/test/Controller/IndexControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace ExampleTest\Controller;

use Example\Controller\IndexController;
use Zend\Stdlib\ArrayUtils;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;

class IndexControllerTest extends AbstractHttpControllerTestCase
{
public function setUp()
{
// The module configuration should still be applicable for tests.
// You can override configuration here with test case specific values,
// such as sample view templates, path stacks, module_listener_options,
// etc.
$configOverrides = [];

$this->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');
}
}
3 changes: 3 additions & 0 deletions module/Example/view/example/index/index.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>Example</p>

<?= $this->something ?>