Autoloader does not seem to be updating (Laravel) - php

I'm new to PHP Laravel. I've added a package using composer: composer require djchen/oauth2-fitbit. It seems that my composer.json did not update the autoloader as i keep getting the following error:
Fatal error: Class 'djchen\OAuth2\Client\Provider\Fitbit' not found in xxx on line 5.
this is what line 5 looks like:
use djchen\OAuth2\Client\Provider\Fitbit;
$provider = new Fitbit([ // line 5
Here is my composer.json (necessary bits)
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "vcs",
"require": {
"php": ">=5.6.4",
"djchen/oauth2-fitbit": "^1.0",
"laravel/framework": "5.4.*",
"laravel/tinker": "~1.0"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~5.7"
},
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"djchen\\OAuth2\\Client\\": "src/" // added manually - still does not work
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/",
}
},
I've done my research and i've tried composer install, composer update and composer dump-autoload. None of these worked. Any help would be appreciated.

Let's go with the nuclear solution here, your autoload file probably is cached somewhere.
Run following commands:
php artisan clear-compiled
php artisan cache:clear
php artisan config:clear
php artisan optimize
If this doesn't work, try to manually delete all autoload files you can find and run a composer update.

Answering my own question..
I probably should have really emphasised the fact that I'm new to Laravel. I managed to get around this by simply including the autoloader:
include('vendor/autoload.php');
And also calling composer dump-autoload afterwards.

Related

How to make laravel a composer dependency for my project

My project is a Laravel implementation. I have created a composer.json file as follows:
{
"name": "company/project",
"description": "My cool project.",
"type": "project",
"license": "proprietary",
"require": {
"laravel/laravel": "8.*"
}
}
I can put this file in its own directory and run composer install. This installs version 8 of laravel/laravel and all dependencies in the vendor/ directory. But this is not a valid Laravel installation. If I then move the contents of vendor/laravel/laravel up into the main directory, copy .env.example to .env and run composer -o dump-autoload, then I have a directory structure that looks (as far as I can tell), just like a standard Laravel installation.
However, when I try to access the site from by browser, I get a 500 Server Error, with no apparent entries in the Apache or Laravel logs.
I know this is not an Apache configuration error because I can create exactly the same site using the command composer create-project laravel/laravel:8.* {directory}, and I get a function basic Laravel site.
My ultimate goal is to use composer to install my project AND Laravel, with all of it's dependencies, and get a functioning site. I am particularly interested in being able to lock all of the dependencies (including the specific version of laravel/laravel) via the composer.lock file, which will then be included with the version-controlled source of my project.
You may ask why I don't just use the recommended command to create a clean Laravel installation and just version-control the resultant composer.lock file. The reason is: That file does not contain the specific version of laravel/laravel. So when I then user the composer.json/lock files in a clean directory, the core laravel/larvel files are missing.
Any pointers as to what I am doing wrong? Or is there a simpler way to achieve my goal?
Your composer.json only requires "laravel/laravel": "8.*", but Laravel needs a lot more to function properly. When I do a composer create-project laravel/laravel it generates a composer.json with the following:
{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"php": "^7.3|^8.0",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"laravel/framework": "^8.54",
"laravel/sanctum": "^2.11",
"laravel/tinker": "^2.5"
},
"require-dev": {
"facade/ignition": "^2.5",
"fakerphp/faker": "^1.9.1",
"laravel/sail": "^1.0.1",
"mockery/mockery": "^1.4.2",
"nunomaduro/collision": "^5.0",
"phpunit/phpunit": "^9.3.3"
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover --ansi"
],
"post-update-cmd": [
"#php artisan vendor:publish --tag=laravel-assets --ansi"
],
"post-root-package-install": [
"#php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"#php artisan key:generate --ansi"
]
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
Now this does a lot more than just requiring a vendor-folder, these contain the namespace-declarations, scripts, and the requirements for a few more things.
And also note that this requires laravel/framework instead of laravel/laravel.
All in all I suggest using the full list from here and keep stripping until it breaks if you want to.
And on the subject of version pinning, you could either put the composer.lock-file in your VCS and only run composer install (that does not update if there is lock-file).
You could also remove the ^ from all the versions that you don't want to update, that way you can restrict a version with some room for updates if you want. 8.0.1 would keep at 8.0.1, but you could allow patches by specifying 8.0.* as a version-requirement.
In the end, I gave up trying to make this work. Instead, I now follow these steps to install a new site:
composer create-project --no-install --no-scripts laravel/laravel:{version} {SITE}
cd {SITE}
svn export --force {repo}/composer.json .
svn export --force {repo}/composer.lock .
composer install
svn co {repo} .
svn -R revert .
This allows me to create a site with the specific version of Laravel that I need.

