如何仅将数据从类传递到模板?

时间:2019-07-01 02:56:29

标签: php

我的课...

class Comments extends Controller
{
   public function addComment()
{
   if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   // sanitize form data
   $_POST = filter_input_array(INPUT_POST,FILTER_SANITIZE_STRING);
  // init data
  $data = [
  'comment' => trim($_POST['comment']),
  'comment_err' => ''
  ];
  // validate comment
  if (empty($data['comment'])) {
  $data['comment_err'] = 'please insert comment';
  }
 }
  // make sure empty error
  if (empty($data['comment_err'])) {
  // validate && add comment
  if($this->model('Comments')->addComment($data)){
   // for show message
  flash('pm','Your comment will be published after confirmation');
  $this->view('pages/show',$data);
  }else{
  flash('pm','Error!...','alert alert-danger');
  $this->view('pages/show',$data);
  }
else{
// load view with error
$this->view('pages/show',$data);
}
}else {
// load view
$data = [
'comment' => '',
'comment_err' => ''
];
$this->view('pages/show',$data);
}
}//end class

我想将数据从类传递到模板,但是对于类,首先,您必须随数据提供模板,其次,您必须有一个单独的页面,例如与其他页面的其余部分共享的菜单,则此方法不起作用,在类中进行处理后,现在如何将数据传递给所需的视图?谢谢您的回答。

0 个答案:

没有答案