Welcome Guest, Not a member yet? Register   Sign In
Writing A Custom Class/Library
#1

[eluser]kaedus[/eluser]
Hey everyone, I'm new to CodeIgniter, but I'm loving it so far!

I'm in the process of porting over the Shopify API to a CodeIgniter library but I'm running into a small issue that I can't figure out for the life me!

I'm getting an undefined variable error, and I feel like it is something very simple that I am missing, but I can't understand why it isn't working. Here is the relevant code from the custom class:

Code:
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;
  }
  }

Here is the code from the config file I created:
Code:
/**
* Your shared secret
*/
$config['shared_secret'] = 'changed for posting on forum';

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

And here is the relevant code in the controller:
Code:
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);
  }
  
}

The error that I get is as follows:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: _api_key

Filename: libraries/Shopify.php

Line Number: 34

So apparently the api key is not getting pulled from the config file even though I have a valid api key? When I do an echo it shows me the whole URL but the API key is not there. I am at a loss as to what to do and would appreciate any help! Thanks!
#2

[eluser]CroNiX[/eluser]
When loading libraries, leave off the .php ($this->load->library('shopify');

In your _assign_libraries() method, you load the config and use the 2nd parameter set to TRUE, which assigns the config to a variable, but you don't assign it to a variable so it doesn't do anything.

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

Maybe you should assign it to a class variable, like $this->_config = $this->load->config('shopify', TRUE);
and then in your __construct() use $this->_config when retrieving the variables and assigning them to _api_key and _shared_secret.
#3

[eluser]kaedus[/eluser]
Ok great. I'll give that a go and let you know how it turns out! Thanks so much for the quick reply. I truly appreciate it!
#4

[eluser]kaedus[/eluser]
[quote author="CroNiX" date="1330469795"]When loading libraries, leave off the .php ($this->load->library('shopify');

In your _assign_libraries() method, you load the config and use the 2nd parameter set to TRUE, which assigns the config to a variable, but you don't assign it to a variable so it doesn't do anything.

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

Maybe you should assign it to a class variable, like $this->_config = $this->load->config('shopify', TRUE);
and then in your __construct() use $this->_config when retrieving the variables and assigning them to _api_key and _shared_secret.[/quote]

I went ahead an implemented your suggestions, so it now looks like this:
Code:
public function __construct ()
    {
     $this->_assign_libraries();
    
     $this->_api_key      = $this->_config->item('api_key', 'shopify');
     $this->_shared_secret    = $this->_config->item('shared_secret', 'shopify');
    }

And the _assign_libraries() function looks like this:
Code:
public function _assign_libraries()
  {
   if($CI =& get_instance())
   {
    $this->load  = $CI->load;
   $this->config = $CI->config;

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

   return;
  }
  }

However I am now getting this error:

Fatal error: Call to a member function item() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/CI/application/libraries/Shopify.php on line 27

where line 27 is
Code:
$this->_api_key      = $this->_config->item('api_key', 'shopify');

Any ideas? I also fixed the shopify.php issue on loading the library.
#5

[eluser]kaedus[/eluser]
Actually, I figured it out. I changed the line in shopify_app_install_url() to be as follows:
Code:
public function shopify_app_install_url($shop_domain)
{
  return "http://$shop_domain/admin/api/auth?api_key={$this->_api_key}";
}
#6

[eluser]CroNiX[/eluser]
I'd write it more like this.
Code:
class Shopify
{
  public $_api_key;
  public $_shared_secret;
  public $CI;  //to hold the CI superglobal
  //public $_shops_myshopify_domain;

  public function __construct ()
  {
     $this->_assign_libraries();  //loads the CI superglobal, and loads the config in it

     // get values from CI config
     $this->_api_key          = $this->CI->config->item('api_key', 'shopify');
     $this->_shared_secret    = $this->CI->config->item('shared_secret', 'shopify');
  }

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

  public function _assign_libraries()
  {  
    $this->CI =& get_instance();
    $this->CI->load->config('shopify', TRUE);

    return;
  }
}
#7

[eluser]kaedus[/eluser]
Can you explain the reasoning? As I said, I'm new to CodeIgniter, and I'm not even the best programmer to begin with so I want to understand the reasoning if you don't mind.
#8

[eluser]CroNiX[/eluser]
This is unnecessary:
Code:
$this->load  = $CI->load;
$this->config = $CI->config;
when you can just directly use the CI instance to do it (since you're loading it anyway).

Now, if you need to use more CI functionality in your class, you just use $this->CI from any method.
#9

[eluser]kaedus[/eluser]
Ok great. That makes sense. Thanks again for taking the time to help.
#10

[eluser]Unknown[/eluser]
Hi kaedus,

Is there any news regarding this project. I was going to do the same thing and try converting the Shopify API into a CI library but was wandering if you've already done this.

Would be happy to help if you need a hand finishing it off.




Theme © iAndrew 2016 - Forum software by © MyBB