I'm a little new to the concept of Namespaces in PHP/Drupal, and I'm trying to figure out where I am going wrong here.
I've got this custom PHP library under the /libraries folder - let's call it ilib
. And inside of ilib, there is another directory called lms
.
Anyway, I've written a little module that will connect with ilib (or I am attempting to get it to connect with it), called ilib_connect.
I am trying to do a test function inside a custom page with a controller for the page, as such:
namespace Drupal\ilib_connect\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use ilib\lms;
/**
* Controller routines for page example routes.
*/
class iLibController extends ControllerBase {
function ilib_connect_test() {
$lms = new \ilib\lms;
\Drupal::logger("ilib_connect")->notice("<pre>" . $lms . "</pre>");
$lms->AddLMSActivity();
return "fake markup";
}
}
However I am getting the error:
Error: Class 'ilib\lms' not found in Drupal\ilib_connect\Controller\iLibController->ilib_connect_test() (line 17 of modules/custom/ilib_connect/src/Controller/iLibController.php).
So I'm sorta having trouble pulling any classes from ilib into scope.
How exactly do I pull a third party library into scope in a situation like this? What about in a .module file?
What exactly am I misunderstanding here?
EDIT: Oh and just to be clear, this is how I am registering the custom library with Composer:
"autoload": {
"psr-4": {"ilib\\": "web/libraries/ilib"}
}
EDIT 2 - question to Clive:
Ah, so say I've got a file named AddLMSActivity.php inside of libraries/ilib/lms, setup like this:
namespace ilib\lms;
class AddLMSActivity
{
}
How would I access it exactly?
ilib\lms\AddLMSActivity?
use
a directory, just a class, function, trait or const