错误消息:在null上调用成员函数render()

2020-03-26 php slim

实际上,我正在尝试从控制器调用我的树枝文件,这是我的代码:
这是我的App.php

<?php

use Slim\App;
use Slim\Http\Environment;
use Slim\Http\Uri;
use Slim\Views\Twig;
use Slim\Views\TwigExtension;

$app = new App (['settings' => ['displayErrorDetails' => true,],]);
$container = $app->getContainer();
$container['view'] = function ($container) {
    $view = new Twig('../Resources/Views/Users', ['cache' => false]); // Instantiate and add Slim specific extension$router = $container->get('router');$uri = Uri::createFromEnvironment(new Environment($_SERVER));$view->addExtension(new TwigExtension($router, $uri));return $view;};require __DIR__ . '/../Routes/routes.php';
};

这是routes.php:

<?php
$app->group('/profile', function () {
    $this->get('/fetchMyLiveAds', Application\Controllers\COlxMyAdsController::class . ':fetchMyLiveAdIdsByUserId');
    $this->get('/fetchMyDeniedAds/{id}', Application\Controllers\COlxMyAdsController::class . ':fetchMyDeniedAdIdsByUserId');
    $this->get('/fetchMyPendingAds/{id}', Application\Controllers\COlxMyAdsController::class . ':fetchMyPendingAdIdsByUserId');
});

这是我的COlxDatabaseHandler:- 您已经解决了“我的问题”,但是现在当我使用COlcUserDetails对象$ this-> m_objOlxUserDetails调用fetchMyLiveAds()时,它向我显示在Null上调用fetchMyLiveAds()的错误。

<?php
namespace Application\Eos;
use Psr\Container\ContainerInterface;

class COlxDatabaseHandler extends Container{

    public function __construct( ContainerInterface $c ) {

        $this->m_objOlxCategories      = new COlxCategories( $c );
        $this->m_objOlxProductImages   = new COlxProductImages( $c );
        $this->m_objOlxProducts        = new COlxProducts( $c );
        $this->m_objOlxProductStatuses = new COlxProductStatuses( $c );
        $this->m_objOlxStatuses        = new COlxStatuses( $c );
        $this->m_objOlxUserInterests   = new COlxUserInterests( $c );
        $this->m_objOlxUserLikes       = new COlxUserLikes( $c );
        $this->m_objOlxUserPurchases   = new COlxUserPurchases( $c );
        $this->m_objOlxUserDetails     = new COlxUserDetails( $c );
    }
}
?>

这是COlxMyAdsController类:

COlxMyAdsController extends COlxDatabaseHandler {
    public
    function fetchMyLiveAdIdsByUserId(Request $request, Response $response)
    {
        $args = $request->getParams();
        $args = $args['id'];
        $result = count($this->m_objOlxUserDetails->fetchMyLiveAdIdsByUserId($args));
        // Here the result stores int(3)
           return $view->render($response,'profile.twig',['counts'=> $result]);
           or return $this->view->render($response,'profile.twig',['counts'=> $result]);
    }
}

这是profile.twig:

<div class="CSlider" ><div class="CProducts" ><table ><tr >{
    %
    for count in 1. . counts %}<td >{
    %
    block tile %}{
    %
    include 'tile.twig' %}{
    %
    endblock %}</td >{
    %
    if count is divisible by(6) %}</tr ><tr >{
    % endif %
}{
    % endfor %
}</table ></div ></div >

这是我要使用该结果的div

Answers

首先,您需要从定义闭包中return创建的对象:

$container['view'] = function ($container) {
    // create Twig instance and set up appropriately
    $view = new Twig(...);
    // add extensions, etc...
    // You MUST return the created instance
    return $view;
};

创建控制器实例时,Slim将容器实例作为第一个参数传递给控制器​​构造函数,因此在控制器构造函数中,您可以从容器获取view并将其保存在私有或受保护的变量中,然后在路由回调中使用它:

COlxMyAdsController extends COlxDatabaseHandler {

    protected $view;

    public function __construct($container) {
        $this->view = $container->get('view');
    }

    public function fetchMyLiveAdIdsByUserId(Request $request, Response $response)
    {
        // logic goes here
        // ...
        return $this->view->render($response,'profile.twig',['counts'=> $result]);
    }

}

Related