Asana Installation using composer

I am trying to install the asana library through the composer.
Json:
"asana/asana": "^0.10.0" added to composer.json and {
"name": "asana/asana",
"description": "A PHP client for the Asana API",
"type": "library",
"keywords": ["asana", "client"],
"homepage": "https://github.com/Asana/php-asana",
"license": "MIT",
"require": {
"php": ">=5.4.0",
"nategood/httpful": "~0.2",
"adoy/oauth2": "^1.2.0"
},
"require-dev": {
"instaclick/php-code-sniffer": "dev-master",
"phpunit/phpunit": "^9"
},
"autoload": {
"psr-0": {
"Asana\\": "src/"
}
}
}
to composer.lock but getting error 'Package Asana/asana has no version defined.
'
As suggested by #Jeto you should not edit composer.lock manually. To install the library, you can follow the steps mentioned in official docs here.
Assuming that you are doing a fresh install, follow steps below:
Put "asana/asana" package as a dependency in your composer.json file:
{
"require": {
"asana/asana": "^0.10.0"
}
}
Now run the command composer install
composer.lock file will be automatically updated by Composer when installation succeed.
EDIT:
OR
As mentioned by #Jeto in comments, You can simply do this using a single command: composer require asana/asana:^0.10.0

Unable to figure out the series of errors by composer while installing Laravel

For last 3 days, I am trying to install laravel 5.5 in my system(windows 8.1) through composer and getting a series of errors one after other. So I am asking some questions in order to clear my doubts which are still doubted after more than 20-25 hour of research on various blogs and SO.
I have installed php 7 in c:\php.
After that, I have installed composer using composer installer and did nothing after that, like setting PATH on environment setting.
I found that I have to create a composer.json file which I have copied from Github and shown below, but I don't know where to keep this composer.json file so I created this file on C:\Users\MyPC (i.e. my home directory).
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=7.0.0",
"fideloper/proxy": "~3.3",
"laravel/framework": "5.5.*",
"laravel/tinker": "~1.0"
},
"require-dev": {
"filp/whoops": "~2.0",
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~6.0"
},
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"dont-discover": [
]
}
},
"scripts": {
"post-root-package-install": [
"#php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"#php artisan key:generate"
],
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover"
]
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
}
}
Please check the code above if I have committed any mistake.
After that I executed "composer update" command which downloaded a lot of files and placed in C:\Users\MyPC\vendor folder (I think), but throws an error at last which I can't remember.
After that I executed "composer install" command and get this error shown below.
This is my problem and I am not able to figure out what to do.
I have a couple of more doubt:
a - composer create-project --prefer-dist laravel/laravel NAME_OF_PROJECT can also be used to set up new project then why we use composer global require “laravel/installer” and then laravel new NAME_OF_PROJECT method. What are the benefits of using this method.
b - What are the steps to follow after the installation of the composer using the installer for setting up a new laravel project. What are the best practices to structure the project folder?
Please help me. I am learning laravel but don't know how to deal with this composer stuff and installing laravel as most or all of the tutorial starts with creating new project file .they don't teach how to install them.
Ok so a lot of research and try and errors I figured out a way that worked for me. I am using Windows 8.1.
I have installed php 7 in c:\php.
Note : Download php from
http://php.net/downloads.php. Download Non Thread Safe
version in zip format and extract it to the
desired folder (I extracted in C:\php).
After that I have installed composer using composer installer and did nothing after that, like setting PATH on environment setting, Installer will do it for you.
Download Composer Installer from https://getcomposer.org/download/.
After that I have downloaded laravel from github and exctrated to the desired project folder.
Download Laravel latest version from https://github.com/laravel/laravel
After that I ran "composer self-update"command.
After that I ran "composer update"command.
After that I ran "composer global require “laravel/installer”"command. This command will install Laravel Installer.
Now I can go to the project folder directory and run "laravel new myProjectName" command to create new laravel project.

How to stop ZF2 from updating while running "composer update"?

