2

In EE I am trying to create a php class that will query an API. When I look at the documentation for creating your own library classes (here: https://docs.expressionengine.com/latest/development/legacy/libraries/creating-libraries.html) I see that it says this about storing the library:

Your library classes should be placed within your system/ee/user/addons/addon_name/libraries directory, as this is where ExpressionEngine will look for them.

I don't see this directory anywhere in my project. Out of exhaustion I tried creating it and made a test class (just copied the example from the documentation except with an echo in the method) and loaded it, which resulted in an error... which I was expecting.

This is in system/ee/user/addons/addon_name/libraries/Someclass.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Someclass {

    public function some_method()
    {
      echo 'test';
    }
}

With this in a template:

<?php
    ee()->load->library('someclass');
    ee()->someclass->some_method();
?>

This gives me a load error:

Error
Unable to load the requested class: someclass

At this point I'm not sure if I'm simply too tired and not understanding the documentation or if I'm in the Twilight Zone.

Can anyone clarify this for me? I would greatly appreciate it.

4

Have you created all the necessary files for your add-on? I'm not sure EE will look for your library file in an empty add-on folder.

You need to make sure you have an addon.setup.php file at least, and possibly the basic framework for a module I believe for EE to try and load any libraries from your addon. You may need to install said module as well for it to be accessible. Try that first.

https://docs.expressionengine.com/latest/development/addon-setup-php-file.html

Also, what EE version are you running? And are you actually calling your addon addon_name? I highly recommend building a bare-bones module, adding your library to it, and installing it into your EE installation, that should work.

I definitely think you can't just have a random library folder in a non-complete non-installed add-on, you need to install a module for EE to look for a library folder and files in that add-on folder.

Edit

It seems you need a little help with setting up an add-on. I will try and make things clear and hopefully that will help.

Third party add-ons live in a folder of their own, inside of this folder:

system/ee/user/addons/

For this example, I am going to call my add-on jrothafers_addon. So to start, I will create this folder:

system/ee/user/addons/jrothafers_addon

In this folder, I will create my addon.setup.php file. The link above in my answer will help you fill out that file. Next, you will want to create either a plugin or module file. In this case, I will create a plugin file:

system/ee/user/addons/jrothafers_addon/pi.jrothafers_addon.php

Please note the name of that file, as well as the name of the folder being the same. In that file, I will have this code:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Jrothafers_addon
{

    public $return_data = '';

    public function __construct()
    {
        $this->return_data = 'Hello World';
    }
}

Again, it is very important to note that the class name has a capital letter first, and the rest is exactly the same as the file name and folder name (jrothafers_addon). If you don't get this right, EE won't recognize your add-on.

After I have this setup, I can go to my ExpressionEngine control panel and visit Developer -> Add-Ons. If I scroll down, I will see my add-on listed under Third Party Add-ons. Once here, I can install my add-on.

So after all that happens, I'm hoping that you can access your library, which in my example, would live here:

system/ee/user/addons/jrothafers_addon/libraries/Someclass.php

| improve this answer | |
  • Thanks for the prompt response! I'm currently using EE version 5.2.1. I'm looking up how to create a plugin now. At (docs.expressionengine.com/latest/development/…) it goes over how to make a plugin, which is great, but it starts with creating your plugin file pi.hello_world.php but doesn't say where to put it. Once I get that I'm sure I'll be able to persist and get something working. – Shayne Apr 10 '19 at 17:45
  • 1
    I've edited my answer to try and explain the add-on structure better, hopefully that helps. – jrothafer Apr 10 '19 at 19:06
  • Thanks, friend! Before I checked back here I had actually printed out PATH_THIRD to see where other plugins were running. That brought me to the system/ee/user/addons folder that you mentioned. I'd put the pi. in there but not in its own folder. Your instruction allowed me to put everything in the right place and install the add-on. Thank you SO MUCH for taking the time to help me out! I really appreciate it. – Shayne Apr 10 '19 at 19:32
  • 1
    No problem! If you end up being able to get your library to work, I'd appreciate it if you accept my answer :) – jrothafer Apr 10 '19 at 19:35
  • 1
    Done. Thanks again. – Shayne Apr 10 '19 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.