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

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



32.2k
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



5.7k

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