siradam's avatar

Call an api class through app

I am currently trying to use Uploadcare as a service for my website to help me to upload images. I am very new to Laravel and have am stuck on a single instruction. Following the instructions below, I do not understand by what is meant when he says, "Call the Uploadcare\Api class through app()->Uploadcare? Can someone please clarify. Clicking on the link of Uploadcare\Api takes me to a 900 line file with the following structure:

0 likes
6 replies
Snapey's avatar

We cannot see the code, and if you provided a link to the tutorial it would help.

Sounds like during boot of the class it is creating a singleton and adding it into the app container.

app() is a Laravel helper that points you to the application container where all services are registered.

You are being advised to call a method that is linked via the app container.

Is it this? https://github.com/pedzed/laravel-uploadcare

Snapey's avatar

thats funny, the package you linked to does not include

Call the Uploadcare\Api class through app()->Uploadcare

but the one I linked does.

What is the problem you have?

Snapey's avatar

try following the steps in the link I posted originally.

If it does not work, post your code here for review.

Robstar's avatar

If you're using a 3rd party service such as https://github.com/uploadcare/uploadcare-php you can do one two things:

Check for a Laravel version / wrapper for the library. Sometimes you'll get lucky. This usually takes away any thinking on your part :)

Alternatively you can make your own service provider (have a read about these on the Laravel docs) that bootstraps the code at https://github.com/uploadcare/uploadcare-PHP to create a new uploadcare client. This is basically what packages like https://github.com/InWave/laravel-uploadcare are doing for you. Specifically see https://github.com/InWave/laravel-uploadcare/blob/master/src/Inwave/LaravelUploadcare/LaravelUploadcareServiceProvider.php#L47-L59

In your original question, you refer to the tutorial stating to call the uploadcare class via app()->Uploadcare, which is equivalent to app(Uploadcare::class)->someMethod().

To understand your tutorial I would recommend you read about the container (see https://laravel.com/docs/5.6/container) and service providers (see https://laravel.com/docs/5.6/providers).

They are both bread and butter stuff you'll need to know about to be productive with Laravel.

Please or to participate in this conversation.