Alex Kerr
Alex Kerr

Reputation: 1090

How to stop Laravel (5.4) thinking a 3rd party API call is a Laravel controller?

I'm trying to integrate a 3rd party library (this video site API: https://github.com/SproutVideo/sproutvideo-php ) with my Laravel site. But I think this is a generic question.

The video API instructs me to call it like so:

SproutVideo::$api_key = 'abcd1234';

or

$token = SproutVideo\UploadToken::create_upload_token();

I put these calls inside a function in one of my Laravel controllers. Unfortunately when this function is called, I get the following kind of error:

(for the first example call above)

FatalThrowableError
Class 'App\Http\Controllers\SproutVideo' not found

(or for the second:)

FatalThrowableError
Class 'App\Http\Controllers\SproutVideo\UploadToken' not found

(I think) I've included the Sproutvideo library at the top of the Controller file:

require '../vendor/autoload.php'; // Load SproutVideo API library

So how do I get Laravel to stop thinking the Sproutvideo API calls are Laravel controller calls, and just let them pass through to the Sproutvideo library?

Thanks

Upvotes: 0

Views: 228

Answers (1)

Fab Fuerste
Fab Fuerste

Reputation: 2231

You have to import it at the top with

use (Path To Lib in vendor directory)

Then it's namespaced and you can use it. Otherwise use the fully qualified namspace directly in the code.

Although you import it via autoload (which really should happen not in the controller class itself when using Laravel btw *), you have to tell the controller class the namespace.

Have a look here

  • The autoloader sits in /bootstrap/autoload.php and references /vendor/autoload.php again. It is called in /public/index.php. It gets called with every request anyway :)

Upvotes: 1

Related Questions