markorstc/laminas-typedview

Strongly typed view layer, as extension for Laminas MVC system, focused on easy code navigation and type hinting

1.0.0 2021-07-10 20:58 UTC

This package is auto-updated.

Last update: 2024-05-11 16:12:59 UTC


README

Key features

  • strongly typed view layer
  • clean template scope
  • easy code navigation
  • type hinting

Installation

Install module via Composer

$ composer require markorstc/laminas-typedview

Requirements

PHP 8 or above

Enable module via config

Enable.module.mp4

Getting started

Create view model and template

You create a typed view model by extending abstract class TypedView\Entity\ViewModel. Then you specify path to your .phtml template file in TypedView\Entity\ViewModel::getTemplate() method.

Create.view.model.mp4

Use model in controller

To render your model, simply return it in Laminas controller action as you do it with standard Laminas\View\Model\ViewModel.

Use.model.in.controller.mp4

If you want to use common layout for multiple controllers or actions, you have to implement TypedView\Controller\LayoutModelAware interface in your controller.

At the same time, you specify callback, that will set rendered child model to the parent model (in this case rendered MyListViewModel to parent MyLayoutModel).

Use.root.layout.model.mp4

Use Laminas view helpers

It's possible to use Laminas standard view helpers, but you have to extend TypedView\Entity\LaminasBridgeModel when creating your typed view model.

If you want to use single particular Laminas view helper and keep clean scope in your template, you can use helper traits e.g. TypedView\Helper\UrlViewHelper.

Laminas.view.helpers.mp4

Nesting view models

It's possible to use view models as individual components and combine them.

To add one model to another use TypedView\Entity\ViewModel::addChild() method and specify capture callback the same way like in the layout model example.

View.models.nesting.mp4