Cakephp控制器操作不会在实时服务器上重定向

时间:2015-01-27 13:01:56

标签: cakephp redirect

我有一个动作来保存4个模型中的数据。这是我的控制器代码。我确实收到任何保存数据的错误,我仔细检查了它,但是这个动作在保存数据后没有重定向到给定的URL,它显示的是同一动作的空白页面(用于保存数据的动作)。此问题仅在实时服务器上。在我的本地副本工作正常。

if ($this->request->is('post')) {
            //First upload user pic
            if ($this->request->data['FacilityResident']['pic']['name'] != NULL) {
                $pic = $this->request->data['FacilityResident']['pic']['tmp_name'];
                if (!is_dir(WWW_ROOT . 'img' . DS . 'residents' . DS)) {
                    mkdir(WWW_ROOT . 'img' . DS . 'residents' . DS);
                    chmod(WWW_ROOT . 'img' . DS . 'residents' . DS, 0777);
                }
                $uniq = mt_rand();
                move_uploaded_file($pic, WWW_ROOT . 'img' . DS . 'residents' . DS . $this->request->data['FacilityResident']['pic']['name']);
                $this->request->data['FacilityResident']['avatar'] = DS . 'residents' . DS . $this->request->data['FacilityResident']['pic']['name'];
                unset($this->request->data['FacilityResident']['pic']);
            }

            $this->request->data['FacilityResident']['facilities_id'] = $this->Session->read('urlparam.facilityId');
            $this->FacilityResident->save($this->request->data['FacilityResident']);
            $residentId = $this->FacilityResident->getLastInsertID();
            if (!empty($this->request->data['FacilityResidentDietary']['diet'])) {
                $dietData = implode(',', $this->request->data['FacilityResidentDietary']['diet']);
                unset($this->request->data['FacilityResidentDietary']['diet']);
                $this->request->data['FacilityResidentDietary']['diet'] = $dietData;
            }
            $this->request->data['FacilityResidentDietary']['facility_residents_id'] = $residentId;
            $this->FacilityResidentDietary->save($this->request->data['FacilityResidentDietary']);
            $this->request->data['ResidentMealschedule']['facility_residents_id'] = $residentId;
            $this->ResidentMealschedule->save($this->request->data['ResidentMealschedule']);
            $residentMealSchedultId = $this->ResidentMealschedule->getLastInsertID();
            foreach ($this->request->data['Mealdetail'] as $key => $value) {
                if ($key == 'Bread') {
                    $value[0]['attribute_four'] = implode(',', array_filter($value[0]['attribute_four']));
                }
                foreach ($value as $val) {
                    $this->Mealdetail->saveAll($val);
                    $data['Mealschedule']['meal_types_id'] = 1;
                    $data['Mealschedule']['resident_mealschedule_id'] = $residentMealSchedultId;
                    $data['Mealschedule']['mealdetails_id'] = $this->Mealdetail->getLastInsertID();
                    $this->Mealschedule->saveAll($data);
                }
                unset($data);
                unset($val);
            }
            $i = 1;
            foreach ($this->request->data['ResidentMealMeta'] as $key => $val) {
                $data['ResidentMealMeta'][$i]['resident_mealschedule_id'] = $residentMealSchedultId;
                $data['ResidentMealMeta'][$i]['meal_value'] = $key;
                $data['ResidentMealMeta'][$i]['meal_option'] = $val;
                $i++;
            }
            if (!empty($this->request->data['ResidentTray']['tray'][0])) {
                $j = count($data['ResidentMealMeta']) + 1;
                $data['ResidentMealMeta'][$j]['meal_value'] = 'tray';
                $data['ResidentMealMeta'][$j]['resident_mealschedule_id'] = $residentMealSchedultId;
                $data['ResidentMealMeta'][$j]['meal_option'] = implode(',', $this->request->data['ResidentTray']['tray']);
            }
            unset($this->request->data['ResidentMealMeta']);
            unset($this->request->data['ResidentTray']);
            if ($this->ResidentMealMeta->saveAll($data['ResidentMealMeta'])) {
                $this->Session->setFlash('New resident added', 'flash', array('alert' => 'success'));
                if(isset($url)){
                    unset($url);
                }
                $url = Router::url(array(
                            'controller' => 'residents',
                            'action' => 'resident_list',
                            '?' => array('facility_name' => $this->Session->read('urlparam.facilityName'), 'facility_id' => $this->Session->read('urlparam.facilityId'))
                ));
                $this->redirect($url);
            }
        }

这里有人可以告诉我出了什么问题吗?我也试过通过相关模型保存数据,但没有效果。

1 个答案:

答案 0 :(得分:0)

我建议使用此解决方案:

Router::redirect(
    '/controller_where_you_re_saving_data/*',
    array('controller' => 'residents', 'action' => 'resident_list'),
    // view action expects $id as an argument
    array('facility_name' => $this->Session->read('url.facilityName'))
);

但是,我建议不要在关于会话值的情况下传递参数,因为你可以在项目的每个控制器中访问它,这使得解决方案如下:

Router::redirect(
    '/controller_where_you_re_saving_data/*',
    array('controller' => 'residents', 'action' => 'resident_list'),
  );

这是文件: http://book.cakephp.org/2.0/en/development/routing.html#redirect-routing

希望它有所帮助。