PHP Laminas Basics
Introduction
Laminas is a modern, open-source PHP framework for building web applications and services. It's the successor to the popular Zend Framework, maintaining the same high-quality codebase while operating under an open governance model through the Linux Foundation.
In this tutorial, we'll explore the fundamentals of Laminas, understand its architecture, and create a simple application to demonstrate its capabilities. By the end, you'll have a solid foundation to start building robust PHP applications using the Laminas framework.
What is Laminas?
Laminas is a collection of PHP packages that together form a powerful framework for web application development. The project was created in 2019 when Zend Framework was transitioned to the Linux Foundation and rebranded. Laminas retains the enterprise-grade quality and features of Zend Framework while embracing a more community-driven approach.
Key features of Laminas include:
- Modular architecture: Use only what you need
- MVC implementation: Clean separation of concerns
- Service management: Dependency injection container
- Event-driven programming: Flexible application flow
- Database abstraction: Simple database interactions
- Authentication and authorization: Secure your applications
- Form handling: Validation and filtering
- REST API support: Build modern web services
Laminas Framework Structure
Laminas is composed of three main sub-projects:
- Laminas Components - Standalone libraries that can be used independently
- Laminas MVC - A full-stack MVC framework for web applications
- Mezzio (formerly Zend Expressive) - Middleware runtime for PSR-15 middleware applications
Let's visualize this structure:
Setting Up Your First Laminas Project
Let's start by creating a new Laminas MVC application. We'll use Composer, which is the standard PHP dependency manager.
Prerequisites
- PHP 7.4 or higher
- Composer installed
- Basic PHP knowledge
Creating a New Project
Open your terminal and run the following command:
composer create-project laminas/laminas-mvc-skeleton my-laminas-app
This command creates a new Laminas MVC application in the my-laminas-app
directory. After the installation completes, navigate to the project folder:
cd my-laminas-app
Project Structure
Here's what your newly created project structure looks like:
my-laminas-app/
├── config/ # Application configuration
├── data/ # Application data (logs, cache)
├── module/ # Application modules
│ └── Application/ # Default module
├── public/ # Public web directory
│ ├── css/
│ ├── img/
│ ├── js/
│ └── index.php # Application entry point
├── vendor/ # Dependencies (managed by Composer)
├── composer.json # Composer configuration
└── README.md # Project documentation
Running the Application
You can run the application using PHP's built-in web server:
php -S 0.0.0.0:8080 -t public
Now open your browser and navigate to http://localhost:8080
. You should see the default Laminas welcome page.
Understanding the MVC Pattern in Laminas
Laminas implements the Model-View-Controller (MVC) pattern, which separates your application into three main components:
- Models: Handle data and business logic
- Views: Present data to users
- Controllers: Process requests and coordinate between models and views
The Request Lifecycle
Let's see how a request flows through a Laminas application:
Creating a Simple Module
In Laminas, applications are divided into modules. Let's create a simple "Blog" module to understand this concept better.
1. Create Module Structure
First, create the module directory structure:
mkdir -p module/Blog/config
mkdir -p module/Blog/src/Controller
mkdir -p module/Blog/src/Model
mkdir -p module/Blog/view/blog/index
2. Create Module Class
Create a file module/Blog/src/Module.php
:
<?php
namespace Blog;
use Laminas\ModuleManager\Feature\ConfigProviderInterface;
class Module implements ConfigProviderInterface
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
}
3. Create Module Configuration
Create a file module/Blog/config/module.config.php
:
<?php
namespace Blog;
use Laminas\Router\Http\Segment;
use Laminas\ServiceManager\Factory\InvokableFactory;
return [
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
],
],
'router' => [
'routes' => [
'blog' => [
'type' => Segment::class,
'options' => [
'route' => '/blog[/:action[/:id]]',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
],
],
'view_manager' => [
'template_path_stack' => [
'blog' => __DIR__ . '/../view',
],
],
];