Composer PSR-4 autoloading completely ignored - php

I have composer.json file:
{
"name": "marko/art-empire",
"description": "Social network",
"type": "project",
"authors": [
{
"name": "Marko Ilic",
"email": "markowebdeveloper#gmail.com"
}
],
"require": {},
"autoload": {
"psr-4": {
"Songs\\": "songs/"
}
}
}
autoload_psr4.php file:
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Songs\\' => array($baseDir . '/songs'),
);
RandomSong.php file in songs folder (which is in root directory):
namespace Song;
class RandomSong
{
public function songName()
{
return 'Random Song';
}
}
test.php file:
require 'vendor/autoload.php';
use Songs\RandomSong;
$randomSong = new RandomSong();
echo $randomSong->songName();
As you can see I am trying to autoload RandomSong class, but I keep getting:
Fatal error: Uncaught Error: Class 'Songs\RandomSong' not found in test.php
Please help, thanks.

Your namespace is called Song, but you refer to it as Songs

Your RandomSong uses Song namespace, while your loader is for Songs namespace.

Related

Composer class not Found Error

I've been working on a project and for which I needed to make a package. But it always returns
PHP Fatal error: Uncaught Error: Class 'youtubetomp3\downloader' not found
Here the structure of directory.
youtubetomp3/
src/
downloader.php
test/
downloaderTest.php
composer.json
composer.lock
and other files
The composer.json contains following details.
{
"name": "princeyadav05/youtubetomp3",
"description": "Downloads mp3 of a video given video-id",
"keywords": ["youtube", "songs", "downloader", "package"],
"license": "",
"authors": [
{
"name": "Prince Yadav",
"email": "princeyadav96#gmail.com"
}
],
"type": "package",
"require": {
"php": ">=5.4",
"php-ffmpeg/php-ffmpeg": "^0.11.0"
},
"require-dev": {
"phpunit/phpunit": "5.2.*"
},
"autoload": {
"psr-4": {
"princeyadav05\\youtubetomp3\\": "src/"
}
}
}
And this is how I'm creating object of class.
<?php
require 'vendor/autoload.php';
require 'scrapper.php';
include 'database.php';
echo "** WELCOME TO THE MP3 DOWNLOADER ** \n \n";
$name = readline("Hey There. Lets start with your name : ");
echo "\nHello " . $name . ".\n";
$search = readline("Please enter the search query : ");
$data_array = searchVideo($search); //returns data array
displayVideos($data_array); // returns video id
$download = new downloader();
$video_details = $download->downloadSong($video_id);
// returns array with video title, Duration, url, path
savingToDb($video_title, $video_duration, $video_url, $songs_path);
?>
Please help. I've tried many things but nothing worked.I'm really frustrated.
It's not clear whether you're including autoload.php file generated by composer. Would you mind posting the full code?
Then I'm not sure about your autoload directive, I would try something like this:
"autoload": {
"classmap": [
"src/"
]
},
see composer docs for more info

Composer autoload can't find class

I'm trying to create a MVC structure and use composer to autoload everything.
But I keep getting this error:
Fatal error: Uncaught Error: Class 'App\Init' not found in C:\wamp64\www\activity\Public\index.php on line 5
|MainFolder
|App
|Public
|Vendor
|ACT
|composer
|autoload.php
|composer.json
composer.json:
{
"name": "vendor/activity",
"description": "descrip",
"require": {
"php": ">=5.6.25"
},
"authors":[
{
"name": "John Doe",
"email": "johndoe#gmail.com"
}
],
"autoload":{
"psr-4": {
"ACT\\": "vendor/",
"App\\": "/"
}
},
"config":{
"bin-dir": "bin"
}
}
App\init.php
<?php
namespace App;
class Init
{
public function __construct()
{
echo "Loaded!!";
}
}
Public\index.php
<?php
require_once '../vendor/autoload.php';
$init = new \App\Init;
\Vendor\composer\autoload_namespaces.php
<?php
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
"ACT" => array($vendorDir . false),
"App" => array($baseDir . '/'),
);
Obs: Already did composer dump-autoload
Don't manually put things in /vendor.
While adhering to #1, don't reference /vendor in autoload, the packages should all have their own fully-functionaly autoloaders that composer will find and use.
You need to specify more of the path in your autoload.
"autoload":{
"psr-4": {
"App\\": "App/"
}
},
Think of it like telling composer "look for things starting with the namespace foo\bar\ in the following folder".
Note: The folder name doesn't have to match the namespace.
Eg: Following the suggested Vendor\Package\ scheme for PSR/Composer
{
"autoload": {
"psr-4": {
"sammitch\\meatstacker\\": "src/"
}
}
}
And then:
\sammitch\meatstacker\Client maps to src/Client.php
\sammitch\meatstacker\Bread\Rye maps to src/Bread/Rye.php
and so on

