Httpful and Laravel, error Class 'Httpful' not found - php

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.

Related

Manually attach package to Laravel (unable to use composer due to firewall)

I can't use composer to handle my dependencies due to the corporate firewall. At the moment I'm trying to use Barry vd Heuvel's DomPDF wrapper for Laravel and tried to:
Download the zipfile from Github (master)
Updated composer.json (not sure if its needed, but did it anyway) and added "barryvdh/laravel-dompdf": "*" in the require container.
Create the folder structure: vendor/barryvdh/laravel-dompdf
Place all files from the package in there (config-folder, src-folder and the files .gitignore, composer.json and readme.md)
Add the service provider and facade in my app.php. Service provider is listed as Barryvdh\DomPDF\ServiceProvider::class and the facade is aliased like 'PDF' => Barryvdh\DomPDF\Facade::class
Ran composer dump-autoload
After refreshing the browser I'm getting Class 'Barryvdh\DomPDF\ServiceProvider' not found. I also tried to run php artisan cache:clear and php artisan dump-autoload but the last one fails over the fact it can't find Barryvdh\DomPDF\ServiceProvider.
What have I forgotten to do to make it work?
Update
I've tried the suggested answer from Wouter J and the composer.json now looks like:
..
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Barryvdh\\DomPDF\\": "vendor/barryvdh/laravel-dompdf/src"
},
..
I've verified if the composer dump-autoload had any effect but I think it had. Because the entry is now also listed in vendor/composer/autoload_psr4.php like:
return array(
// more entries
'Barryvdh\\DomPDF\\' => array($vendorDir . '/barryvdh/laravel-dompdf/src'),
'App\\' => array($baseDir . '/app'),
);
I believe at this point this is working, but the Facade isn't responding. When I try to call something like PDF::loadView(...) and I let PhpStorm import the class (vendor/barryvdh/laravel-dompdf/src/PDF.php) it throws an error I can't call the method loadView statically. According to the documentation I should be able to call it like this:
$pdf = PDF::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');
But that results in Non-static method Barryvdh\DomPDF\PDF::loadView() should not be called statically, assuming $this from incompatible context on my end.
Suggestions?
Composer autoload still doesn't know anything about how to download the package. You have to configure autoloading like this:
{
"autoload": {
"psr-4": { "Namespace\\Of\\The\\Package\\": "vendor/the/package" }
}
}
I know this question is old, but since I don't see any solution, here is mine: I had exactly the same problem. Was able to solve by running composer dumpautoload on my localhost and then copied the folder 'vendor/composer' to the shared hosting without an SSH access. Hope this will help someone in similar situation.
The problem addresses that application don't know about DomPDF\ServiceProvidor or anything related to DomPDF because barryvdh/laravel-dompdf itself depends dpmpdf/dompdf package which is still missing and does not comes with barryvdh/laravel-dompdf
the package dompdf/dompdf also depends on several extension i.e.
PHP version 5.3.0 or higher
DOM extension
GD extension
MBString extension
php-font-lib
php-svg-lib
have a look at package here https://packagist.org/packages/dompdf/dompdf download this and put in your vendor directory then update your composer dumpautoload it should be working

Laravel5 Class 'Laravel\Socialite\SocialiteServiceProvider' not found

