shippo-php-client icon indicating copy to clipboard operation
shippo-php-client copied to clipboard

Laravel integration doesn't work

Open BrunoTeixeiraNavila opened this issue 7 years ago • 11 comments

Class 'App\Http\Controllers\Front\Shippo_Shipment' not found

BrunoTeixeiraNavila avatar Feb 22 '17 15:02 BrunoTeixeiraNavila

Is Shippo inside of your vendors folder of your Laravel installation?

Also, do you have require_once('vendor/autoload.php'); or require_once('/path/to/vendor/shippo/shippo-php/lib/Shippo.php'); in your file for loading Shippo?

Any additional details about your configuration would be really helpful in troubleshooting why the class isn't being found.

mootrichard avatar Feb 24 '17 18:02 mootrichard

Looks to me like your missing a backslash in front of Shippo_Shipment. This package is not namespaced, so you need to use \Shippo_Shipment::create() or add a use statement in the head of the file.

casperbakker avatar Mar 28 '17 13:03 casperbakker

Hi Casperbakker I am learning laravel and got shippo library to hands on with laravel. I couldn't understand how to integrate shippo in laravel. I am not sure how to bind.

To use the bindings, either user Composer's autoload:

require_once('vendor/autoload.php'); Or manually:

require_once('/path/to/vendor/shippo/shippo-php/lib/Shippo.php');

Please let me know Steps in easy and learners way so that i can understand. i am totally novice.

pardeep7387 avatar May 11 '17 07:05 pardeep7387

I am getting error after adding this \Shippo::setApiKey($this->app['config']['services.shippo.key']); to service.php.

ERRROR: C:\wamp64\www\shippo>php artisan serve

[Symfony\Component\Debug\Exception\FatalThrowableError] Parse error: syntax error, unexpected 'config' (T_STRING), expecting ']'

My services.php looks like this after adding line

'mailgun' => [
    'domain' => env('MAILGUN_DOMAIN'),
    'secret' => env('MAILGUN_SECRET'),
],

'ses' => [
    'key' => env('SES_KEY'),
    'secret' => env('SES_SECRET'),
    'region' => 'us-east-1',
],

'sparkpost' => [
    'secret' => env('SPARKPOST_SECRET'),
],

'stripe' => [
    'model' => App\User::class,
    'key' => env('STRIPE_KEY'),
    'secret' => env('STRIPE_SECRET'),
],

** \Shippo::setApiKey($this->app['config']['services.shippo.key']);**

];

pardeep7387 avatar May 11 '17 07:05 pardeep7387

This has nothing to do with Laravel, it is just normal PHP namespacing.

This package is recommended to use with composer, if you do that in a blank PHP project, you need the require_once('vendor/autoload.php');. (Never use the manually method.)

But, with Laravel that autoloader is already loaded by the framework. So you can just use those classes as normal, without the need for including files.

If you are getting Class 'App\Http\Controllers\Front\Shippo_Shipment' not found, it is useful to read up on using namespacing in PHP. But as an easy start: you need to include the whole namespace with a leading backslash, so \Shippo_Shipment. Otherwise it will add the current namespace you are in, in front of the class you are using. So you where probably in a class inside the App\Http\Controllers\Front namespace, so using Sippo_Shipment without a backslash will search for that class inside that namespace. Just like relative/absolute URI's.

casperbakker avatar May 11 '17 07:05 casperbakker

You can simple not add any PHP to any of the files in the config/ directory, other then things that are added to that array.

It is probably a bad idea to load Shippo on every request. But if you want that, you should add that in a ServiceProvider, or just in the code right before doing something with the Shippo client.

But this is just basic Laravel, it has nothing to do with this Shippo library. Maybe read a bit more of the Laravel docs? They are pretty good.

casperbakker avatar May 11 '17 07:05 casperbakker

Please let me know how to integrate "shippo-php-client" in laravel and how to test laravel project.

pardeep7387 avatar May 11 '17 07:05 pardeep7387

Haha, please do my work for me.

This is just basic PHP stuff. You have to figure some things out for yourself.

casperbakker avatar May 11 '17 07:05 casperbakker

Thanks Brother! #

pardeep7387 avatar May 11 '17 07:05 pardeep7387

A note on the service provider:

If he sets the defer flag on his service provider then the closure will only be evaluated when it is retrieved from the container. This will have a much smaller footprint while still letting him inject the service.

Although I think that is beyond the scope of his problem.

Nacoma avatar May 11 '17 13:05 Nacoma

I have it working fine in my Laravel project. Follow this guide:

Laravel + Shippo

packytagliaferro avatar Feb 11 '20 14:02 packytagliaferro