PSR-4 autoloading with Composer - Class not found

I'm create composer package with type library. And trying to require it to Symfony2 project.
The package has following composer.json
{
"name": "vendor/package-sdk",
"description": "My private package",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {"Vendor\\PackageSDK\\": "src/"}
},
"require": {
"php": ">=5.3.3"
}
}
Then I require it to my SF2 project.
"repositories": [
{
"type": "git",
"url": "git#github.com:me/vendor-package-sdk.git"
},
],
"require": {
...
"vendor/package-sdk": "~0.0.1-alpha1"
...
}
When I calling
use Vendor\PackageSDK\Client;
...
$client = new Client();
```
And I got fatal error:
PHP Fatal error: Class 'Vendor\PackageSDK\Client' not found in /path
If I do
composer dump-autoload -o
It works, but
composer dump-autoload
not.
The file vendor/composer/autoload_psr4.php contain:
'Vendor\\PackageSDK\\' => array($vendorDir . '/vendor/package-sdk/src'),
Could anybody tell me what am I doing wrong?
In composer autoload_classmap.php file I saw the following line
'Vendor\PackageSDK\Client' => $vendorDir . '/vendor/package-sdk/src/Cilent.php',
So it's just a typo in filename of package
Cilent.php should be Client.php

Composer for autoloading php

Hi i have this folder structure:
I'm using composer for autoloading my files but it is dont work .. i do it first time and i dont know how to implement this.
My composer.json
{
"name": "Some name",
"description": "Some Framework",
"minimum-stability": "stable",
"license": "proprietary",
"authors": [
{
"name": "Some names of authors",
"email": "some#gmail.com"
}
],
"autoload": {
"psr-4": {
"Apison": "/../sdk/"
}
}
}
And my index.php
<?php
require_once 'vendor/autoload.php';
$app = new \Apison\Sdk\App();
When i update my composer it will write: Nothing to load and PHP will catch exeption on line with $app = new \Apison\Sdk\App();
Thanks for your tips
namespaces need \\:
"Apison\\": "../sdk"
documentation:
https://getcomposer.org/doc/04-schema.md#psr-4
Based on our chat, the solution is this:
"autoload": {
"psr-4": {
"Apison\\Sdk\\": "sdk"
}
}
Then the namespaces and file structure was changed to comply with the psr-4 standard

Referencing a custom composer package in Slim PHP

I'm trying to figure out how to reference a custom class using composer
my composer.json file looks like this:
{
"name": "adtools_api",
"repositories": [
{
"type": "package",
"package": {
"name": "qz/adtools_middleware",
"version": "dev-master",
"source": {
"url": "repo-name",
"type": "git",
"reference": "master"
}
}
}
],
"require": {
"slim/slim": "2.*",
"qz/adtools_middleware": "src/"
}
}
and the folder structure looks like this:
app
routes
vendor
composer
qz
adtools_middleware
src
hello-world.php
slim
composer.json
index.php
I'm trying to reference the hello-world.php file which looks like this:
<?php
namespace HelloWorld;
class SayHello
{
public static function world()
{
return 'Hello World, Composer!';
}
}
?>
In the index.php file I'm trying to reference the class like this:
$hello = new HelloWorld\SayHello();
but getting an error telling me "Fatal error: Class 'HelloWorld\SayHello' not found in..."
If anyone can point me in the right direction that would be great! Thank you!
Can you check the autoload inside the vendor folder and see if your HelloWorld namespace is loaded?
If not, you may need to add autoload attribute to your composer.json file, like this
{
"autoload": {
"psr-0": {"HelloWorld": "qz/adtools_middleware/src/"}
},
to load the HelloWorld namespace in your project

Categories