composer autoload namespaces missing? - composer-php

I'm developing on a project which uses Monolog, which requires Psr/log.
When I use Monolog by autoload, it then complains about missing Psr\Log\LoggerInterface.
So I look through the composer generated autoload_namespaces.php, and I couldn't find Psr being registered.
Here's the contents of my vendor/autoload_namespaces.php
return array(
'Symfony\\Component\\Process' => $vendorDir . '/symfony/process/',
'Monolog' => $vendorDir . '/monolog/monolog/src/',
'Imagine' => $vendorDir . '/imagine/Imagine/lib/',
'Gedmo' => $vendorDir . '/gedmo/doctrine-extensions/lib/',
'Gaufrette' => $vendorDir . '/knplabs/gaufrette/src/',
'Evenement' => $vendorDir . '/evenement/evenement/src',
'Doctrine\\ORM' => $vendorDir . '/doctrine/orm/lib/',
'Doctrine\\DBAL' => $vendorDir . '/doctrine/dbal/lib/',
'Doctrine\\Common' => $vendorDir . '/doctrine/common/lib/',
'Assetic' => $vendorDir . '/kriswallsmith/assetic/src/',
'Analog' => $vendorDir . '/analog/analog/lib/',
);
Is composer supposed to register namespaces for all projects which has {autoload} defined in their composer.json files? (I checked both Monolog and Psr/Log, they both have {autoload} defined.)
Or am I getting the wrong idea about composer?

You need to explicitly add
"psr/log": "1.0.0"
in your composer.json require block. The project monolog/monolog doesn't require psr/log [you can verify that in monolog's composer.json file].
Then run composer update to update your autoloader file.
[UPDATE] Made a mistake.
I am using monolog 1.2.* and it doesn't require psr/log. After monolog 1.3.*, it does requires psr/log. In that case, composer should load psr/log namespace in vendor/autoload_namespaces.php.
For example, I just updated my composer.json to have
"monolog/monolog": "1.3.*",
in the require block. Then run composer update monolog/monolog [you can run this command again to see if it fixes your problem]
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing psr/log (1.0.0)
Loading from cache
- Removing monolog/monolog (1.2.1)
- Installing monolog/monolog (1.3.1)
Downloading: connection...
Downloading: 100%
Then in my vendor/composer/autoload_namespaces.php, I found this proper entry:
'Psr\\Log\\' => $vendorDir . '/psr/log',
This should work. Sometimes I also just run
composer dump-autoload
to re-generate vendor/composer/autoload_namespaces.php file again.

I was actually confused by the number of autoload_namespaces.php there are.
The one I posted in my question was in vendor/, then I found another one in vendor/.composer/, and finally found the CORRECT one in vendor/composer/.
Sorry for the answering my own (stupid) question but it's worth mentioning in case some other developers have the same trouble.
And thanks to #Chuan Ma for the answer.

