Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to route submitted form to specific controller action

I'm new to using phalcon, I have a simple form

<form id="form" method="post" action="/user"> <input type="text" name="address"> <input type="submit"> </form>

How do I route the form so that it posts to the controller UserController and method postAction, currently when I submit the page just reloads, I thought that Phalcon would recognise the post request and route it to the postAction?

Thanks



21.6k
Accepted
answer

You might check if is a POST request and then redirect or forward. +Info: dispatcher, controller, request and response

class UserController extends \Phalcon\Mvc\Controller {

public function indexAction() {
    if ($this->request->isPost()) { 
        return $this->dispatcher->forward('users', 'post'); // here forward to don't lose POST data
    }
}

public function postAction() {
    // here check data and store the address

    // then redirect to index or stay here
    $this->response->redirect('/users');
}

Good luck



2.9k

Hi, thanks for the response would this be the typical (recommended) way of doing things, or is there a better alternative?

As this seems like a bit extra processing invloved

Thanks

well, other way, you can build a system based on events, in this case you have to attach an event to listen POST request and check if some data exists do something