Autoloading a custom package in Laravel7 - php

Sorry if my question looks so basic...
Am trying to use the following package in a fresh Laravel7 installation.
https://github.com/phpclassic/php-shopify
As like they mentioned i used 'composer require phpclassic/php-shopify' command to install this package. It is done successfully and i see it under 'Vendor/phpclassic/php-shopify/' folder. Now i want to use it in my controller...
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PHPShopify\ShopifySDK;
class ShopifyConnectionController extends Controller
{
function index(Request $request){
$config = array(
'ShopUrl' => config('app.shopify_app_url'),
'ApiKey' => config('app.shopify_app_api_key'),
'SharedSecret' => config('app.shopify_app_api_secret'),
);
PHPShopify\ShopifySDK::config($config);
dd($request->all());
}
}
?>
Am getting following error...
Error
Class 'App\Http\Controllers\PHPShopify\ShopifySDK' not found
Now i see that the package is not auto loaded. I tried using "composer dump-autoload" command and tried adding the folder path in autoload section of composer.json file, etc... am keep getting the same error... I also noticed the "composer dump-autoload" command showing...
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
etc...
but not the package i installed. Those discovered packages also in the same vendor folder. Then why is my package is not being discovered by Composer autoload when i run that command ? Someone kindly help.

You are simply missing a backslash. This tells the autoloader that the file you are looking for is not in the namespace your controller resides in.
\PHPShopify\ShopifySDK::config($config);
And since you've already imported ShopifySDK there's no need for a FQCN. Just use the class:
ShopifySDK::config($config);

Related

How to troubleshoot Composer autoload in Codeigniter?

I am new to using Composer in Codeigniter and have read all I can to try and fix my issue, but are coming up short. The autoloader seems to to be autoloading? So here is where I am stuck:
I have installed a composer.
Required the MPDF package using composer require pdf/mpdf
I did that from the root of my project, so the vendor folder, as well as the composer.json file is on the root of my project
In my config/config.php file, I changed the composer_autoload value to
$config['composer_autoload'] = FCPATH . "/vendor/autoload.php";
Then I created a simple function in my default controller like this:
function m_pdf() {
$mpdf = new mPDF();
$mpdf->WriteHTML('Hello World');
$mpdf->Output();
}
But when I run it, I get an error: Message: Class 'mPDF' not found
I have tried moving everything to the application folder, and change the value in the config file to TRUE, I have tried calling the autoloader from the function itself, but no luck.
I feel I am missing something obvious. Maybe some dependencies. As I said, I am new to use the composer. There were some suggestions on the web about running php composer.phar but I get an error when I try and run that. Could that be it?
1) in namespace => use correct Mpdf class
If still not help => composer dump-autoload(reconfigure your composer classMap)
If dump-autoload not help =>
remove vendor directory
composer install

Fatal error: Interface 'Psr\Container\ContainerInterface' not found in

Two weeks ago I didn't have any problem, but today this appears when I put composer install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating optimized autoload files
Illuminate\Foundation\ComposerScripts::postAutoloadDump
PHP Fatal error: Interface 'Psr\Container\ContainerInterface' not found in /opt/lampp/htdocs/nuevodirectorio/ClinicaLaravel/vendor/laravel/framework/src/Illuminate/Contracts/Container/Container.php on line 8
Fatal error: Interface 'Psr\Container\ContainerInterface' not found in /opt/lampp/htdocs/nuevodirectorio/ClinicaLaravel/vendor/laravel/framework/src/Illuminate/Contracts/Container/Container.php on line 8
Here is part of the code of Container.php:
<?php
namespace Illuminate\Container;
use Closure;
use Exception;
use ArrayAccess;
use LogicException;
use ReflectionClass;
use ReflectionParameter;
use Illuminate\Support\Arr;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Container\Container as ContainerContract;
class Container implements ArrayAccess, ContainerContract
{
...
}
At my job we had the same problem. In our case, there was a psr/container file causing a conflict, but this file was in the composer global cache, so removing the vendor folder alone didn't help us. First we needed to clear the composer cache using this command:
composer clearcache
and then we removed the vendor folder and ran composer install, everything went fine. I hope this solution helps someone.
I had the same issue.
Still investigating, but what I discovered so far is that, running composer install --no-dev [...], I have the package psr/container in vendor/psr/container in vendors, but 'Psr\\Container\\' => array($vendorDir . '/psr/container/src'), line missing in vendor/composer/autoload_psr4.php.
For the time being, I fixed it including dev dependencies, until I figure out the real fix.

