mercredi 27 septembre 2017

Laravel Create a package with Service Provider and Facade -- Not Working

I'm trying to build a package that uses Guzzle, it started out as just a ServiceProvider, but after some research it looked like the best way was a small custom package.

However, due to my lack of experience with Laravel, I'm running into problems getting this working. At the moment I am getting the error:

Class zoho-api does not exist

Here is the package code below, I am hoping that someone can see where I've gone wrong/missed something and help steer me in the right direction. I've enjoyed the learning experience so far, be nice to get across the line.

Package is located here: api/packages/energynet/zoho

In the src folder:

ZohoServiceProvider.php

<?php

namespace EnergyNet\Zoho;

use Illuminate\Support\ServiceProvider;

class ZohoServiceProvider extends ServiceProvider
{
    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = true;

    /**
     * Perform post-registration booting of services.
     *
     * @return void
    */
    public function boot()
    {
        if ($this->app->runningInConsole()) {
            $this->publishes([$this->configPath() => config_path('zoho.php')]);
        }
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->mergeConfigFrom($this->configPath(), 'cors');

        $this->app->bind('zoho-api', function ($app){
            $client = new Client([
                'base_uri' => config('zoho.endPoint') . 'crm/private/' . config('zoho.defaultResponse')
            ]);
            return new ZohoApi($client);
        });
    }

    protected function configPath()
    {
        return __DIR__ . '/../config/zoho.php';
    }
}

ZohoApi.php

<?php

namespace EnergyNet\Zoho;

use GuzzleHttp\Client;

class ZohoApi
{
    protected $client;

    protected $authScope;

    public function __construct(Client $client)
    {
        $this->client = $client;

        $authScope = '?authtoken=' . config('zoho.authtoken') . '=' . config('zoho.scope');
    }

    public function ZohoLeads(){

        $response = $client->request('GET', 'Leads/getRecords' . $authScope);

        dd($response);
    }
}

ZohoFacade.php

<?php

namespace EnergyNet\Zoho;

use Illuminate\Support\Facades\Facade;

class ZohoFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'zoho-api';
    }
}

In config/app.php

'providers' => [
    ...,
     EnergyNet\Zoho\ZohoServiceProvider::class,
]

'aliases' => [
    'Zoho' => EnergyNet\Zoho\ZohoFacade::class
]

In composer.json

"autoload": {
     "psr-4": {
           "App\\": "app/",
            "EnergyNet\\Zoho\\": "packages/energynet/zoho/src"
      }

Then in LeadController.php

namespace App\Api\V1\Controllers;
...
use Zoho;

class LeadController extends Controller
{
    ...

    public function qualify(Request $request)
    {
         var_dump(Zoho::ZohoLeads());

Hopefully this is clear enough, any questions, please let me know. Really hoping someone can help. Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire