PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 16, 2022

[FIXED] Process post action with another action within controller

 January 16, 2022     cakephp, cakephp-2.5     No comments   

Issue

I have a terribly designed app where the index action displays a form in a JavaScript-based dialogue that's submitted to process action and then redirects to index (either on success or on error). The process action does not even have a view:

class UnicornsController
{
    public function index($foo, $bar)
    {
        $this->set(
            array(
                'unicorn' => $this->Unicorn->findByFooAndBar($foo, $bar);
            )
        );
    }

    public function process()
    {
        $this->Unicorn->save($this->request->data);
        $this->redirect(
            array(
                'action' => 'index',
                $this->request->data['Unicorn']['foo'],
                $this->request->data['Unicorn']['bar'],
            )
        );
    }
}

I'm adding proper error reporting. I'm trying to change the this->redirect() part so $this->request->data is not lost and I have a chance to display it again in the form generated in index.ctp but I can't get it right: both $this->requestAction() and $this->index() try to render process.ctp anyway. Am I using them incorrectly or I'm missing the right approach?


Solution

If you want to run a different action, you can use Controller::setAction(), it changes the action parameter in the request, sets the template to render accordingly, and returns the possible return value of the invoked action.

public function process()
{
    // ....

    $this->setAction(
        'index',
        $this->request->data['Unicorn']['foo'],
        $this->request->data['Unicorn']['bar']
    );
}

See also

  • API > Controller::setAction()


Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing