Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rocketpanda's avatar

Using non-Laravel composer package with Laravel

I know this may be a stupid question, but here goes: I'm wondering how I can use a regular composer package, not designed for Laravel, with Laravel? I tried to find some information on this, but wasn't able to find anything that really answered the question. I'm using L5, and the package I'm trying to use is this one: https://github.com/klarna/kco_php

0 likes
9 replies
rocketpanda's avatar

I've read the docs the package, but that's not the issue. I'm just confused about the autoloading and importing of classes as I'm not too familiar with Composer (and Laravel). Also, I'm not sure what what file and class I should import to get things right. I'm new to Laravel and the Composer eco system and it's sometimes a little bit scary, so I wanted to get an overview of all this is connected before I tried it out, so to speak. I've used many Composer packages designed for Laravel. But this time, trying to use a package not designed for Laravel I just felt unsure of how I should do it, the right way. For example, when I try to import a couple of classes, like this:

use Klarna\Checkout\Klarna_Checkout_Connector;
use Klarna\Checkout\Klarna_Checkout_Order;

PhpStorm tells me there is no such namespace. It might just be me being a bit stupid, but that's why I asked the question in the first place.

bestmomo's avatar

Hello,

The simpliest way is to require your classes. For example :

require_once base_path('vendor/Klarna/checkout/src/Klarna/Checkout.php');

The cleanest way is to create a service provider.

5 likes
Alias's avatar

When someone makes a package, they code it with PSR standards. This means, when you install it with Composer, the files in that package are scanned. Composer then builds a file (based on the PSR standard) into vendor/composer. This file is included by Laravel when it "starts" up....

This allows you to use the classes anywhere in your application, whether it's specific for Laravel or not. At the top of one of your PHP classes, you could do:

use Klarna/Checkout;

class MyClass {

   public function something() {
      $checkout = new Checkout;
   }

}

If a package is Laravel specific, the developer has basically wrapped some Laravel specific stuff in there, meaning you can access them custom classes via Facades, or using a Service Provider.

Like the first post states... it's all just PHP, Laravel just makes them easier to use.

6 likes
rocketpanda's avatar

Thank you very much for your answers. I think I got a better overview of this now. I'll try to experiment a bit and see if I can get a better understanding.

rocketpanda's avatar

So, I tried to make this work, but I still didn't manage to get it to work. I tried using it at the top of the controller, but the namespace did not exist. I looked at the Klarna package, and compared it to other PSR-0 packages. Other PSR-0 packages seems to be using namespaces, but the Klarna package does not. Is this why I'm not able to use any of the classes in the package? Also, the Klarna package has a file called Checkout.php at the root of the src directory which basically just requires all the necessary files. I tried to require this file in my controller to see if that would work, but when I tried to instantiate one of classes, none where found. I find this to be a "dirty" solution anyway, and hoped to be able to use the classes at the top of the controller instead.

Ashaeron's avatar

What the PSR-0 standard does is that you can do:

<?php
require_once 'vendor/autoload.php';
// ...
$connector = Klarna_Checkout_Connector::create("shared secret");

Without having to explicitly do require_once 'somedir/Klarna/Checkout/Connector.php'; since the composer autoloader takes care of that for you.

The package is built for PHP 5.2+ which does not support proper namespaces, thus the use of the deprecated PSR-0 standard with the PEAR coding standard (which uses underscores to denote directory separators instead of slashes) instead of the PSR-4 standard.

What use does is that you "import" a class from a different namespace into the current namespace, removing the need to reference it by the fully qualified class name, so for instance use Vendor\ClassName; makes it possible to do new ClassName(); instead of new \Vendor\ClassName();.

So sadly you cannot do use Klarna_Checkout_Connector;, since it is a virtual namespace and not a real one. I think you could use class_alias to reference "Klarna_Checkout_Connector" as just "Connector" but I wouldn't recommend it.

Hopefully this clears up some of the confusion. :)

1 like
iceberg53's avatar

I know that this thread is 3 years old, but I landed here after a google search and I think what I found can be useful to others :

The precedent answers are right, and the problem faced seems to be related to autoloading process.

After installing the package via composer, you should run

composer dump-autoload

After that, you will be able to use the php classes the way you want.

There's is a more detailed answer on Stack Overflow here

2 likes

Please or to participate in this conversation.