Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Meet Laminas

Laminas is a set of PHP packages for building websites and the continuation of Zend Framework, one of the best-known frameworks for enterprise projects. In 2020 Zend Framework was handed over to the Linux Foundation and renamed Laminas; the code stayed the same, only the class names changed.

This course describes an application built on laminas-mvc version 3 and PHP 8.1 or newer.

How Laminas differs

Almost nothing in Laminas happens "by itself". The page address, the controller class, the way it is created — everything is written down in configuration files, plain PHP files that return an array. There is more code than in Laravel, but you can always see where things come from.

This is what a fragment of such a file looks like:

// module/Application/config/module.config.php
return [
    'view_manager' => [
        'doctype' => 'HTML5',
    ],
];

The view_manager key is the section with template settings, and doctype is one setting inside it. You will work with arrays like this in every lesson.

What MVC is

MVC is a way to split code into three parts:

PartWhat it doesWhere it lives
ModelWorks with data and the databasemodule/Blog/src/Model
ViewBuilds the HTML of a pagemodule/Blog/view
ControllerReceives the request and connects the other twomodule/Blog/src/Controller

How a request travels

  1. The browser requests an address, for example /blog.
  2. The router finds a route for that address in the configuration.
  3. The route names a controller and an action — a method of the controller.
  4. The action prepares data and returns it.
  5. A view script turns the data into HTML, and the layout adds the common page frame.

Laminas packages

Laminas consists of separate packages. Each one is installed with Composer when you need it:

PackagePurpose
laminas-mvcRoutes, controllers, view scripts
laminas-servicemanagerCreating the application's objects
laminas-dbDatabase access
laminas-form, laminas-inputfilterForms and validation
laminas-authenticationUser login
laminas-eventmanagerEvents

What you will build

Through the course you will build a blog: a list of posts, a post page, a form for adding posts, a login for the author and a small API. Each lesson adds one piece to it.

Common mistake

Old articles and forum answers contain class names that start with Zend\. That code will not work in a new project: replace Zend with Laminas in every name, for example Zend\Mvc\Controller\AbstractActionController becomes Laminas\Mvc\Controller\AbstractActionController.

Thanks for reading!

Donate to the site — we work hard for you.