简体   繁体   English

Codeigniter 4:使用 BaseController 加载的助手 function 在库中不可用,除非助手也在库中加载

[英]Codeigniter 4: Helper function which is loaded using BaseController is not available in library unless the helper is loaded in library too

Previously I was using Codeigniter 3 and I load all helpers, libraries using the autoload.php .以前我使用的是 Codeigniter 3,我使用autoload.php加载所有助手和库。 Now migrating to CI4 where I tried the following,现在迁移到 CI4,我尝试了以下操作,

  1. I tried loading my helper files in the BaseController.php我尝试在BaseController.php中加载我的帮助文件
  2. I tried loading the helper in __construct at my Controller.php as well.我也尝试在我的Controller.php__construct中加载助手。

I have a Library say Demo.php and function check_user_logged() .我有一个图书馆说 Demo.php 和 function check_user_logged() When I called my get_cookie() from the function, it says Call to undefined function App\Libraries\get_cookie() .当我从 function 调用我的get_cookie()时,它显示Call to undefined function App\Libraries\get_cookie()

This function check_user_logged() when it is called from a controller as,这个 function check_user_logged()从 controller 调用时,

<?php
use App\Libraries\Demo;

protected $demo;

public function __construct()
{
    helper('cookie');
    $this->demo = new Demo();
}

public function index()
{
    $this->demo->check_user_logged();
}

The Demo.php演示.php

<?php
namespace App\Libraries;
Class Demo
{
   public function check_user_logged()
   {
      print_r(get_cookie('name')); // just for simplicity printing the cookie
   }
}

Is it the only way to load the cookie helper in Demo library constructor?这是在 Demo 库构造函数中加载 cookie 助手的唯一方法吗? Or I am missing something?或者我错过了什么?

I personally prefer loading a helper in a specific function/class that needs it .我个人更喜欢在需要它的特定函数/类中加载助手。

Option 1:选项1:

Loading the cookie helper in the library's constructor.在库的构造函数中加载cookie助手。

<?php

namespace App\Libraries;
class Demo
{
    public function __construct()
    {
        helper('cookie');
    }

    public function check_user_logged()
    {
        print_r(get_cookie('name')); // just for simplicity printing the cookie
    }
}

Option 2:选项 2:

Loading the cookie helper in the library's method.在库的方法中加载cookie助手。

The helper function get_cookie() gets the cookie from the current Request object, not from Response . 助手 function get_cookie()从当前Request object 中获取 cookie,而不是从Response中获取。 This function checks the $_COOKIE array if that cookie is set and fetches it right away.这个 function 检查$_COOKIE数组是否设置了该 cookie 并立即获取它。

<?php

namespace App\Libraries;
class Demo
{
    public function check_user_logged()
    {
        helper('cookie');

        print_r(get_cookie('name')); // just for simplicity printing the cookie
    }
}

Option 3A:选项 3A:

Getting the cookie in the current Response's cookie collection .当前响应的 cookie 集合中获取 cookie。

<?php

namespace App\Libraries;
class Demo
{
    public function check_user_logged()
    {
        print_r(\Config\Services::response()->getCookie('name')); // just for simplicity printing the cookie
    }
}

Option 3B:选项 3B:

Using the cookies(...) global function.使用cookies(...)全局 function。 No need to load any helpers.无需加载任何助手。

Fetches the global CookieStore instance held by Response .获取Response持有的全局CookieStore实例。

<?php

namespace App\Libraries;
class Demo
{
    public function check_user_logged()
    {
        print_r(cookies()->get('name')); // just for simplicity printing the cookie
    }
}

Loading the generic CI4 cookie helper in the BaseController should work.在 BaseController 中加载通用 CI4 cookie助手应该可以。

protected $helpers = ['cookie','my_security_helper' ];

Can you post your original faulty code?您可以发布您的原始错误代码吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM