从控制器到视图的设置数据中的未定义变量

时间:2018-06-10 12:53:13

标签: php

我在我的纯php mvc cms中有这个Controller:

class AdminController extends Controller
{
    /**
     * Construct this object by extending the basic Controller class
     */
    public function __construct()
    {
        parent::__construct();

        // special authentication check for the entire controller: Note the check-ADMIN-authentication!
        // All methods inside this controller are only accessible for admins (= users that have role type 7)
        Auth::checkAdminAuthentication();
    }

    /**
     * This method controls what happens when you move to /admin or /admin/index in your app.
     */
    public function index()
    {

        $this->View->render('admin/index','admin', array(
                'users' => UserModel::getPublicProfilesOfAllUsers(),

            )
        );

        $this->Language->load('error/not_found'); 

        $data['header'] = $this->Language->get('heading_title');   

        //echo $data['header'];


    }
}

当我echo $data['header'];我的输出为真并显示我的结果时。现在我需要将此结果发送到view模板。鉴于我有:

<p><?php echo $header; ?></p>

但结果我看到了这个错误:

Notice: Undefined variable: header in /htdocs/cms/application/view/admin/index.php on line 59

更新:查看课程:

class View
{

    /**
     * simply includes (=shows) the view. this is done from the controller. In the controller, you usually say
     * $this->view->render('help/index'); to show (in this example) the view index.php in the folder help.
     * Usually the Class and the method are the same like the view, but sometimes you need to show different views.
     * @param string $filename Path of the to-be-rendered view, usually folder/file(.php)
     * @param array $data Data to be used in the view
     */
    public function render($filename, $folder = null, $data = null)
    {
        if ($data) {
            foreach ($data as $key => $value) {
                $this->{$key} = $value;
            }
        }

        if($folder = 'admin'){

            require Config::get('PATH_VIEW_ADMIN') . '_templates/header.php';
            require Config::get('PATH_VIEW') . $filename . '.php';
            require Config::get('PATH_VIEW_ADMIN') . '_templates/footer.php';   

        } else {

            require Config::get('PATH_VIEW') . '_templates/header.php';
            require Config::get('PATH_VIEW') . $filename . '.php';
            require Config::get('PATH_VIEW') . '_templates/footer.php';

        }
    }

如何解决此错误并将数据发送到视图模板?

1 个答案:

答案 0 :(得分:0)

正如我在你的观点课上看到的那样。

您必须像这样修改您的操作方法:

<p><?php echo $this->header; ?></p>
<p><?php echo $data['header']; ?></p>

然后在你的观点上你可以做到:

{{1}}

如您所见,您可以通过两种方式访问​​html部分的参数包。