A free and open-source book on ZF3 for beginners


17.11. Adding the Not Authorized Page

Next we will create the Not Authorized page (see figure 17.13) on which we will redirect the user when they are not allowed to access some web page.

Figure 17.13 Not Authorized page Figure 17.13 Not Authorized page

Add the following route to the module.config.php file of the User module:

return [
    'router' => [
        'routes' => [
            'not-authorized' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/not-authorized',
                    'defaults' => [
                        'controller' => Controller\AuthController::class,
                        'action'     => 'notAuthorized',
                    ],
                ],
            ],
        ],
    ],
];

Then add the notAuthorizedAction() method to the AuthController in the User module:

/**
 * Displays the "Not Authorized" page.
 */
public function notAuthorizedAction()
{
    $this->getResponse()->setStatusCode(403);
    
    return new ViewModel();
}

Finally, add the not-authorized.phtml view template file under the user/auth directory under the User module's view directory:

<?php
$this->headTitle("Not Authorized");
?>

<h1>Not Authorized</h1>

<div class="alert alert-warning">Sorry, you have no permission to see this page.</div>

Now you can see the Not Authorized page if you type the following URL into the address bar of the browser: "http://localhost/not-authorized".


Top