【问题标题】:Variable Not Getting Pulled From config File?变量没有从配置文件中提取?
【发布时间】:2012-02-28 22:19:16
【问题描述】:

我是 CodeIgniter 的新手,但到目前为止我很喜欢它!

我正在将 Shopify API 移植到 CodeIgniter 库,但我遇到了一个我一生都无法解决的小问题!

我收到一个未定义的变量错误,我觉得这是我遗漏的非常简单的东西,但我不明白为什么它不起作用。以下是自定义类的相关代码:

class Shopify
{

public $_api_key;
public $_shared_secret;
//public $_shops_myshopify_domain;



public function __construct ()
{
    $this->_assign_libraries();

    $this->_api_key                     = $this->config->item('api_key', 'shopify');
    $this->_shared_secret               = $this->config->item('shared_secret', 'shopify');
    //$this->_shops_myshopify_domain        =$this->config->item('shops_myshopify_domain', 'bitauth');
}

public function shopify_app_install_url($shop_domain)
{
    return "http://$shop_domain/admin/api/auth?api_key=$_api_key";
}
 public function _assign_libraries()
 {
    if($CI =& get_instance())
    {
        $this->load     = $CI->load;
        $this->config   = $CI->config;

        $this->load->config('shopify', TRUE);

        return;
    }
 }[/code]

这是我创建的配置文件中的代码:

/**
* Your shared secret
*/
$config['shared_secret'] = 'changed for posting on forum';

/**
* Your Shopify API key
*/
$config['api_key'] = 'changed for posting on forum';

这是控制器中的相关代码:

    Class shopifyPermission extends CI_Controller {
    function __construct ()
    {
        parent::__construct();

        // Load the Shopify API library
        $this->load->library('shopify.php');
        // Require url helper to perform the header redirect
        $this->load->helper('url');
    }

    function index() {
        //require 'shopify.php';

        $shop_domain = "changed.myshopify.com";

        $url = $this->shopify->shopify_app_install_url($shop_domain);

        //redirect($url);

         $data['url'] = $url;

         $this->load->view('shopifyPermission_view', $data);
    }

}

我得到的错误如下: 遇到 PHP 错误

严重性:通知

消息:未定义变量:_api_key

文件名:libraries/Shopify.php

行号:34

所以即使我有一个有效的 api 密钥,显然也没有从配置文件中提取 api 密钥?当我执行回显时,它会显示整个 URL,但 API 密钥不存在。我不知道该怎么做,不胜感激!谢谢!

【问题讨论】:

    标签: php codeigniter shopify


    【解决方案1】:

    您忘记在您的shopify_app_install_url() 中添加$this

    public function shopify_app_install_url($shop_domain)
    {
        return "http://$shop_domain/admin/api/auth?api_key={$this->_api_key}";
    }
    

    【讨论】:

    • 完美!这正是问题所在。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2022-10-06
    • 2010-12-30
    • 2011-08-09
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多