如何解决错误“Trying to get 属性 of non-object” - Slim 3 Framework PHP
How to solve the error " Trying to get property of non-object" - Slim 3 Framework PHP
首先,对不起我的英语,其次我的错误是
Notice: Trying to get property of non-object in
C:\wamp64\www\projeto\app\OfertasVendor\Middleware\ValidationErrorsMiddleware.php
on line 9
ValidationErrorsMiddleware.php的第9行是:
$this->container->view->getEnvironment()->addGlobal('errors',$_SESSION['errors']);
ValidationErrorsMiddleware.php的完整代码为
<?php
namespace OfertasVendor\Middleware;
class ValidationErrorsMiddleware extends Middleware{
public function __invoke($req, $res, $next){ $this->container->view->getEnvironment()->addGlobal('errors',$_SESSION['errors']);
//var_dump($_SESSION['errors']);
//unset($_SESSION['errors']);
var_dump('teste');
$res = $next($req, $res);
return $res;
}
}
但是,我已经制作了 Middleware.php,我通过了容器,
中间件代码:
namespace OfertasVendor\Middleware;
//use OfertasVendor\Controllers\DatabaseController;
class Middleware{
protected $container;
public function __contruct($container){
$this->container = $container;
}
}
我只需要传递容器->视图,但是,框架不识别容器...
我做了Controller.php
namespace OfertasVendor\Controllers;
abstract class Controller{
protected $container;
public function __construct($container){
$this->container = $container;
}
public function __get($key){
if($this->container->{$key}){
return $this->container->{$key};
}
}
}
我的应用程序 index.php 有中间件
$app->add(new \OfertasVendor\Middleware\ValidationErrorsMiddleware($container));
你能帮帮我吗?
试试 var_dump($this->container);
和 die();
如果那是一个对象,请尝试 var_dump($this->container->view);
,然后 die();
基本上,继续 vardump 每一步,直到你到达任何不是对象的地方。 null
.
更常见的是 null
.
首先,对不起我的英语,其次我的错误是
Notice: Trying to get property of non-object in C:\wamp64\www\projeto\app\OfertasVendor\Middleware\ValidationErrorsMiddleware.php on line 9
ValidationErrorsMiddleware.php的第9行是:
$this->container->view->getEnvironment()->addGlobal('errors',$_SESSION['errors']);
ValidationErrorsMiddleware.php的完整代码为
<?php
namespace OfertasVendor\Middleware;
class ValidationErrorsMiddleware extends Middleware{
public function __invoke($req, $res, $next){ $this->container->view->getEnvironment()->addGlobal('errors',$_SESSION['errors']);
//var_dump($_SESSION['errors']);
//unset($_SESSION['errors']);
var_dump('teste');
$res = $next($req, $res);
return $res;
}
}
但是,我已经制作了 Middleware.php,我通过了容器, 中间件代码:
namespace OfertasVendor\Middleware;
//use OfertasVendor\Controllers\DatabaseController;
class Middleware{
protected $container;
public function __contruct($container){
$this->container = $container;
}
}
我只需要传递容器->视图,但是,框架不识别容器...
我做了Controller.php
namespace OfertasVendor\Controllers;
abstract class Controller{
protected $container;
public function __construct($container){
$this->container = $container;
}
public function __get($key){
if($this->container->{$key}){
return $this->container->{$key};
}
}
}
我的应用程序 index.php 有中间件
$app->add(new \OfertasVendor\Middleware\ValidationErrorsMiddleware($container));
你能帮帮我吗?
试试 var_dump($this->container);
和 die();
如果那是一个对象,请尝试 var_dump($this->container->view);
,然后 die();
基本上,继续 vardump 每一步,直到你到达任何不是对象的地方。 null
.
null
.