将数据从类传递到模板

时间:2018-07-09 12:36:15

标签: php

我不知道如何将数据从一个类传递到我的一个类中的studentShow模板文件。

模型

<?php
class Student{
public function all(){
    require_once('config/db.php');
    $SQLCommand = "SELECT * FROM people";
    $Query      = $conn->prepare($SQLCommand);
    $Query->execute();
    $rows       = $Query->fetchAll(PDO::FETCH_OBJ);

    return $rows;
  }
}
?>

索引

<?php 


require_once("app/Controller/StudentController.php");


$Student = new StudentController();
$Student->index();

?>

控制器

<?php 


require_once("app/Student.php");

class StudentController{


public function index(){
    $Student = Student::all();
    include ('resource/studentShow.php');
  }
}

?>

我的问题是:在我的控制器中,如何将$student变量传递给studentShow.php。

1 个答案:

答案 0 :(得分:0)

这是实现这一目标的方法:

public function index() {
    $student = Student::all();
    extract(['student' => $student]);
    ob_start();
    include ('resource/studentShow.php');
    return ob_get_clean();
}

详细了解ob_startextractob_get_clean

相关问题