Advertisement
Google Ad Slot: content-top
Zend Views
What is a View in Zend?
A View is a .phtml file that handles how data is presented to the user. It's part of the MVC architecture (Model-View-Controller).
- Controller sends data
- View renders it as HTML
View File Structure
Typically, views are stored in:
module/ └── Application/ └── view/ └── application/ └── controller-name/ └── action-name.phtml
🔹 Example: View for AboutController@indexAction
/module/Application/view/application/about/index.phtml
Displaying Variables
From Controller:
return new ViewModel([ 'title' => 'About Us', 'year' => 2025 ]);
In .phtml file:
<h1><?= $this->title ?></h1> <p>Copyright © <?= $this->year ?></p>
Looping in Views
<ul> <?php foreach ($this->users as $user): ?> <li><?= $user['name'] ?> - <?= $user['email'] ?></li> <?php endforeach; ?> </ul>
Zend View Helpers
View Helpers are mini-classes that encapsulate common logic for use in views.
- Accessed with
$this->helperName()inside a.phtmlfile. - No need to write raw logic again and again.
- They keep view files clean, modular, and secure.
1. doctype() & headTitle()
Code:
<?= $this->doctype('HTML5') ?>
<?= $this->headTitle('My Page Title') ?>
Output in Browser:
<!DOCTYPE html> <title>My Page Title</title>
2. headMeta()
Code:
<?= $this->headMeta()->appendName('viewport', 'width=device-width, initial-scale=1') ?>
Output:
<meta name="viewport" content="width=device-width, initial-scale=1">
3. headLink() for Stylesheets
Code:
<?= $this->headLink()->appendStylesheet($this->basePath('css/style.css')) ?>
Output:
<link href="/css/style.css" rel="stylesheet" type="text/css">
4. headScript() for JS in <head>
Code:
<?= $this->headScript()->appendFile($this->basePath('js/head.js')) ?>
Output:
<script src="/js/head.js" type="text/javascript"></script>
5. inlineScript() for JS in <body> end
Code:
<?= $this->inlineScript()->appendFile($this->basePath('js/footer.js')) ?>
Output:
<script src="/js/footer.js" type="text/javascript"></script>
6. escapeHtml()
Code:
<?php $x = "<b>Hello</b>"; ?> <p>Escaped: <?= $this->escapeHtml($x) ?></p>
Output:
<p>Escaped: <b>Hello</b></p>
7. basePath() for Static Assets
Code:
<img src="<?= $this->basePath('images/logo.png') ?>">
Output:
<img src="/images/logo.png">
8. url() – Route Generator
Assume Route Name = home
Code:
<a href="<?= $this->url('home') ?>">Home</a>
Output:
<a href="/">Home</a>
9. form() Full HTML Demo
PHP Controller
$form = new Laminas\Form\Form(); $form->add(['name' => 'email', 'type' => 'Email']); $form->add(['name' => 'submit', 'type' => 'Submit', 'attributes' => ['value' => 'Send']]);
View Code:
<?= $this->form()->openTag($form) ?>
<?= $this->formElement($form->get('email')) ?>
<?= $this->formElement($form->get('submit')) ?>
<?= $this->form()->closeTag() ?>
Output:
<form method="post" action=""> <input type="email" name="email"> <input type="submit" name="submit" value="Send"> </form>
10. partial() – Include Template
header.phtml
<h2><?= $this->escapeHtml($title) ?></h2>
Main View:
<?= $this->partial('partial/header.phtml', ['title' => 'Hello']) ?>
Output:
<h2>Hello</h2>
11. partialLoop() – Render List of Data
user-row.phtml
<li><?= $this->name ?> - <?= $this->email ?></li>
Main View:
<ul>
<?= $this->partialLoop('partial/user-row.phtml', [
['name' => 'Sam', 'email' => 'sam@example.com'],
['name' => 'Raj', 'email' => 'raj@example.com']
]) ?>
</ul>
Output:
<ul> <li>Sam - sam@example.com</li> <li>Raj - raj@example.com</li> </ul>