我在尝试请求从表单发送的数据时遇到了一些问题。当我检查$this->request->data
时,它表示它是一个空数组,所以我想我会尝试debug($this->request)
并显示数据为array(0)
。我不确定为什么会这样。我提供了我的模型 - 视图 - 控制器文件。
修改
经过一些进一步的测试,我发现我的问题在于
if(!empty($this->request->data))
UsersController.php
中的。我不知道为什么,但data[Reference][]
形式没有到达控制器。我已经添加了以下代码的片段。
(Controller)UsersController.php:
public function send_invitation() {
$user = $this->Auth->user();
$this->layout = "dashboard";
// debug($this->request);
if (!empty($this->request->data)) {
...
} else {
echo 'there is no data';
}
$this->set("user", $user);
}
(查看)send_invitation.php:
<div class="dashboard-container-right">
<div class="dashboard-innerright">
<div class="upper-dashboard"><h1> <?php echo __("Send Invitations"); ?> </h1></div>
<div class="botm-forcolor">
<div class="midd-dashord">
<?php echo __("Your Referral Code is"); ?> <strong><?php echo $user["referral_code"];?> </strong>
<div class="invite-content">
<!--- Instructor Login -->
<div class="tab-pane active" id="instructor">
<?php echo $this->Form->create('Reference' , array('id'=>'send_invites' , 'url' => array('controller' => 'users', 'action' => 'send_invitation' ) , 'novalidate'=>'novalidate')); ?>
<?php $form=$this->Form;?>
<div class="padding_15tabs">
<div class="col-md-12 show-grid row">
<?php $name=__("Name"); ?>
<?php echo $form->input('Reference.name',array('class'=>'col-xs-12 col-sm-12 col-lg-12 text-style', 'placeholder'=>$name, 'div' => false,'label'=>false))?>
</div>
<div class="col-md-12 show-grid row">
<?php $email=__("Email"); ?>
<?php echo $form->input('Reference.email',array('class'=>'col-xs-12 col-sm-12 col-lg-12 text-style', 'placeholder'=>$email, 'div' => false,'label'=>false))?>
</div>
<div class="col-md-12 show-grid row">
<?php $message=__("Message"); ?>
<?php echo $form->textarea('Reference.message',array('class'=>'col-xs-12 col-sm-12 col-lg-12 text-style', 'placeholder'=>$message, 'div' => false,'label'=>false))?>
</div>
<input type="hidden" name="data[Reference][user_id]" value="<?php echo $user['id']; ?>">
<div class="col-md-12 show-grid row">
<div class="align-center">
<span class="span_float">
<input type="submit" value="<?php echo __('Invite'); ?>" class="create-account btn btn-primary" /><br/>
</span>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
(型号)Reference.php:
class Reference extends AppModel {
public $useTable = 'references';
public $name = 'Reference';
public $belongsTo = array('User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
));
var $actsAs = array('Multivalidatable');
public $validationSets = array(
'add' => array(
'email' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'Please enter email.'
),
'valid' => array(
'rule' => 'email',
'required' => true,
'message' => 'Please enter valid email.'
),
),
'name' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'Please enter name.'
)
),
),
);
}
CommonComponent.php文件:
<?php
App::uses('Component', 'Controller');
class CommonComponent extends Component {
public function sent_mail($params)
{
$Email = new CakeEmail();
//$Email->config('smtp');
//$Email->template($params['template']);
$Email->emailFormat('html');
//$Email->viewVars($params['vars']);
$Email->from(array($params['from'] => 'StempClub'));
$Email->to($params['to']);
$Email->subject($params['subject']);
if($Email->send($params['vars']['link']))
{
return 1;
}
else
{
return 0;
}
}
function sendCommonEmailTo($params)
{
$Email = new CakeEmail();
$email_template = $this->get_email_template($params['template']);
$Email->to($params['to']);
$Email->from(array($params['from'] =>'StempClub'));
$Email->subject($email_template['EmailTemplate']['subject']);
$Email->emailFormat('html');
$email_template_content = $this->render_email_template($email_template['EmailTemplate']['message'] , $params['vars']);
$Email->send($email_template_content);
return true;
}
function get_email_template($title)
{
$EmailTemplate = ClassRegistry::init('EmailTemplate');
$email_template = $EmailTemplate->find('first', array('conditions'=>array('EmailTemplate.keyword'=>$title)));
return $email_template;
}
function render_email_template($html_content , $vars)
{
foreach($vars as $key => $value)
{
$html_content = str_replace("$".$key,$value,$html_content);
}
return $html_content;
}
public function resizeImage($image,$width,$height,$scale)
{
list($imagewidth, $imageheight, $imageType) = getimagesize($image);
$imageType = image_type_to_mime_type($imageType);
$newImageWidth = ceil($width * $scale);
$newImageHeight = ceil($height * $scale);
$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
switch($imageType) {
case "image/gif":
$source=imagecreatefromgif($image);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
$source=imagecreatefromjpeg($image);
break;
case "image/png":
case "image/x-png":
$source=imagecreatefrompng($image);
break;
}
imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
switch($imageType) {
case "image/gif":
imagegif($newImage,$image);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
imagejpeg($newImage,$image,90);
break;
case "image/png":
case "image/x-png":
imagepng($newImage,$image);
break;
}
chmod($image, 0777);
return $image;
}
public function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale)
{
list($imagewidth, $imageheight, $imageType) = getimagesize($image);
$imageType = image_type_to_mime_type($imageType);
$newImageWidth = ceil($width * $scale);
$newImageHeight = ceil($height * $scale);
$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
switch($imageType) {
case "image/gif":
$source=imagecreatefromgif($image);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
$source=imagecreatefromjpeg($image);
break;
case "image/png":
case "image/x-png":
$source=imagecreatefrompng($image);
break;
}
imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height);
switch($imageType) {
case "image/gif":
imagegif($newImage,$thumb_image_name);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
imagejpeg($newImage,$thumb_image_name,90);
break;
case "image/png":
case "image/x-png":
imagepng($newImage,$thumb_image_name);
break;
}
chmod($thumb_image_name, 0777);
return $thumb_image_name;
}
public function getHeight($image)
{
$size = getimagesize($image);
$height = $size[1];
return $height;
}
public function getWidth($image)
{
$size = getimagesize($image);
$width = $size[0];
return $width;
}
public function unlink_file($image_location)
{
@unlink($image_location);
return 1;
}
public function save_image_from_url($server_image_path,$filename)
{
$local_realimage_path = REAL_IMAGE_PATH.$filename;
file_put_contents($local_realimage_path, file_get_contents($server_image_path));
return $local_realimage_path;
/*$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);*/
}
public function copy_file($from_image_location,$to_image_location)
{
copy($from_image_location, $to_image_location);
return $to_image_location;
}
}
答案 0 :(得分:1)
看起来好像在使用SecurityComponent
(可能在AppController::$components
中加载?)。
此组件要求您使用FormHelper
创建表单,以及SecurityComponent
文档中提到的其他限制。
如果您希望禁用特定操作的安全约束,可以将方法添加到:
$this->Security->unlockedActions()
如Disabling CSRF and Post Data Validation For Specific Actions中所述。
答案 1 :(得分:0)
此安全组件有时可能会造成混淆并导致此类问题。我建议的是在Model / AppModel.php中编写下面提到的代码。它的效果一样好!
public function beforeSave($ options = array()){
if(isset($this->data)){
foreach($this->data as $key=>&$result_array){
foreach($result_array as $field=>&$value){
if($value)
$value = filter_var ($value, FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
}
}
}
return true;
}