Redirect after saving an entry The question was already asked (Possible to redirect elsewhere after saving an entry?) but this was about Craft 2.x.

Up until now I used the following code in my plugin (I took it from one of the comments from the same question):

Craft::$app-&gt;getView()-&gt;hook('cp.entries.edit', function (array &amp;$context) {
    if (isset ($context['section']['type']) &amp;&amp; $context['section']['type'] === 'single')
    {
        Craft::$app-&gt;getView()-&gt;registerJs("
            $(function () {
                $('input[name=\"redirect\"]').val('/');
            });
        ");
    }
});


The code did the job, but after I updated to 3.1.32.1, it stopped working, every time after entry was saved the following exception was thrown:


  HTTP 400 – Bad Request – yii\web\BadRequestHttpException
  Request contained an invalid body param


Stacktrace:

yii\web\BadRequestHttpException: Request contained an invalid body param in .../vendor/craftcms/cms/src/web/Request.php:651
Stack trace:
#0 .../vendor/craftcms/cms/src/web/Controller.php(314): craft\web\Request-&gt;getValidatedBodyParam('redirect')
#1 .../vendor/craftcms/cms/src/controllers/EntriesController.php(595): craft\web\Controller-&gt;redirectToPostedUrl(Object(craft\elements\Entry))
#2 [internal function]: craft\controllers\EntriesController-&gt;actionSaveEntry()
#3 .../vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#4 .../vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction-&gt;runWithParams(Array)
#5 .../vendor/craftcms/cms/src/web/Controller.php(109): yii\base\Controller-&gt;runAction('save-entry', Array)
#6 .../vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller-&gt;runAction('save-entry', Array)
#7 .../vendor/craftcms/cms/src/web/Application.php(297): yii\base\Module-&gt;runAction('entries/save-en...', Array)
#8 .../vendor/craftcms/cms/src/web/Application.php(565): craft\web\Application-&gt;runAction('entries/save-en...', Array)
#9 .../vendor/craftcms/cms/src/web/Application.php(281): craft\web\Application-&gt;_processActionRequest(Object(craft\web\Request))
#10 .../vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application-&gt;handleRequest(Object(craft\web\Request))
#11 .../web/index.php(21): yii\base\Application-&gt;run()
#12 {main}


Is there any chance to make it work again on 3.1.32?
The redirect url always needs to be hashed:

https://docs.craftcms.com/v3/dev/functions.html#redirectinput

In Twig you can use {{ redirectInput(url) }} which is a shortcut of

&lt;input type="hidden" name="redirect" value="{{ url|hash }}"&gt;


But if you need the same in a PHP code you can use

Craft::$app-&gt;getSecurity()-&gt;hashData($url)


based on the code how the Twig filter defined in craftcms/cms/src/web/twig/Extension.php.