I've added
"laravel/socialite": "~2.0"
as a composer dependency, and i've added the service provider in my config/app.php just like this
'providers' => [
// more providers
'Laravel\Socialite\SocialiteServiceProvider'
]
Then, when i access to any of my application route, i've got this Class 'Laravel\Socialite\SocialiteServiceProvider' not found exception.
Looking into my vendor/composer/autoload_psr4.php i see there's not the Socialite mapping - ive ran composer update and composer dump-autoload more than once.
What's wrong?
Did you run the composer command before adding the provider to the file? If not, this can sometimes cause issues.
I recommend removing the reference from the config file and the composer.json, then running composer request laravel/socialite 2.0. Also, just fyi, using the ::class notation in the providers listing will help if you're using a full IDE like phpstorm, as it will highlight when it can't find the class.
I have removed the reference from the config file and the composer.json, then ran composer require laravel/socialite 2.0 instead of composer request laravel/socialite 2.0.
Then add the following lines to the config file
Laravel\Socialite\SocialiteServiceProvider::class,
and
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
first of all remove the line
Laravel\Socialite\SocialiteServiceProvider::class,
and also
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
from Config\app.php and update the composer by adding
"laravel/socialite": "^2.0" and after that add these two line in Config\app.php it will work because when you add these lines before updating composer it goes to vender folder and search for the SocialiteProvider but it is not available yet so when you will update composer first and then add these lines it will work fine
At first of all, check if your socialite package is compatible with your Laravel version.
You can verify your Laravel version inside your Laravel path, for it type:
$ php artisan --version
Then, check in the socialite Git repository (link below) a compatible version with your Laravel.
https://github.com/laravel/socialite/releases
Now install the socialite package with composer require laravel/socialite x.x (where x.x is your socialite choosed version)
Lastly add the following lines in config/app.php file
inside 'providers' => [
Laravel\Socialite\SocialiteServiceProvider::class,
and almost at the end of the file, inside 'aliases' => [
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
Save the file.
first of all you need to clear the cache of your project by this code
php artisan config:cache
and then you need to enter this code in the terminal (for facebook for example)
composer require socialiteproviders/facebook
in the previous line i ended it with facebook as an example but you could change it like instagram or anything else and the error will be solved

Laravel 5.1 Package Development - loading packages dependencies in development

Hi I'm trying to develop a package in Laravel 5.1. Thanks to help here I have the basics set up.
My current problem is how to load dependencies for the package while I'm developing it.
In the packages composer.json I have added dependencies and have these installed now in a vendor folder within my packages development folder. This is not the frameworks root vendor folder.
Here's my require section of the packages composer.json:
"require": {
"illuminate/support": "~5.1",
"php" : ">=5.3.0",
"google/apiclient": "dev-master"
},
Because they are not part of the main autoload process what is the best approach to ensuring the dependencies for my package are loaded correctly from within the development folder? How do I include the autoload? I'm concerned that if I reference them to their current location/namespace that it will break when later installed as a package in another app.
in my code I have the following:
$client = new \Google_Client($config);
which gives the error:
Class 'Google_Client' not found
I can get round this by adding this dependency to the main composer.json but don't think that is the correct approach to keep the package development independent (if that makes sense)
When I developed in L4.2 there was the workbench which took care of the loading which of course no longer features in L5.1
Any help and best practice appreciated
Because they are not part of the main autoload process
I think you misunderstood how composer dependencies are managed. When in your main compose.json file you list a dependency, composer will add it to the main autoload process as well as all their dependencies, and the dependencies of their dependencies, and so on recursively.
You don't have to worry about where the dependencies are stored or how Composer will load them. Composer will automatically add them to the autoload file and all you have to do is make sure you require the composer autoload file. Once you require the composer autoload file, all the classes and functions loaded by composer will be available. Provided you required the composer autoload file all you have to do to use the classes from any of the installed packages is to make sure you address them using the proper namespace. Composer is smart enough to know where all classes are stored and how to load them (that is what psr-0, psr-4,... are for).
So if you are developing a Composer package, lets call it 'A', and you list the package 'C' as one of the dependencies of your package 'A', composer will add it to the autoload file for you. If you use another package, lets say, Laravel, which has a dependency of you package 'A', then also the package 'C' will be available within Laravel, since it is a dependency of 'A'.
I.e: If this is your composer.json file
{
"name": "foo/bar",
"require": {
"google/apiclient": "1.0.*"
}
}
This code will work
require_once __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$youtube = new Google_Service_YouTube($client);
Note I've required the composer autoload file, which seems to be your problem. When you are using Laravel, it will add that file for you.

How to include ServiceProviders in Laravel 5 manually?

I just want to ask a question. I just want to ask if there's a way to include ServiceProviders in Laravel manually? I looked in github page and I saw some laravel serviceprovider. How can I include those in my laravel file? I am new to laravel and I am studying this in our home without an Internet connection. I downloaded the HTMLServiceProvideer manually and I also tried the command:
composer require illuminate/html
Can the composer run without having an Internet Connection? If no how can I install a serviceprovider by using the master-files? Where should I place the files and what file should I need to update?
I believe this is possible but you need to manually do the work of the Composer.
So for instance lets assume want to manually install illuminate/html package.
You can follow these steps:
Add "illuminate/html": "5.*" to your composer.json although we are not using Composer for installing this package, its important we have this listed in composer.json should in case we run 'composer update' in the future our manually added package will not be deleted but rather updated.
"require":{
"laravel/framework": "5.0.*",
"illuminate/html": "5.*"
}
Manually download the package from Github and place the package inside
vendor directory which is located at the root of your laravel application
example in mylaravelapp/vendor
Add the package to mylaravelapp/vendor/composer/autoload_namespaces.php
return array(
.....
'Illuminate\\Html\\' => array($vendorDir . '/illuminate/html'),
);
Add the package to mylaravelapp/vendor/composer/autoload_namespaces.php
return array(
....
'Illuminate\\Html\\' => array($vendorDir . '/illuminate/html'),
);
Run composer dump-autoload to check for errors
Add Package service provider in config/app.php 'providers' => []
Run composer dump-autoload once more
Service Providers are added to your config/app.php
But you will need to have that package downloaded...

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

Categories