在控制器中调用操作后全局变量更改为 null

问题描述 投票:0回答:1

我想调用删除操作,但我的全局变量设置为空。 控制器:

App::uses('AppController', 'Controller');

class WhitelistController extends AppController
{
public $helpers = array('Html', 'Form');

private $deletes;


public function index()
{
    if ($this->request->is('post'))
    {

        $whitelist = $_POST['data']['whitelist']['stylecodes'];
        if($whitelist == "")
            return $this->Session->setFlash('You did not enter stylecodes. If you send this, it will delete all the shoes from the website.');


        $whitelist = preg_replace('/\s+/', '', $whitelist);
        $whitelist = explode(",", $whitelist);


        $url = 'http://fonda.vps.***********/product_import_json_all_published_products';
        $ch = curl_init($url);
        curl_setopt( $ch, CURLOPT_POST, 1);
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt( $ch, CURLOPT_HEADER, 0);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
        $all = curl_exec($ch);
        $all = json_decode($all, true);
        print_r($all);

        $this->deletes = array_diff($all, $whitelist);

        if($this->deletes == $all)
            return $this->Session->setFlash('It seems you entered wrong data, what could delete all of the shoes from the website.');


        $this->deletes = array_values($this->deletes);
        pr($this->deletes);

        $this->set('delete', $this->deletes);
        $this->render('delete');
    }

}

public function delete()
{
    if($this->request->is('post'))
    {


    }else{

        $url = 'http://fonda.vps.*******/product_import_json_remove';
        $myvars = 'products=' . json_encode($this->deletes);
        print_r();

        pr($myvars);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($ch);
        $response = json_decode($response, true);

        pr($response);





        $removed = count($response['removed']);
        $skipped = count($response['skipped']);

        $string = $removed . " shoe(s) removed and " . $skipped . " shoe(s) skipped.";
        $this->Session->setFlash($string);

        if (count($response['error']) != 0) {
            $string = "";
            echo " ";
            foreach ($response['error'] as $key => $value) {
                $string .= $key . ": " . $value;
                $string .= "\n";
            }
            $this->Session->setFlash('Error found in following shoe(s): ' . $string);
        }

        return $this->redirect(array( 'action' => 'index'));

    }
}


}

从视图中调用delete()后,$this->deletes 得到 null。

我没有看到任何改变删除值的东西。

有什么问题吗?

php cakephp cakephp-2.3
1个回答
0
投票

我想我已经明白了。我删除了全局变量,并在第一个操作中将局部变量放入会话中:

$this->Session->write('deletes', $delete);

我在第二个动作中读到了这样的内容:

$delete = $this->Session->read('deletes');

这样我就可以在第二个操作中使用 $delete 了。

© www.soinside.com 2019 - 2024. All rights reserved.