Page Title in MVC in PHP
The template file also has access to the variable
$title, containing the page title.
Obviously, this header will also be
different for various pages. Let's use
this variable for its intended purpose:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><?= $title ?></title>
</head>
<body>
...
</body>
</html>
In order to set the title for a specific
view, it is necessary to write it
into the title property in the controller:
<?php
namespace Project\Controllers;
use Core\Controller;
class TestController extends Controller
{
public function act()
{
// Set the title:
$this->title = 'Act action of the test controller';
// Render the view, passing some data:
return $this->render('test/act', [
'var1' => 'eee',
'var2' => 'bbb',
'var3' => 'kkk',
]);
}
}
?>
Modify the template file and all your controllers so that each view displays its own title.
Let in the Page controller the following
array is given:
<?php
$this->pages = [
1 => ['title'=>'page 1', 'text'=>'text 1'],
2 => ['title'=>'page 2', 'text'=>'text 2'],
3 => ['title'=>'page 3', 'text'=>'text 3'],
];
?>
Create a show action that will
display the given page. Let in the view
the page text from the 'text' key be
wrapped in a paragraph, and the text from the 'title' key
becomes the page title.