【问题标题】:Getting Call to a member function view() on null在 null 上调用成员函数 view()
【发布时间】:2020-12-19 14:08:10
【问题描述】:

我遇到了 CodeIgniter 4 的问题:在我的控制器中,如果我试图调用视图,它会显示以下错误。

namespace App\Controllers;

class Security extends BaseController{
/**
 * An array of helpers to be loaded automatically upon
 * class instantiation. These helpers will be available
 * to all other controllers that extend BaseController.
 *
 * @var array
 */
protected $helpers = ['curl'];

public function index(){
    $this->load->view('head');
    $this->load->view('security/login');
}



}

我正面临以下问题。

CRITICAL - 2020-08-31 07:39:19 --> 调用成员函数 view() on 空 #0 /var/www/html/codeignietor/system/CodeIgniter.php(918): App\Controllers\Security->index() #1 /var/www/html/codeignietor/system/CodeIgniter.php(404): CodeIgniter\CodeIgniter->runController() #2 /var/www/html/codeignietor/system/CodeIgniter.php(312): CodeIgniter\CodeIgniter->handleRequest() #3 /var/www/html/codeignietor/public/index.php(45): CodeIgniter\CodeIgniter->run() #4 /var/www/html/codeignietor/system/Commands/Server/rewrite.php(34): require_once('/var/www/html/c...') #5 {主}

【问题讨论】:

    标签: codeigniter view codeigniter-4


    【解决方案1】:

    $this->load 为空,因为这不是您在 CI4 中加载视图的方式。您需要确保您正在查看文档的 CI4 版本,如果这实际上是您正在使用的版本:https://codeigniter4.github.io/userguide/outgoing/views.html

    在 CI4 中,视图是通过 echoing(或 returning)调用 view() 来加载的:

    namespace App\Controllers;
    
    class Security extends BaseController{
    /**
     * An array of helpers to be loaded automatically upon
     * class instantiation. These helpers will be available
     * to all other controllers that extend BaseController.
     *
     * @var array
     */
    protected $helpers = ['curl'];
    
    public function index(){
        //$this->load->view('head');
        //$this->load->view('security/login');
        echo view('head');
        echo view('security/login');
    }
    }
    

    您在代码中呈现的方式是您在 CI3 中加载视图的方式。 3 和 4 之间有很大的变化,所以如果这是您第一次跳转,您应该完整地查看文档。

    【讨论】:

    • 是的,我知道了,我的错误是 CI4 版本不稳定,其文档尚未更新
    猜你喜欢
    • 1970-01-01
    • 2017-08-09
    • 2020-03-10
    • 2017-09-24
    • 2018-02-06
    • 2018-09-29
    • 2018-08-04
    • 2020-03-28
    • 2020-10-28
    相关资源
    最近更新 更多