Laravel won't load class

I'm trying to use the following library, this.
It all seems pretty use but doesn't seem to work for me. I've added the stuff the the composer.json and I also updated the app.php. Then I ran composer dump-autoload in the laravel directory.
I still seem to be getting the same error..
FatalErrorException in ProviderRepository.php line 146:
Class 'Invisnik\LaravelSteamAuth\SteamServiceProvider' not found
Try to install this package by this command:
composer require invisnik/laravel-steam-auth
You will install the latest version of current package and update your composer.json, composer.lock and autoload files.

Class Carbon\Carbon not found

I recently added a package to my Laravel 4 site and now anything that uses Eloquent (or at least Eloquent with any reference to date/time) is showing a 500 error that states:
Class 'Carbon\Carbon' Not Found.
I tried running
composer install
composer update
composer dump-autoload
Yes, it can work as #oli-folkerd 's answer. However, as seen in Laracasts (Laravel 5 Fundamentals series Video 10 "forms" min 16:55), almost in top of your ControllerClass php file, just add the following (or import the class if your php editor allows you do so):
use Carbon\Carbon;
Now you can simply use Carbon
$input['published_at'] = Carbon::now();
without having to add Carbon\
you need to add the line:
'Carbon' => 'Carbon\Carbon',
to the bottom of the 'aliases' array in app/config/app.php this will make the carbon library available everywhere in laravel.
You this class in controller of Laravel.
use Carbon\Carbon;
then you simply define the carbon command for print the current date
$date = Carbon::now();
For all updated version you just need to
use Carbon\Carbon;
and for the global use, you can add this in app.php
'Carbon' => 'Carbon\Carbon',
Not saying this is work for you, but those are steps that usually fix Laravel, when the problem is not on your source code, of course:
cd /your/application/dir
rm bootstrap/compiled.php
rm -rf vendor
composer install --no-dev
My problem solved by just requiring nesbot/carbon just do this:
composer require nesbot/carbon
For Laravel 8.x Please add
'Carbon' => Illuminate\Support\Carbon::class,
in your app/config/app.php
under aliases
or if you only want to use it in your controller
then please add
use Illuminate\Support\Carbon;
I had this problem once when I updated a project from gitlab. The below command worked for me.
composer dump-autoload
Some times specifying prefer-dist prefixed by “--” (aka “bare double dash”) at the end or suffixing at the end of create-project also matters while installing...
The below command was working fine in laravel 5.5 without getting an error
composer create-project laravel/laravel blog "5.5.*" --prefer-dist
But when I was about to begin installing Laravel 5.6 with this below command
composer create-project laravel/laravel blog --prefer-dist
I used to get
Whoops\Exception\ErrorException : Class 'Carbon\Carbon' not found
After referring to the official Installation Documentation
composer create-project --prefer-dist laravel/laravel blog
After executing the above command there were no exceptions raised, therefore installation succeeded, thereby generating a base64 hash key

Httpful and Laravel, error Class 'Httpful' not found

I've installed Httpful as described with Composer adding to composer.json the following:
{
"require": {
"nategood/httpful": "*"
}
}
I'm using Laravel 4 so i ran composer install
I've checked if the plugin is installed and is there, in fact under the vendor folder of laravel i can find it. But i keep getting the following error:
ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class 'Httpful' not found'
I'm missing some steps?
Thank you in advance
The class related to this package name conflicts with Laravel's Response class, so this is how you use it in Laravel:
$url = "http://api.openweathermap.org/data/2.5/weather?lat=22.569719&lon=88.36972";
$response = \Httpful\Request::get($url)->send();
echo $response->body->name."<br>";
echo $response->body->weather[0]->description;
The class is not Httpful, but Response, so you have to add the correct Namespace so it doesn't get confused by Laravel's Response class.
EDIT:
In Laravel you can create aliases for classes. Edit your app/config/app.php and in the aliases array, add:
'aliases' => array(
....
'Httpful' => '\Httpful\Request',
),
And you'll be able to use it this way:
$response = Httpful::get($url)->send();
You may need to do composer update rather than composer install.
The difference being install will go by whatever is in your composer.lock file, while update will pick up any new dependencies added into your composer.json file, and then write those to your composer.lock file.
Note: install should generally be used in production to get the latest from your composer.lock file, while update is generally a command used in development to get your updated dependencies.
This also means you should add composer.lock to your git repository, even tho it's in your .gitignore file by default in a new Laravel project.

Categories