【问题标题】:How to solve the error " Trying to get property of non-object" - Slim 3 Framework PHP如何解决错误“尝试获取非对象的属性” - Slim 3 Framework PHP
【发布时间】:2018-06-22 03:29:58
【问题描述】:

首先,对不起我的英语,其次是我的错误

注意:试图获取非对象的属性 C:\wamp64\www\projeto\app\OfertasVendor\Middleware\ValidationErrorsMiddleware.php 在第 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));

你能帮帮我吗?

【问题讨论】:

  • 第 9 行 哪一行是第 9 行。从您在这里粘贴的代码中看不清楚
  • 第9行是$this->container->view->getEnvironment()->addGlobal('errors',$_SESSION['errors']);
  • 嗯,你认为是对象的事情之一不是对象
  • 他们说“视图”不是一个对象,但我需要这个对象......我在其余代码中使用视图对象并且工作,但在这个文件中不起作用

标签: php frameworks slim


【解决方案1】:

试试var_dump($this-&gt;container);die();

如果这是一个对象,请尝试var_dump($this-&gt;container-&gt;view);,然后尝试die();

基本上,在你到达任何不是对象的地方之前,不断地推销每一步。 null.

【讨论】:

  • 我做 var_dump($this->container) 并返回 NULL
  • 当我做 var_dump($this->container->view) 时返回“注意:试图在 C:\wamp64\www\projeto\app\OfertasVendor\Middleware\ 中获取非对象的属性第 12 行的 ValidationErrorsMiddleware.php"
  • 但我需要使用“$this->container->view->getEnvironment()->addGlobal('errors',$_SESSION['errors']);”
  • 那么,就是这样,那么,由于某种原因,$this->container 为 NULL。那不是一个对象。尝试在 Middleware.php 的构造函数中转储 $container。某处将其设置为空。继续往上走,看看发生了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-14
  • 1970-01-01
  • 2020-06-24
  • 1970-01-01
  • 2014-05-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多