We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Get array for dispatcher from url

I have a page /form with a form that submits to /profiles/create. In my ProfilesController in the createAction method I have

if (!$profile->save()) {
    $this->flash->error($profile->getMessages());
    if (!is_null($this->request->getServer('HTTP_REFERER'))) {
        return $this->dispatcher->forward($this->request->getServer('HTTP_REFERER'));
    }
}

with the intention being to redirect the user back to \form if they came from there but still show the flash message explaining what went wrong (which is why I was using forward instead of redirect since redirect would remove the flash messages).

However, $this->dispatcher->forward() requires an array but $this->request->getServer("HTTP_REFERER") returns a string. How do I get the array needed from this string? Or what should I use instead of dispatcher->forward()?

edited Sep '15

Using $this->response->redirect($url)

Or just pass in the correect array to $this->dispatcher->forward(), you most know the controller and action for the /form url, why use HTTP_REFERER?

I'll try implementing that now, thanks.

Although in this example I know the controller/action for /form, ultimately an admin will be able to dynamically add/edit/remove certain pages - some of which may contain forms. E.g. an enquiry form on every page would submit to enquiries/create but you wouldn't want the user to be taken away from their page if they use the form.

Hmm.. you could name the individual routes, and then use that name to fetch back the module/controller/action!

edited Sep '15

I've got the flashSession to show up, but if there are multiple messages stored - $this->flashSession->error($profile->getMessages()) - then the message just shows "Array" - how can I make it loop through the array of messages to actually display each one?

When using Phalcon\Flash\Direct each message was showing, i.e. - $this->flash->error($profile->getMessages()) - displays each message in the view.