0

in zend framework you can easily return $this->notFoundAction(); to return 404 (not found). The 'not_found_template' application config key is used to render the content.

We want to do the same but with a different status code 410 (gone). I cant figure out how to do this. I tried to return my own response but i cant set my view template.

What would be the prefered way to do this?

Stillmatic1985
  • 1,792
  • 6
  • 20
  • 39
  • and `$this->getResponse()->setStatusCode(410);` does not work? – Roland Starke Sep 03 '19 at 12:44
  • may be this would help you [Link Here](https://stackoverflow.com/questions/13807821/zf2-how-to-change-the-error-404-response-page-not-just-template-but-to-set-a) – Dhaval Purohit Sep 03 '19 at 12:46
  • Possible duplicate of [ZF2 - How to change the error/404 response page? Not just template but to set a new ViewModel](https://stackoverflow.com/questions/13807821/zf2-how-to-change-the-error-404-response-page-not-just-template-but-to-set-a) – Dhaval Purohit Sep 03 '19 at 12:48
  • I saw this thread. Problem is, how can i trigger this event from controller action. I would like to have something like `$this->notFoundAction();` => `$this->goneAction();` – Stillmatic1985 Sep 03 '19 at 12:59

1 Answers1

0

I created an Controller Plugin for this.

<?php

namespace Application\Controller\Plugin;

use Zend\Http\Response;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\View\Model\ViewModel;
use Zend\View\Renderer\PhpRenderer;

/**
 * Class GoneAction
 *
 * @package Application\Controller\Plugin
 */
class GoneAction extends AbstractPlugin
{
    /** @var PhpRenderer $phpRenderer */
    private $phpRenderer;

    /**
     * GoneAction constructor.
     *
     * @param PhpRenderer $phpRenderer
     */
    public function __construct(PhpRenderer $phpRenderer)
    {
        $this->phpRenderer = $phpRenderer;
    }

    /**
     * @return ViewModel
     */
    public function __invoke(): ViewModel
    {
        /** @var AbstractActionController $controller */
        $controller = $this->getController();

        /** @var Response $response */
        $response = $controller->getResponse();
        $response->setStatusCode(Response::STATUS_CODE_410);

        $viewModel = new ViewModel();
        $viewModel->setTemplate('layout/gone');
        return $viewModel;
    }
}
Stillmatic1985
  • 1,792
  • 6
  • 20
  • 39