CristalTeam / php-api-wrapper

:rainbow: Work with APIs like with Laravel Eloquent or Doctrine (no longer a dream)
MIT License
117 stars 32 forks source link

Example not working with Laravel 9 #45

Closed italo1983 closed 1 year ago

italo1983 commented 1 year ago

I'm unable to make the example code working with Laravel 9. Adding MyModel::setApi($this->app->make(CustomWrapper::class));to the AppServiceProvider boot function generate the error: Class "App\MyModel" not found

I've created the MyModel.php file inside the app/Http/Models folder with the example code:

<?php

namespace App;

use Cristal\ApiWrapper\Bridges\Laravel\Model;

class MyModel extends Model
{
    // Nothing here
}

and also a wrapper inside app/Wrappers folder:

<?php

namespace App;

use Cristal\ApiWrapper\Api;

class CustomWrapper extends Api
{
    // Nothing here...
}

and this is the content of my provider:

<?php

namespace App\Providers;

use App\MyModel;
use App\CustomWrapper;
use Illuminate\Support\ServiceProvider;
use Cristal\ApiWrapper\Transports\Basic;
use Curl\Curl;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(): void
    {
        MyModel::setApi($this->app->make(CustomWrapper::class));
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(CustomWrapper::class, function(){
            $transport = new Basic(
                'username', 
                'password', 
                'http://api.example.com/v1/', 
                $this->app->make(Curl::class)
            );
            return new CustomWrapper($transport);
        });
    }

}
TZK- commented 1 year ago

Your classes are not following PSR-4 so composer cannot autoload these in your project.

That must be why you have PHP errors. It is not related to php-api-wrapper.