Extbase未发现异常,重定向到404默认页面



我有一个扩展,当我用一个不存在或不再存在的记录ID调用这个扩展时,我得到一个异常Object of type MyModel... with identity "1035" not found.

页面本身的标头状态为500。

在这种情况下,我想做的是显示默认的404页面。这可能吗?我该怎么做呢?

我猜你是在使用像

这样的方法
public function detailAction(YourVendorExtDomainModelMyModel $model)

在这种情况下,这并不容易,因为异常已经在extbase的核心中抛出。你可以看看我是如何解决它为我的新闻扩展:

   /**
     * @param RequestInterface $request
     * @param ResponseInterface $response
     * @throws Exception
     */
    public function processRequest(RequestInterface $request, ResponseInterface $response)
    {
        try {
            parent::processRequest($request, $response);
        } catch (Exception $exception) {
            $this->handleKnownExceptionsElseThrowAgain($exception);
        }
    }
    /**
     * @param Exception $exception
     * @throws Exception
     */
    private function handleKnownExceptionsElseThrowAgain(Exception $exception)
    {
        $previousException = $exception->getPrevious();
        if (
            $this->actionMethodName === 'detailAction'
            && $previousException instanceof TYPO3CMSExtbasePropertyException
            && isset($this->settings['detail']['errorHandling'])
        ) {
            $this->handleNoNewsFoundError($this->settings['detail']['errorHandling']);
        } else {
            throw $exception;
        }
    }

在方法handleNoNewsFoundError中,您可以做任何您想做的事情,例如调用$GLOBALS['TSFE']->pageNotFoundAndExit('No news entry found.');

未经测试,但从逻辑POV你应该只是捕获异常,然后让你的控制器决定做什么(在你的情况下:显示404页)。

最新更新