How to redirect with error messages? If validation fails in plugin, how can I redirect it bank to previous URL with errors?

craft()-&gt;urlManager-&gt;setRouteVariables(array(
    'errors'    =&gt; $errors
));


gives 404

My Controller:



class DB_LogsController extends BaseController
{
    protected $allowAnonymous = true;
    public function actionStoreDances()
    {
        $this-&gt;requirePostRequest();
        $f = craft()-&gt;request-&gt;getPost('logs');
        $dances = new DB_LogsModel();

        $errors = $dances-&gt;getAllErrors();
        craft()-&gt;urlManager-&gt;setRouteVariables(array(
            'errors'    =&gt; $errors
        ));
        $redirectUrl = '';
        //$this-&gt;redirect('http://db.craft.dev/admin/db/',true);
    }
}
How to redirect with error messages?


Generally speaking, then answer is "you don't".  You redirect on success, but if there is an error (validation or otherwise), you add those to your model, call setRouteVariables and let the request continue as usual.  That will reload the current page and you can pull the errors out of the model from Twig.

You can see the general workflow for this in a controller action in all of Craft's controllers at craft/app/controllers.

class SomeController extends BaseController
{
    public function actionDoSomething()
    {
        $myModel = new MyModel();
        $myModel-&gt;myProperty = 1;

        // Validate the model
        if ($myModel-&gt;validate())
        {
            // Pass it to the service.
            if (craft()-&gt;myService-&gt;doSomething($myModel))
            {
                // Success!
                $this-&gt;redirectToPostedUrl();
            }
        }

        // Return the model back to the template so the errors can be displayed.
        craft()-&gt;urlManager-&gt;setRouteVariables(array(
            'model' =&gt; $myModel
        ));
    }
}