Skip to content

Commit

Permalink
Updated as per review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Richer <[email protected]>
  • Loading branch information
visto9259 committed Mar 20, 2024
1 parent befad7a commit 0ae3ae5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 52 deletions.
62 changes: 11 additions & 51 deletions docs/book/getting-started/database-and-models.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,34 +220,14 @@ use Laminas\ServiceManager\Factory\InvokableFactory;

return [
'controllers' => [
'factories' => [
Controller\AlbumController::class => InvokableFactory::class,
],
// ...
],

// The following section is new and should be added to your file:
'router' => [
'routes' => [
'album' => [
'type' => Segment::class,
'options' => [
'route' => '/album[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\AlbumController::class,
'action' => 'index',
],
],
],
],
// ..
],
'view_manager' => [
'template_path_stack' => [
'album' => __DIR__ . '/../view',
],
// ...
],
'service_manager' => [
'factories' => [
Expand Down Expand Up @@ -397,47 +377,27 @@ class AlbumController extends AbstractActionController
</code></pre>
<!-- markdownlint-enable MD033 -->

Our controller now depends on `AlbumTable`, so we will need to create a factory
for the controller that will inject the `AlbumTable`.

Let's create the `AlbumControllerFactory.php` in `module/Album/src/Controller`:

````php
namespace Album\Controller;

use Album\Model\AlbumTable;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Psr\Container\ContainerInterface;

class AlbumControllerFactory implements FactoryInterface
{
Our controller now depends on `AlbumTable`, so we will need to update the factory
for the controller so that it will inject the `AlbumTable`.

/**
* @inheritDoc
*/
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): AlbumController
{
return new AlbumController(
$container->get(AlbumTable::class)
);
}
}
````
We will use the `ReflectionBasedAbstractFactory` factory to build the `AlbumController`.
`ReflectionBasedAbstractFactory` provides a reflection-based approach to instantiation, resolving constructor dependencies to the relevant services. Since the `AlbumController` constructor has an `AlbumTable` parameter, the factory will instantiate an `AlbumTable` instance and pass it to the `AlbumController`constructor.

Then we can modify the `controllers` section of the `module.config.php` to use the new factory:
Then we can modify the `controllers` section of the `module.config.php` to
use `ReflectionBasedAbstractFactory`:

<!-- markdownlint-disable MD033 -->
<pre class="language-php" data-line="3,10"><code>
namespace Album;

use Album\Controller\AlbumControllerFactory;
use Laminas\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory;
use Album\Model\AlbumTableFactory;
use Laminas\Router\Http\Segment;

return [
'controllers' => [
'factories' => [
Controller\AlbumController::class => AlbumControllerFactory::class
Controller\AlbumController::class => ReflectionBasedAbstractFactory::class
],
],

Expand Down
2 changes: 1 addition & 1 deletion docs/book/getting-started/routing-and-controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ URL | Method called
`http://localhost:8080/album/edit` | `Album\Controller\AlbumController::editAction`
`http://localhost:8080/album/delete` | `Album\Controller\AlbumController::deleteAction`

**Note:** If you are using self-hosted Apache, replace `http://localhost:8080/` by `http://laminas-mvc-tutorial.localhost/`
NOTE: If you are using self-hosted Apache, replace `http://localhost:8080/` by `http://laminas-mvc-tutorial.localhost/`

We now have a working router and the actions are set up for each page of our
application.
Expand Down

0 comments on commit 0ae3ae5

Please sign in to comment.