AI Reading
Quick summary of this article
In Zend Framework 3, the view layer uses .phtml template files to render HTML output, with each module having its own view folder organized by controller and action names. Data is passed from controllers to views using the ViewModel object, and layouts serve as master templates that wrap around individual views. The framework supports reusable partials for common UI elements and allows dynamic layout changes for different controller actions.
- Views are stored in the view/ folder of each module, organized by namespace, controller name, and action name (e.g., view/application/index/index.phtml)
- Data is passed from controllers to views using ViewModel with an array of key-value pairs, accessed in templates via $this->key
- The default layout is located at module/Application/view/layout/layout.phtml and contains the common HTML structure with $this->content for the main view
- Partials are reusable view snippets that can be included using $this->partial('path/to/partial', ['data' => $data])
- Best practices include keeping views simple with minimal logic, using layouts for common structure, and passing only necessary data from controllers
Chapter 4: Views & Templates in Zend Framework 3
✅ Introduction to Views
In Zend Framework 3, the view layer is responsible for rendering the final HTML output. Views are written in .phtml files (PHP-based templates) and are located inside the view/ folder of a module.
- Each module can have its own
viewfolder. - Views are mapped based on the controller and action being called.
- Zend uses the
PhpRendererby default for rendering templates.
✅ View File Structure
Inside the view/ folder of a module, templates are organized by namespace (controller) and action:
/module
/Application
/view
/application
/index
index.phtml
application→ matches the controller namespace (Application\Controller).index→ matches the controller name (IndexController).index.phtml→ matches the action method (indexAction()).
✅ Creating a Simple View
Suppose you have an IndexController with an indexAction. The corresponding view file will be:
// File: module/Application/src/Controller/IndexController.php
<?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function indexAction()
{
return new ViewModel([
'message' => 'Welcome to Zend Framework 3!',
]);
}
}
// File: module/Application/view/application/index/index.phtml
<h1><?= $this->message; ?></h1>
✅ Passing Variables to Views
You can pass data from a controller to a view using ViewModel:
return new ViewModel([
'username' => 'John Doe',
'email' => 'john@example.com',
]);
// index.phtml
<p>User: <?= $this->username; ?></p>
<p>Email: <?= $this->email; ?></p>
✅ Layouts
A layout is a master template that wraps around your views. It usually contains the common HTML structure like header, footer, and navigation.
- Default layout is stored in:
module/Application/view/layout/layout.phtml - You can define multiple layouts for different modules.
// layout.phtml
<!DOCTYPE html>
<html>
<head>
<title>My Zend App</title>
</head>
<body>
<header><h1>Site Header</h1></header>
<main>
<?= $this->content; ?>
</main>
<footer><p>Copyright 2025</p></footer>
</body>
</html>
✅ Changing Layout Dynamically
You can change the layout for a controller action:
$this->layout('layout/custom');
This will load:
module/Application/view/layout/custom.phtml
✅ Using Partials
Partials are reusable view snippets. Example: a sidebar, navigation, or footer.
// Inside a view file
<?= $this->partial('application/partial/sidebar', [
'categories' => ['PHP', 'Zend', 'Laminas']
]); ?>
// File: module/Application/view/application/partial/sidebar.phtml
<ul>
<?php foreach ($this->categories as $cat): ?>
<li><?= $cat; ?></li>
<?php endforeach; ?>
</ul>
✅ Best Practices for Views
- Keep views simple (avoid complex logic inside
.phtml). - Use layouts for common page structure.
- Use partials for reusable UI blocks.
- Pass only necessary data from controllers to views.
✅ Exercises
- Create a new layout
blog-layout.phtmland apply it to your Blog module. - Create a partial view for navigation and include it in your layout.
- Pass an array of posts to a view and display them in an HTML list.
