php - 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 中的模板
我该如何解决这个错误?
解决方案
您没有明确列出供应商文件夹的内容,但我想您通过 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
在后代...
推荐阅读
- c# - 你如何使用 Intent 的字幕启动 VLC for android
- javascript - 将函数 A 作为 prop 传递,并在子调用 A 时调用另一个函数
- r - RHS 中使用 dplyr:case_when() 的评估错误
- c - 从 C 项目中删除“下雨天”代码
- java - 为什么我在新项目中得到具有相同 jsp 文件的 Whitelabel 错误页面?
- sql - 图片未从数据库中显示
- python - Python在特定目录中创建文件
- ios - iOS Firebase crashlytics 仪表板在通过 Testflight 归档和安装应用程序时不显示崩溃
- uicollectionview - 高效地创建包含很多部分和项目的 NSDiffableDataSourceSnapshot
- php - PHP 7.2 抛出运行“composer require symfony/dotenv”,7.1 没有