0

With the QueryController i ask for $querySCP->text and $querySD->text

I pass those parameters to hte ResultController with $this->request->session...

I try to acces the $querySCP and $querySD in the view.ctp.

I have the following code:

Controller Query:

public function add()
{
$querySCP = $this->Query->newEntity();
$querySD = $this->Query->newEntity();
if ($this->request->is('post')) {
    $this->request->session()->write(
        'my-stuff', 
        $this->request->data);
    $this->redirect('/result/view');    
}
}

Controller Result:

public function view()
    {
        $myStuff = $this->request->session()->read('my-stuff');
        if (!$myStuff) {
            $this->Flash->error(__('Unable.'));
            return $this->redirect('/start/point');
        }
        $this->set($myStuff); //here i tried to pass parameters to the view.ctp
    }

View view.ctp:

<?php $myStuff->querySCP->text?>
<?php $myStuff->querySD->text?>

How do i access to those parameters? Are they methods of $myStuff? thanks.

2 Answers 2

1

I will not go into detail on how to work with entities, but the basics of what you are trying to do will look something like this.

For further information on how to patch entities etc. take a look at the docs: https://book.cakephp.org/3.0/en/orm/saving-data.html#merging-request-data-into-entities

Controller1:

public function add()
{
    $someArray = ['a' => 'b', 'c' => 'd'];

    // write the array to a session variable
    $this->request->session()->write(
        'my-stuff', 
        $someArray);

    // redirect by using array will respect CakePHP routes defined in `routes.php`
    $this->redirect(['controller' => 'Controller2', 'action' => 'view']);
}

Controller2:

public function view()
{
    // retrieve our array from the session store
    $myStuff = $this->request->session()->read('my-stuff');

    // redirect if session variable is not set or isn’t an array
    if (!$myStuff || !is_array(myStuff)) {
        $this->Flash->error(__('Error'));
        return $this->redirect(['controller' => 'Controller1', 'action' => 'add']);
    }

    // data = the variable name in the View later on
    $this->set('data', $myStuff);
}

View view.ctp:

<?= $data['a'] ?> /
<?= $data['c'] ?>

Will output:

b / d
Sign up to request clarification or add additional context in comments.

Comments

0

Honestly, do you actually have any idea what you're doing? The code doesn't make me think so.

  1. You write HTTP POST data into the session but expect to get entities back in the other controller. Why do you think this would work at all? You need to pass the entities to the session if you expect them to be there. And by the way: $this->request->data is deprecatd since CakePHP 3.4.
  2. Setting an entity the way you try to do it would result in a warning Illegal offset type
  3. You try to show input without echoing it in the view (missing echo)
  4. The way you try to pass the data and then access $myStuff makes me think that you don't know the difference between an object and an array.

I strongly recommend you to do some starter tutorials on php in general and to do the CakePHP blog tutorial from the beginning as it will teach you how to work with the framework.

Have a read of at least these topics:

I'm not going to chew the code for you but here is what you need to do: Put both entities in an array, pass the array to session, read that array from the session, set that to the view. But what you really should do first is to get an understanding how to do what what you want to do. Read the linked documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.