Composer registers classes based on the autoloadin the project's composer.json file, e.g.
{
"autoload": {
"classmap": [
"path/to/FirstClass.php",
"path/to/SecondClass.php"
]
}
}
Normally composer update will automatically regenerate autoload_namespaces.php lists all namespaces which your application can use (unless you're using multiple autoloaders).
If the namespaces are missing, here is the command to update the composer autoloader directly:
composer dump-autoload -o
Then you need to make sure you include the autoloader at the top of our scripts:
<?php
require_once __DIR__ . '/vendor/autoload.php';
See: How to directly autoload classes with Composer?
If it's still failing, double check the syntax in the failing project's composer.json file (especially autoload section). To debug, try running composer with -vvv or under XDebug.

Related

Class 'Yajra\DataTables\DatatablesServiceProvider' not found

I've developed Laravel Project in my local computer.
I used Yajra Pakagebox for using bootstrap datatables on it.
Like this :
composer require yajra/laravel-datatables-oracle
php artisan vendor:publish
Then I pushed them all into Hosting Server but it displays errors like below.
(1/1) FatalThrowableError
Class 'Yajra\DataTables\DatatablesServiceProvider' not found
in ProviderRepository.php (line 208)
at ProviderRepository->createProvider('Yajra\\DataTables\\DatatablesServiceProvider')
in ProviderRepository.php (line 144)
at ProviderRepository->compileManifest(array('Illuminate\\Auth\\AuthServiceProvider', 'Illuminate\\Broadcasting\\BroadcastServiceProvider', 'Illuminate\\Bus\\BusServiceProvider', 'Illuminate\\Cache\\CacheServiceProvider', 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider', 'Illuminate\\Cookie\\CookieServiceProvider', 'Illuminate\\Database\\DatabaseServiceProvider', 'Illuminate\\Encryption\\EncryptionServiceProvider', 'Illuminate\\Filesystem\\FilesystemServiceProvider', 'Illuminate\\Foundation\\Providers\\FoundationServiceProvider', 'Illuminate\\Hashing\\HashServiceProvider', 'Illuminate\\Mail\\MailServiceProvider', 'Illuminate\\Notifications\\NotificationServiceProvider', 'Illuminate\\Pagination\\PaginationServiceProvider', 'Illuminate\\Pipeline\\PipelineServiceProvider', 'Illuminate\\Queue\\QueueServiceProvider', 'Illuminate\\Redis\\RedisServiceProvider', 'Illuminate\\Auth\\Passwords\\PasswordResetServiceProvider', 'Illuminate\\Session\\SessionServiceProvider', 'Illuminate\\Translation\\TranslationServiceProvider', 'Illuminate\\Validation\\ValidationServiceProvider', 'Illuminate\\View\\ViewServiceProvider', 'Yajra\\DataTables\\DatatablesServiceProvider', 'Laravel\\Tinker\\TinkerServiceProvider', 'App\\Providers\\AppServiceProvider', 'App\\Providers\\AuthServiceProvider', 'App\\Providers\\EventServiceProvider', 'App\\Providers\\RouteServiceProvider'))
in ProviderRepository.php (line 61)
The important thing is I can't execute any command on Hosting Server because it is Shared Hosting Server.
I saw many articles for solving this problem but they are all using "artisan" and "composer" command.
But I can't use this command at all.
I can only upload the source code to server with FTP.
Depending on what version of DataTables you are using, it may be simple capitalization issue. After version 8 you should use:
Yajra\DataTables\DataTablesServiceProvider
Before version 8 use:
Yajra\Datatables\DatatablesServiceProvider
Upgrade notes reference: https://yajrabox.com/docs/laravel-datatables/master/upgrade#namespace
Please run below command and try:
composer update
composer dump-autoload
php artisan config:cache
php artisan cache:clear
It is working for v#8.3
Yajra\DataTables\DataTablesServiceProvider::class,
'Datatables' => Yajra\DataTables\Facades\DataTables::class,
Please add this to the config/app.php file.
The first line goes under the "Package Service Providers" section
and the second line goes under the "Class Aliases" section
replace Datatables with DataTables
Re install with the plugin along with the buttons plugin and now it's working.
composer require yajra/laravel-datatables-buttons:^3.0
In the project's folder
rm -R vendor/
rm -R bootstrap/cache
mkdir bootstrap/cache
chmod -R 777 bootstrap/*
if your laravel version => 5.4
composer require yajra/laravel-datatables-oracle:"~8.0"
if your laravel version => 5.8
composer require yajra/laravel-datatables-oracle:"~9.0"
#config/app.php
'providers' => [
...,
Yajra\DataTables\DataTablesServiceProvider::class,
]
'aliases' => [
...,
'DataTables' => Yajra\DataTables\Facades\DataTables::class,
]
composer dumpautoload
composer install
It works for me.
Source: https://github.com/yajra/laravel-datatables[https://github.com/yajra/laravel-datatables][1]
REASON ITS NOT WORKING IS:
you installed the library. and added it in config/app.php in providers array.
don't forget to run
php artisan vendor:publish
after that.
Try below steps to resolve this issue:
Use composer show to check which version of packages you are using.
Delete all files under bootstrap/cache folder
Delete vendor folder and reinstall all packages using composer install.
all the files you know you're changing to ftp (migrations config controller...)
and replaced local files to server with ftp
/composer.json
/composer.lock
/bootstrap/*
/storage/framework/cache/*
/storage/framework/views/*
/vendor/composer/*
/vendor/autoload.php
If the problem persists, I'm need to relay the version of the Laravel.
Tested with
php artisan --version
Laravel Framework 5.4.19
In your [config/app.php] file, edit the aliases array. Change it from
'Datatables' => Yajra\Datatables\Facades\Datatables::class
to
'Datatables' => Yajra\Datatables\DatatablesServiceProvider::class

Add package Syllable to Laravel 5.3 with autoloader not work

I need use a package Syllable on my project.
Install with composer, and class to autoload on my composer.json but get errors.
$ composer require vanderlee/syllable
Edit composer.json
"autoload": {
"classmap": [
"database",
"vendor/vanderlee/syllable/classes"
],
Dump autoload
$ composer dump-autoload
Now try with tinker
php artisan tinker
>>> namespace Abkrim\Setdart;
=> null
>>>
>>> $syllable = new \Syllable('en-us');
=> Syllable {#670}
>>> echo $syllable->hyphenateText('Provide a plethora of paragraphs');
PHP warning: file_put_contents(/home/vagrant/setdart/vendor/vanderlee/syllable/classes/../cache/syllable.en-us.json): failed to open stream: No such file or directory in /home/vagrant/setdart/vendor/vanderlee/syllable/classes/Syllable_Cache_FileAbstract.php on line 43
>>>
I know that any steep it's wrong. Which ?
Faced same error: you just have to set the right path to cache folder for Syllable.
From doc:
// Set the directory where Syllable can store cache files
$syllable->getCache()->setPath(__DIR__ . '/cache');
With setPath() set an existing folder in your laravel app where Syllable can store cache files.

Laravel 5.1 PHPexcel install error

I want to add class PHPExcel to my project. I use composer to add PHPExcel. I tried some command line.
php composer.phar update
or
php composer.phar require phpexcel/phpexcel
But, i alway receive error look like image:
at here. And maatwebsite/excel, too.
What is wrong with my project? Please help me!
First open the file composer.json
and find require object/array and add line
"require": {
"maatwebsite/excel": "~2.1.0"
},
and composer update so composer update is completed
and open the file config/app.php
and find providers object/array and add line
'providers' => [
/*
* Application Service Providers...
*/
'Maatwebsite\Excel\ExcelServiceProvider'
],
and find aliases object/array and add line
'aliases' => [
'Excel' => Maatwebsite\Excel\Facades\Excel::class
]
Remove this line:
"jrenton/laravel-scaffold": "dev-master"
and run composer update again.
jrenton/laravel-scaffold needs "fzaninotto/faker": "1.3.0", which I guess, was used in lower versions of Laravel

Can not start laravel after trying to install library

I have web-app on Laravel and I tried to update yajra/datatables to last version, so it needed
'Maatwebsite\Excel\ExcelServiceProvider'
I tried composer update, so it didn't help.
Now I removed this line from composer.json
But on calling php artisan clear-compiled
It shows me an error:
PHP Fatal error: Class 'Maatwebsite\Excel\ExcelServiceProvider' not found in /var/www/html/talimger.xyz/vendor/laravel/framework/src/Illuminate/Foundation/Application.php on line 575
Show where I should remove this Maatwebsite files
`
The instructions at https://github.com/Maatwebsite/Laravel-Excel tell you what to do, but not how to do it. The assumption is that you already know a bit about composer.
Here's the 'how to do it'...
1) Remove the service provider entries you made and delete any lines you added to composer and then type the following:
composer require "maatwebsite/excel"
2) After updating composer, add the ServiceProvider to the providers array in config/app.php
Laravel 5.1:
'Maatwebsite\Excel\ExcelServiceProvider',
Laravel 5.2:
Maatwebsite\Excel\ExcelServiceProvider::class,
3) You can use the facade for shorter code. Add this to your aliases:
Laravel 5.1:
'Excel' => 'Maatwebsite\Excel\Facades\Excel',
Laravel 5.2:
'Excel' => Maatwebsite\Excel\Facades\Excel,::class
Comment out 'Maatwebsite\Excel\ExcelServiceProvider' from the providers array in app.php under config then run composer update when you are done then your uncomment it.

Laravel 5 render() pagination error

->render() work fine.
I run: composer require illuminate/html
set config/app.php
add providers
'Illuminate\View\ViewServiceProvider',
'Illuminate\Html\HtmlServiceProvider',
add aliases
'Form' => 'Illuminate\Html\FormFacade',
'Html' => 'Illuminate\Html\HtmlFacade',
now, ->render() do not work, I try ->render() with {!! !!} and <?php ?>
Whoops, looks like something went wrong.
1/1
FatalErrorException in LengthAwarePaginator.php line 126:
Access to undeclared static property: Illuminate\Pagination\LengthAwarePaginator::$presenterResolver
1. in LengthAwarePaginator.php line 126
2. at HandleExceptions->fatalExceptionFromError(array('type' => '1', 'message' => 'Access to undeclared static property: Illuminate\Pagination\LengthAwarePaginator::$presenterResolver', 'file' => 'C:\wamp\www\laravel\vendor\laravel\framework\src\Illuminate\Pagination\LengthAwarePaginator.php', 'line' => '126')) in compiled.php line 1721
3. at HandleExceptions->handleShutdown()
Check your local and remote Laravel version by using artisan -v | grep version.
In my case, they were different. I had a hard time updating it to match my local though. I didn't catch it because composer update results to Nothing to install or update even if I had cleared composer cache by composer clear-cache and reinstalling all the dependencies in the vendor folder.
I ended up deleting the whole project directory and cloning from source.
Something is still out of date...
check version as suggested using:
php artisan -V
and compare with https://github.com/laravel/laravel
In my situation i was need to delete: compiled.php in storage dir
then run composer update -vvv
and ... error disrepair :-)
The $compiledPath variable has been changed in the 5.0.16 release.
To solve your problem, you need to update $compiledPath variable in your bootstrap/autoload.php file to:
$compiledPath = __DIR__.'/../vendor/compiled.php';

Resources