【问题标题】:CodeIgniter 4: Autoload LibraryCodeIgniter 4:自动加载库
【发布时间】:2019-11-17 11:56:03
【问题描述】:

我正在使用CodeIgniter 4 的最新“主”分支

我有一个试图自动加载的库。实际上,我希望拥有“一个”index.php(具有元、基本 html 结构等),通过它我可以通过“模板”库加载视图。

我的库文件:(~/app/Libraries/Template.php)

//class Template extends CI_Controller
class Template {

    /* This throws an error, but I will open up a separte thread for this
    public function __construct() {
        parent::__construct();
    }
    */

    public function render($view, $data = array()) {        
        $data['content_view'] = $view;  
        return view('layout/index', $data);     
    }

}

我还设置了一个控制器:

class Locations extends BaseController
{

    public function index()
    {
        return $this->template->render("locations/index", $view_data); 
        //return view('locations/index');
    }

    //--------------------------------------------------------------------

}

在 ~/app/Config/ 我添加了我的库

    $classmap = [
        'Template' => APPPATH .'/Libraries/Template.php'
    ];

我收到以下错误:

Call to a member function render() on null

我做错了什么导致我的库无法加载?

【问题讨论】:

  • CodeIgniter 4 未发布。不利于生产。你为什么用它?
  • 测试一下:)

标签: php autoload codeigniter-4


【解决方案1】:

在 CI4 中,BaseController 是您创建希望被多个其他控制器使用的东西的地方。在 CI4 中创建扩展其他类非常容易。

在我看来,您唯一缺少的就是创建 Template 类。 (还有一些其他的小事,但我该指责谁?)

一个大项目可能只是你没有展示它,即使你正在做它。那就是使用namespaceuse 指令。它们是 CI 4 的必备项目。

由于您放置了不需要的文件,因此应该删除以下内容。看看我是如何使用 use 的,它会导入自动加载器已知的 namespace

$classmap = [
        'Template' => APPPATH .'/Libraries/Template.php'
    ];

首先,BaseController

/app/Controllers/BaseController.php

<?php
namespace App\Controllers;

use CodeIgniter\Controller;
use App\Libraries\Template;

class BaseController extends Controller
{

    /**
     * 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 = [];

    protected $template;

    /**
     * Constructor.
     */
    public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);

        $this->template = new Template();
    }

}

/app/Controllers/Locations.php

class Locations extends BaseController
{
    public function index()
    {
        // set $viewData somehow
        $viewData['someVar'] = "By Magic!";
        return $this->template->render("locations/index", $viewData);
    }
}

/app/Libraries/Template.php

<?php namespace App\Libraries;

class Template
{
    public function render($view, $data = [])
    {
        return view($view, $data);
    }
}

/app/Views/locations/index.php

This works as if... <strong><?= $someVar; ?></strong>

我知道我没有准确地创建您想要做的事情。但是以上内容应该可以让您到达您想去的地方。无论如何,我希望如此。

【讨论】:

    【解决方案2】:

    一开始很棘手。 但我设法成功运行它

    确保你给它正确的命名空间

    然后只需在您的控制器位置“使用”。 我没有更改 Autoload.php 上的任何内容。

    app/Libraries/Template.php

    <?php
    
    namespace App\Libraries;
    
    class Template {
        public static function render($param) {
            return 'Hello '.ucwords($param);
        }
    }
    

    正确的调用方式是将use App\Libraries\Template放在class Location extends BaseController之前

    app/Controllers/Locations.php

    <?php
    
    namespace App\Controllers;
    use App\Libraries\Template;
    
    class Locations extends BaseController {
        public function index() {
            $template = new Template();
            $renderedStuff = $template->render('World!');
            echo $renderedStuff;
        }
    }
    

    这是如何工作的? 请注意Template.php 中有一个命名空间namespace App\Libraries;,因此 CI4 将自动正确加载该库并识别“模板”类。在我看来,这是创建 CI4 库的正确方法。

    我们如何使用该库? 查看我的Locations.php 示例,然后查看此代码use App\Libraries\Template;,这就是我们所说的库。

    我们如何调用函数? 查看 index() 函数内部,这里我们使用 var $template = new Template(); 调用类模板。 然后我们用$template-&gt;render('World!');调用模板库中的render()函数。

    就这么简单。 我希望这有帮助,让我知道它是否不起作用。 :)

    【讨论】:

      【解决方案3】:

      只是一点提示,因为我的眼睛被CI_Controller 部分所吸引。

      您似乎在 CI4 中使用了 CI3 语法,至少在加载视图时,它转换为:

      $data = [
          'title' => 'Some title',
      ];
      
      echo view('news_template', $data);
      

      参见 CI3docCI4doc 的“静态页面”教程。

      【讨论】:

      • 感谢您指出这一点,我刚刚更新了代码以防止在这方面出现任何混淆。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      • 2022-12-16
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多