DANLEE
DANLEE

Reputation: 453

CakePHP 3.0 URL Parameter

Previously, in CakePHP 2.0. I am able to access the tokenid inside the 'if it a post condition' after I hit the submit button. Apparently now, after I hit the submit button in CakePHP 3.0, I am no longer able to receive the tokenid in the 'if it a post condition'. How can I continue to access the URL parameter inside the 'if it a post condition'? I know, it is really something simple. Can anyone enlighten me? What am I missing?

URL

/users/reset/15d3a535ecdd4ec705378b146ef572cf5bb9bfc2

Controller

public function reset($token=null) {

    if ($token) { //I am able to get the tokenid here. 

    Debugger::Dump($this->request->pass[0]); //I am able to get the tokenid here. 
    Debugger::Dump($this->request->params['pass'][0]); //I am able to get the tokenid here. 

         if ($this->request->is(['post'])) {
                 Debugger::Dump($token) //I am no longer able to get the tokenid.
                 Debugger::Dump($this->request->pass[0]); //I am no longer able to get the tokenid.
                 Debugger::Dump($this->request->params['pass'][0]); //I am no longer able to get the tokenid.
         }
    }
}

View

<?php echo $this->Form->create(null, ['url' => ['controller' => 'Users', 'action' => 'reset']]); ?>
<?php echo $this->Form->input('password'); ?>
<?php echo $this->Form->button('Submit', ['type' => 'submit']); ?>
<?php echo $this->Form->end() ?>

After Ofir feedback,

I added the below inside the form.

<?php echo $this->Form->input('resetToken', array('type'=> 'hidden','value'=>$this->request->pass[0])); ?>

Upvotes: 3

Views: 1942

Answers (1)

If the for you created in the view template file is posting to another URL, you need to add the token to the form action url:

$this->Form->create(null, ['url' => ['controller' => 'Users', 'action' => 'reset', $token]]);

If you are posting to the same URL, there is no need to specify the URL, as it will post to the same location:

$this->Form->create();

With that, you will be able to access the $token param in your controller after a POST.

Upvotes: 4

Related Questions