Here is my composer.json:
{
"name": "zendframework/skeleton-application",
"description": "Skeleton Application for ZF2",
"license": "BSD-3-Clause",
"keywords": [
"framework",
"zf2"
],
"homepage": "http://framework.zend.com/",
"require": {
"php": ">=5.5",
"zendframework/zendframework": "~2.5",
"facebook/php-sdk-v4" : "~5.0",
"kbariotis/feedly-api": "dev-master"
}
}
I want to run composer update but I don't want to update ZF2, just other dependencies.
If you want only a specific version to be installed see the documentation for exact If you specify the exact version you require it cannot attempt to upgrade your version of the package which "~2.5" suggests to composer is what you want to happen
So you could use for example
"require": {
"php": ">=5.5",
"zendframework/zendframework": "2.5.3",
"facebook/php-sdk-v4" : "~5.0",
"kbariotis/feedly-api": "dev-master"
}
See the documentation for EXACT
With this you can run composer update and it should not attempt to upgrade the ZF Framework
You need to run
composer update facebook/php-sdk-v4 kbariotis/feedly-api
instead.

Laravel: Class 'GuzzleHttp\Client' not found

I am trying to use Mandrill to send emails via my Laravel framework, however I am receiving the following error:
FatalErrorException in MandrillTransport.php line 114: Class
'GuzzleHttp\Client' not found
I have installed Guzzle using the following command in Terminal:
"guzzlehttp/guzzle": "~4.0"
According to Laravel's documentation I need to add "guzzlehttp/guzzle": "~4.0" to my composer.json file, but I'm not sure if where I have placed it is correct as I still see the error.
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"laravel/framework": "5.0.*",
"illuminate/html": "^5.0",
"guzzlehttp/guzzle": "~4.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"phpspec/phpspec": "~2.1"
},
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-create-project-cmd": [
"php -r \"copy('.env.example', '.env');\"",
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
}
}
Here is the list of packages my application has, notice that guzzle has a different version: 4.2.3 which i've also tried updating to but still get the same error.
Open up your terminal at the root of your project and enter
composer require guzzlehttp/guzzle
It worked for the mailgun API. For some reason, the suggested method at the laravel's mail doc. You may install this package to your project by adding the following line to your composer.json file
"guzzlehttp/guzzle": "~5.3|~6.0"
doesn't make the composer download the Guzzle source codes. By the way, I didn't find out what | means in determining the version. This command just downloads the PSR code.
In this moment, the solution may work. However, be aware of compatibility issues. Because the command would install the latest stable version, not the suitable one.
If you're using Laravel when you run into this error, just run:
composer require guzzlehttp/guzzle
And try again.
After updating your composer.json file you need to run the update command to resolve and install your dependencies:
composer update
or, if composer isn't in your path:
php composer.phar update
Did you try :
artisan clear-compiled
or if artisan is not available try to remove compiled.php if exist (in vendor directory) and launch composer dumpautoload
I had the same issue.
I used an old version in order to work.
It's not working anymore since version 4.
It works on version 3.8.1
So you can add "guzzlehttp/guzzle": "~3" to works
simple in my case add "guzzlehttp/guzzle": "^6.3" in composer.json
require object as mentioned below
"require": {
"php": ">=7.0.0",
"ext-gd": "*",
"barryvdh/laravel-cors": "^0.11.2",
"barryvdh/laravel-dompdf": "^0.8.1",
"dingo/api": "2.0.0-alpha1",
"doctrine/dbal": "^2.6",
"fideloper/proxy": "~3.3",
"guzzlehttp/guzzle": "^6.3",
"intervention/image": "^2.4",
"laravel/framework": "5.5.*",
"laravel/tinker": "~1.0",
"league/flysystem-aws-s3-v3": "~1.0",
"predis/predis": "^1.1",
"tymon/jwt-auth": "dev-develop"
},
than run composer update in project root using terminal than its working fine.
I am oblivious to why this worked for me, I saw this in a forum somewhere
I just added this captcha generator to the composer.json file
"mews/captcha": "~2.0.",
added it to all the require package
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.1.*",
"laravelcollective/html": "5.1.*",
"laracasts/flash": "~1.3",
"mews/captcha": "~2.0.",
"guzzlehttp/guzzle": "~4.0"
},
If someone knows why this worked it would really scratch the itch in my brain.
I got this error when I tried running my code outside of the Laravel framework while testing in a stand alone file. It worked for me when I moved it inside a controller.
You can solve this problem to add "mews/captcha": "1.0.1" and "guzzlehttp/guzzle": "~4.0" to your composer.json file. And then you need run composer update command on your terminal.
I have tried on Laravel 4. It works for me.
Try updating your composer to latest version might end sorting your problem. This worked out for me. Ubuntu/Linux users use this tutorial in Digital ocean

Categories