首页 > 解决方案 > PHP 未定义变量:Controller 中的模板

问题描述

我有这个 mvc 的文件夹结构:

application
-----------catalog
------------------controller
----------------------------IndexContoller.php
------------------model
----------------------------IndexModel.php
------------------view
------------------language
-----------admin
------------------controller
------------------model
------------------view
------------------language
core
------Controller.php
public
------Index.php
vendor
....

index.php我有:

define('DS', DIRECTORY_SEPARATOR, true);
define('BASE_PATH', __DIR__ . DS, TRUE);
//Show errors
//===================================
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
define('ABSPATHS', preg_replace('/\\\/', '/', dirname(dirname(__FILE__))));
$abspath = preg_replace('/\\\/', '/', dirname(__FILE__));

require '../vendor/autoload.php';
//Upadte
$templates = new League\Plates\Engine(Config::get('PATH_VIEW'));

并像这样使用composer PSR4自动加载:

"autoload": {
        "psr-4": { "Application\\": "application/","Application\\Core\\": "application/core/","Application\\Catalog\\Model\\": "application/catalog/model/"}
    }
    }

现在Core Controller我有:

namespace Application\Core;

class Controller
{

    // public $templates;
    /**
     * Construct the (base) controller. This happens when a real controller is constructed, like in
     * the constructor of IndexController when it says: parent::__construct();
     */
    public function __construct()
    { 
        $this->Language = new Language();
    }
}

controller IndexController我有:

namespace Application\Catalog\Controller;

use Application\Core\Controller as Controller;


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

    }

    /**
     * Handles what happens when user moves to URL/index/index - or - as this is the default controller, also
     * when user moves to /index or enter your application at base level
     */
    public function index()
    {
        echo $templates->render('index/index', ['name' => 'Jonathan']);
    }
}

在行动中,我看到了这个错误并且自动加载没有在控制器中加载类:

注意:未定义的变量:第 23 行 /Applications/XAMPP/xamppfiles/htdocs/cms/application/catalog/controller/IndexController.php 中的模板

我该如何解决这个错误?

标签: php

解决方案


您没有明确列出供应商文件夹的内容,但我想您通过 composer...安装了包https ://github.com/thephpleague/plates/blob/master/composer.json... 然后:

如何放置在 Controller 中:

  /Applications/XAMPP/xamppfiles/htdocs/cms/application/core/Controller.php

最重要的是:

  use League\Plates\Engine;

还:

"autoload": {
    "psr-4": { "Application\\": "application/","Application\\Core\\": "application/core/","Application\\Catalog\\Model\\": "application/catalog/model/"}
}
}

太多了……

The following should be enough:



 autoload": {

    "psr-4": {
        "Application\\": "application/"

    }
}

然后在你的类中添加指令,如(当需要时):

  use Application\Catalog\Model\... 

编辑

在 IndexController 中无法访问 Var $templates,试试这个:

删除表格 index.php $templates = new League\Plates\Engine(Config::get('PATH_VIEW'));

并编辑 IndexController 如下:

namespace Application\Catalog\Controller;

use Application\Core\Controller as Controller;
use League\Plates\Engine;

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

    }

    /**
     * Handles what happens when user moves to URL/index/index - or - as this is the default controller, also
     * when user moves to /index or enter your application at base level
     */
    public function index()
    {
        $templates = new Engine(Config::get('PATH_VIEW'));
        echo $templates->render('index/index', ['name' => 'Jonathan']);
    }
}

但是因为我想你需要在更多控制器中使用 $templates 你应该定义

  protected $templates = new League\Plates\Engine(Config::get('PATH_VIEW'));

在父类 Controller 中并通过它访问它

   $this->templates 

在后代...


推荐阅读