Composer PSR-4 Autoload class not found - php

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

Related

Composer does not index classes [duplicate]

This question already has an answer here:
Class Foo\Bar\Baz located in ./foo/bar/utility/baz.php does not comply with psr-4 autoloading standard. Skipping
(1 answer)
Closed 1 year ago.
Composer 2.0.1..
I have used version 1 and never had a problem... but now...
The problem is that classes not indexed and php fails with "class not found in..." error.
dump-autoload -o shows no errors and 1193 classes as before I have added last ones.
composer.json section
"autoload": {
"psr-4": {
"Application\\" : "src/"
}
}
class location: src/models/syslog.php
class code:
<?php
namespace Application\Models;
class SysLog extends CustomModel {
}
It is a very simle task... but i have spent a lot of time and it does not work...
Read/write rights are ok.
Autoload adds this line to autoload_psr4.php: 'Application\\' => array($baseDir . '/src'),
So i do not actually have a clue...
The answer is that path in Composer2 MUST be exactly the same as namespace.
path /src/models/syslog.php MUST be /src/Models/SysLog.php for Application
I bet it is wierd but... it is true.

Composer autoload doesn't work regardless of whatever I do

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.

Composer (Facebook Ads SDK) setup with CodeIgniter

I have a CodeIgniter application. I've been trying to set it up to work with Facebook ADS SDK ( https://github.com/facebook/facebook-php-ads-sdk ).
I want to use a simple lines in my controller like:
use FacebookAds\Object\CustomAudience;
use FacebookAds\Object\Fields\CustomAudienceFields;
use FacebookAds\Object\Values\CustomAudienceSubtypes;
$audience = new CustomAudience(null, 'act_123123');
I have created composer.json file:
{
"require": {
"facebook/php-ads-sdk": "2.8.*"
}
}
I let it through command
php composer.phar install --no-dev
And everything worked fine. It installed me autoload.php with external facebook folder.
Now when it comes to the part where I need to make it work with CodeIgniter I constantly get errors.
I tried two different approaches:
First was to include it in my index.php Like this:
include_once 'application/vendor/autoload.php';
require_once BASEPATH.'core/CodeIgniter.php';
However I was getting errors for not loading my classes that are in application/core folder (Back_Controller is extending CI_Controller and I am using Back_Controller in my every controller, I also have My_Model and Front_Controller there).
Second aproach:
To use CodeIgniter's feature to use Auto-loader together with Composer. So I changed in the config:
$config['composer_autoload'] = TRUE;
Both approaches returned me the same errors:
"Fatal error: Class 'Back_Controller' not found in /public_html/application/controllers/admin/Shops.php on line 4
Warning: include(): open_basedir restriction in effect.
File(application/errors/html/error_php.php) is not within the allowed
path(s): () in /public_html/system/core/Exceptions.php on line 269"
How can I set it up so Facebook SDK actually works together with CodeIgniter?
Any help will be very appreciated. Thank you.
I am thinking that one of the solutions would be to add "autoload" to composer.json so it loads my core classes but I can't figure out how should it be. I tried:
"autoload": {
"psr-4": {
"Back_Controller\\":"core/"
}
}
Try the following:
Include the main Api file
include APPPATH.'vendor/wherever_your_sdk_is/FacebookAds/Api.php';
Then do the following... as per documentation...
use FacebookAds\Api;
// Initialize a new Session and instanciate an Api object
Api::init($app_id, $app_secret, $access_token);
// The Api object is now available trough singleton
$api = Api::instance();
Figured it out.
I just added "autoload" to composer.json to autoload my core files and now both solutions that I tried before - works.
"autoload": {
"psr-4": {
"":"core/"
}
}

Cannot redeclare class error in Laravel but works fine in native php

I am using Alchemy API for filter out some data. Everything works fine in the native code. But when i used it in my Laravel Controller it throws Cannot redeclare class. My controller,alchemyapi.php and example.php are in the same directory. Here is how i include alchemyapi.php in native code
<?php
require_once 'alchemyapi.php';
$alchemyapi = new AlchemyAPI("MY API KEY"); ?>
But when i include it in the controller it throws my the error. is there something i am missing ?
require_once 'alchemyapi.php';
$alchemyapi = new AlchemyAPI("MY API KEY");
The native code(example.php) works well without any issue. But in laravel controller it throws a error saying Cannot redeclare class AlchemyAPI
in alchemyapi.php line 20
Instead of using require_once use namespace in your alchemyapi.php and then use use for same namespace in your MyController
alchemyapi.php
<?php
namespace App\Http\Controller;
class AlchemyApi {
//your code
}
MyController.php
<?php
namespace App\Http\Controller;
use App\Http\Controller\AlchemyApi;
class MyController {
$alchemy = new AlchemyApi("Your_api_key");
}
OK, so what I think is happening is that alchemyapi.php is somehow already being included by the composer autoloader.
Try this.
Create the directory lib in the root of your project.
Move alchemiapi.php into lib.
Under the "autoload" section in your composer.json add the following code. Make sure the JSON is valid:
"files": [
"lib/alchemyapi.php",
],
Run composer dump-autoload. If it errors, your composer.json is invalid or you haven't put the file in the correct place.
Delete require_once 'alchemyapi.php'; from your controller.
When dealing with composer, this is how you deal with classes in the global namespace. After running composer it scan those directories in app for PSR-4 classes.
I can't be sure but I think that composer is looking for it and you are also manually requiring it. That would explain why PHP thinks you are redeclaring the class.

Class 'AWeberAPI' not found

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';

Categories