I have a project, and I need some packages, so I reorganized the project to use PSR-4.
Here's my composer.json:
{
"name": "me/production",
"type": "project",
"authors": [
{
"name": "Me",
"email": "me#me.com"
}
],
"config": {
"platform": {
"php": "5.6.1"
}
},
"autoload": {
"psr-4": {
"API\\": "api/"
}
},
"require": {
"nesbot/carbon": "^2.25"
},
}
it doesn't work in my scripts. But, here's the real kicker: I do require_once __DIR__ . '/vendor/autoload.php'; in my php console, and it doesn't work either. I have triple and quadruple checked the path, the autoload is there.
What do I mean by "doesn't work"? the autoload requires successfully. It allows me to use libraries. BUT actual instantiations result in a
Class not found error.
Not only for my classes in the API namespace, which are namespaced in the top of the file and are exactly in the folder they're suppose to be, but I also CANNOT instantiate Carbon. I can use Carbon\Carbon but any attempt to instantiate will fail.
Interestingly, instantiation of \Carbon\Carbon directly does not fail.
What's going on? This is weird.
Thank you in advance.
EDIT: I tried redumping the autoloader, I also tried removing the vendor folder and reinstalling. All to no avail.
EDIT: May be worth mentioning I downgraded carbon to ^1.21 because carbon 2 doesnt support php 5.6. But it still doesn't work.
EDIT: It happens with my API namespace as well, here's an example using my implementation of the Instagram API:
use \API\Insta;
php > var_dump(new Insta);
PHP Fatal error: Class 'Insta' not found in php shell code on line 1
php > var_dump(new \API\Insta);
object(API\Insta)#3 (1) {
["ig_token"]=>
string(51) "A_VALID_TOKEN"
}
php >
EDIT: The problem solved itself, it has now mutated into one I care very little about: I can use everything but not in the php console. I am not sure what fixed it.
As you found out, nesbot/carbon, version 2.25 requires at least PHP v7.1.8.
Just having a use statement doesn't check anything, instead it creates a local alias that can be used. If you try to use something that does not exist, only then would it fail.
Please clarify that you have the following directory structure:
./composer.json
./composer.lock
./Api/Insta.php
and in the file Api/Insta.php:
namespace API; // this is your top namespace name
use Carbon/Carbon; // etc
class Insta { ... }
Having the namespace different to the directory name can lead to confusion.
There will also be the index.php/front-controller that pulls in the vendor/autoload.php file and presumably runs the framework.
Related
I have been beating my head for a couple hours trying to figure out why the autoload is not working for "Authentication\auth()". The "dBase\db()" class is loading just fine, but i'm getting:
Error: Class 'Authentication\auth' not found in
/var/htdocs/dev/test.php on line 8
when calling test.php.
Root composer.json -
"require": {
"geeshoe/dbClass": "dev-develop",
"geeshoe/authClass": "dev-master"
},
"autoload": {
"psr-4": {
"dBase\\": "vendor/geeshoe/dbclass/",
"Authentication\\": "vendor/geeshoe/authClass/"
}
}
authClass.php header -
<?php
namespace Authentication;
use dBase\db;
class auth extends db
{
test.php -
if (file_exists("vendor/autoload.php")) {
require "vendor/autoload.php";
} else {
echo "Dam.. Something went wrong!";
}
$test = new \dBase\db();
$var = new \Authentication\auth();
If someone could point out the obvious to me, that would be great. On a side note, autoload is not specified in the authClass->composer.json file for testing purposes.
The problem here is that in fact you don't use PSR-4. In PSR-4 class name should match file name. For db class its fine because db class is located in db.php file, but auth class is located in authClass.php file and that's the problem. You should update file name to auth.php
You might need to run:
composer dump-autoload
Also keep in mind in real packages, one vendor package has one namespace, so you don't create multiple namespaces for single package but only single
Just recently we decided to upgrade from symfony 2.8 too 3.3 with this being noted I felt it was best to start a fresh symfony project and just pull over some parts of the project and change the key needed items that changed over this major version upgrade. I have worked out most of the issues however currently I get this meaning that I may have more issues that I suspect. here is the current one:
C:\xampp\htdocs\Fresh_Api>php bin/console server:run PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to lo ad class "CommandListener" from namespace "Directive\MultiTenant\MasterBundle\EventListener". Did you forget a "use" statement for another namespace? in C:\xampp\htdocs\Fresh_Api\var\cache\dev\a ppDevDebugProjectContainer.php:580 Stack trace:
#0 C:\xampp\htdocs\Fresh_Api\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Contai ner.php(331): appDevDebugProjectContainer->getAppBundle_CommandListenerService()
#1 C:\xampp\htdocs\Fresh_Api\var\cache\dev\appDevDebugProjectContainer.php(965): Symfony\Component\D ependencyInjection\Container->get('app_bundle.comm...')
#2 C:\xampp\htdocs\Fresh_Api\vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\EventDispatcher.php(229): appDevDebugProjectContainer->{closure}()
#3 C:\xampp\htdocs\Fresh_Api\vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\EventDispatcher.php(61):Symfony\Component\EventDispatcher\EventDispatcher->sortListeners('console.command')
#4 C:\x in C:\xampp\htdocs\Fresh_Api\var\cache\dev\appDevDebugProjectContainer.php on line 580
This is of course after I try running php bin/console server:run
I have tried deleting my cache, deleting my autoload file, deleting my vendor file and reinstalling composer. I have tried to look to see if there are any dependencies that are missing. (I could have missed something here)
The file it's talking about is a generated file so something about it is not properly noticing that that class exists.
Here is the CommandListener
namespace Directive\MultiTenant\MasterBundle\EventListener;
use Directive\MultiTenant\MasterBundle\Connection\ConnectionWrapper;
use Directive\MultiTenant\MasterBundle\Entity\Tenant;
use Directive\MultiTenant\MasterBundle\Repository\TenantRepository;
use AppBundle\TenantProviderInterface;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\InputOption;
The appDevDebugProjectContainer is a little large so I'll wait to see if there are any response to see if it's even needed as I'm sure it's something else that is causing it not to generate the proper information needed.
thanks for any input you have.
EDIT: This is a link here noting it's the same issue however this exact issue there is already resolved before all this. Here is my code for that section.
"autoload" : {
"psr-4": {
"AppBundle\\": "src/AppBundle"
},
"classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},
This was already fixed already. here is another link I used to try to solve my issue that of course had no help.
php bin/console symfony commands give a fatal error
and another
Symfony 3.3 Getting ClassNotFoundException
All Credit goes to Cerad the answer is sadly duplicated however I did not notice something that was needed. the autoloader needs to know where it is pulling classes from and has to be setup with the new way the symfony does it now. Here is my example with my additional code to make it work.
"autoload" : {
"psr-4": {
"AppBundle\\": "src/AppBundle",
"BundleName\\": "src/BundleName"
},
"classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},
I got no clue why AWeberAPI is not found. Any help is appreciated.
php code:
require('vendor/autoload.php');
new PHPExcel;
new AWeberAPI;
composer.json:
{
"require": {
"aweber/aweber": "^1.1",
"phpoffice/phpexcel": "^1.8"
}
}
The problem
The module doesn't appear to be properly configured for use/autoloading with composer. They may have just added the composer configuration to allow you to easily install it, but not to use it within the composer autoloader.
The generic convention for it is that AWeberAPI should match the package's PSR-4 autoloader format, which says "look in aweber_api", then it will look for a class named AWeberAPI.php. You can test this behaviour is correct by adding this file:
<?php
// File: vendor/aweber/aweber/aweber_api/AWeberAPI.php
class AWeberAPI {
public function __construct() {
die('yeah, it works now...');
}
}
Then try your script again, the class will exist now.
What can I do?
Well - you could submit a pull request to their repository to fix it, but it looks like it would involve renaming the classes and filenames which would be a breaking change so I probably wouldn't bother.
You can get it to work by requiring the actual source of the API library instead of the composer autoloader in this case:
require_once 'vendor/aweber/aweber/aweber_api/aweber_api.php';
The error I am getting is
file: "/var/www/html/goalline/swiftmailer333/Swift.php" line: 32
message: "Cannot redeclare class Swift" type:
"Symfony\Component\Debug\Exception\FatalErrorException"
I have a need to remove Swift from Laravel as it conflicts with functions form a legacy application that my Laravel app needs to call.
How can I do this? Whether I should is irrelevant I have to use those functions from the legacy application.
I've tried commenting
'Mail' => 'Illuminate\Support\Facades\Mail',
and 'Illuminate\Mail\MailServiceProvider' but that didn't work.
You'll have to namespace your Swift class:
<?php
namespace YourApp;
class Swift {
}
Then use it this way:
$swift = new YourApp\Swift;
Another possibility is to create a nasty hack to remove it from your Laravel installation, but to do that you'll have to create a repository of your own and use your repository in your composer.json file:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/yourusername/swiftmailer"
}
],
"require": {
"swiftmailer/swiftmailer": "dev-master"
}
}
Your repository can pretty much be a copy of swiftmailer's which you basically remove every file except the composer.json.
I have an ecommerce site that i am building for a client. This site had previously worked perfectly fine using procedural functions and curl to make the calls to Paypal. I download the Mailgun and Easypost API manual and installed manually. DOwn the road, I wanted to update the site to utilize PDO & OOP. I have done a fair job at getting the foundation laid. Now it is time to start making calls to the various API's.I installed everything using composer and currently run a dual autoloader. The composer autoload and then beneath it a custom autoload to load my classes. Now, when I call on the PayPal API, I get the following error:
Fatal error: Class 'Paypal\Rest\ApiContext' not found in /var/www/html/myla-dev/shoppingCartFinalize.php on line 18
I think what is happening is my autoloader is trying to load this rather than the composers autoloader. Here is where the autoloading is occurring:
init.php
require __DIR__.'/core/functions/general.php';
function autoLoader ($class) {
if (file_exists(__DIR__.'/core/classes/'.$class.'.php')) {
require __DIR__.'/core/classes/'.$class.'.php';
}
}
spl_autoload_register('autoLoader');
require __DIR__.'/vendor/autoload.php';
This file sits in project root directory and is then required at the top of every file.
File Structure
core
--classes
----Alert.php
----....
--functions
----general.php
vendor
--composer
--easypost
--guzzle
--mailgun
--symfony
--paypal
--autoload.php
index.php
init.php
...
composer.json
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/EasyPost/easypost-php"
}
],
"require": {
"php": ">=5.3.0",
"easypost/easypost-php": "dev-master",
"ext-curl": "*",
"ext-json": "*",
"paypal/rest-api-sdk-php":"*",
"mailgun/mailgun-php": "dev-master"
}
}
Any and all assistance is appreciated. If you feel like writing code, GREAT, but that is not what I am asking for. That is my job, but help with reworking to make it work would be awesome.
Thanks
It ended up being a simple syntax error. After I dumped and updated composer it still failed to work, so I just copied the code from the installation wiki. It worked so I composer their code to mine and I was missing a backslash in my call to the wiki. Thank you for your assistance though.