{% do redirect() %} stopped working after upgrading to OC 4.2.15 / Twig 3.24

Environment

  • October CMS: 4.2.15
  • Laravel: 12.56.0
  • PHP: 8.4.18
  • Twig: 3.24.0
  • Composer: 2.9.5

I’ve been using {% do redirect(‘/some-url’) %} in my CMS page templates without any issues for a long time. Since upgrading to OC 4.2.15 and Twig 3.24.0, I started getting a runtime error when redirecting. The redirect throws a “Twig\Error\RuntimeError” with the message “An exception has been thrown during the rendering of a template (”“) in …/page.htm”

I dug into the code with Claude and found that the redirect is working, but it seems that twig wraps all exceptions thrown during doDisplay() into a RuntimeError before they can bubble up. By the time CmsException::mask() in HasRenderers.php sees it, the original HttpResponseException is buried inside the RuntimeError as its previous exception, and Laravel never gets a chance to handle it as a redirect response.

The fix:

In modules/cms/classes/controller/HasRenderers.php, the renderPageContents() method needs to unwrap and re-throw HttpResponseException before it gets swallowed
before:

CmsException::mask($this->page, 400);
        $this->loader->setObject($this->page);
        $template = $this->twig->load($this->page->getFilePath());
        $pageContents = $template->render($this->vars);
        CmsException::unmask();
        return $pageContents;

After:

CmsException::mask($this->page, 400);
        $this->loader->setObject($this->page);
        $template = $this->twig->load($this->page->getFilePath());
        try {
            $pageContents = $template->render($this->vars);
        } catch (\Twig\Error\RuntimeError $e) {
            $prev = $e->getPrevious();
            if ($prev instanceof \Illuminate\Http\Exceptions\HttpResponseException) {
                CmsException::unmask();
                throw $prev;
            }
            throw $e;
        }
        CmsException::unmask();
        return $pageContents;

Applying this patch locally fixed the issue for me. Hope this helps someone else!

Thanks @Shaquil

Looks like this was a regression introduced by adding compatibility with nunomaduro/collision package

A fix has been included in v4.2.18

1 Like