簡體   English   中英

錯誤消息:在 null 上調用成員函數 render()

[英]Error Message: Call to a member function render() on null

實際上我試圖從我的控制器調用我的樹枝文件,這是我的代碼:
這是我的 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

首先,您需要從定義閉包中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]);
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM