Why this Simple case of psr-4 not working with composer - php

I am trying to understand how psr-4 works using composer. These are the contents of my composer.json file
{
"autoload": {
"psr-4": {
"Vehicle\\Car\\":"src/"
}
}
}
The Folder Structure is given below (please note I am using windows 10, so directory name casing should not matter I think)
The folder where I have created 'vendor' folder is inside D:\temp\composer_test
D:\
temp\
composer_test\
test.php
composer.json
composer.lock
vendor\
vehicle\
car\
src\
Tire.php
Contents of test.php
require __DIR__ . '/vendor/autoload.php';
$tire = new Vehicle\Car\Tire();
Contents of Tire.php
<?php
namespace Vehicle\Car;
class Tire {
public function __construct()
{
echo "initialize Tire\n";
}
}
But when I run the code (snapshot below) . I see error
D:\temp\composer_test>php test.php
PHP Fatal error: Uncaught Error: Class 'Vehicle\Car\Tire' not found in D:\temp\composer_test\test.php:3
Stack trace:
#0 {main}
thrown in D:\temp\composer_test\test.php on line 3
I Don't know what is wrong I am doing. Problem is when I install any other standard package I can use it successfully which seems like using the same format

Your composer.json should be:
{
"autoload": {
"psr-4": {
"Vehicle\\":"src/vehicle/"
}
}
}
Your directory structure should be:
D:\
temp\
composer_test\
test.php
composer.json
composer.lock
src\
vehicle\
car\
Tire.php
Make sure to run composer dump-autoload to re-general the autoload files.
Also your namespace does not make much sense, naming your namespace Vehicle would mean that you only have vehicles in your repo. Usually you would name your namespace based on your project name, could be your brand or your username, or something more generic such as App.
So this would make more sense:
{
"autoload": {
"psr-4": {
"App\\":"src/"
}
}
}
then you have:
D:\
temp\
composer_test\
test.php
composer.json
composer.lock
src\
vehicle\
car\
Tire.php
plane\
...
and test.php
require __DIR__ . '/vendor/autoload.php';
$tire = new App\Vehicle\Car\Tire();

Related

Class not found in the namespace

I'm very new in the field and I'm trying to create my first composer package. I'm following the structure mentioned here but for some reason I always get that the class is not found.
My directory structure is
Project
- src/
-- project
index.php
- vendor/
-- composer/
autoload.php
index.php
So in the main directory Project I have index.php with
<?php
use App\project;
// Autoload files using the Composer autoloader.
require_once __DIR__ . '/vendor/autoload.php';
$entry = new simplePrint();
echo($entry->printHome());
In the directory src/project/ I have index.php with
<?php
namespace App\project;
class simplePrint {
public function printHome() {
return "Hey";
}
}
in composer.json
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
After I create the files, I've made
composer install
composer dump-autoload
What I'm missing here?
Update: after composer update it is still same. The output of the composer update
$ composer update
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
127.0.0.1:45046 [500]: GET / - Uncaught Error: Class "App\project" not found in ...
PSR-4 says following: The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.
So you must call your filename simplePrint.php and not index.php.
Here you can read some more information about PSR-4: https://www.php-fig.org/psr/psr-4/

Composer not generating autoload files package made by me

I hate to add more to the noise of "autoload isn't working!!!!!", but I can't seem to get this problem figured out, and I figured getting some fresh eyes on it would get the problem in a lot less time. Here is my index.php file:
<?php
declare(strict_types=1);
require_once 'vendor/autoload.php';
require_once 'model/PageNav.php';
use ShinePHP\{Crud, CrudException, EasyHttp, EasyHttpException, HandleData, HandleDataException};
// ALWAYS serve over encrypted channels
try {
EasyHttp::checkHttps();
} catch (EasyHttpException $ex) {
echo $ex;
}
try {
// check if it's a GET request, if it is, serve page, if not, do nothing
if (EasyHttp::isRequestMethod('GET')) {
$Page = new PageNav('Home', 'view/home.php');
$Page->buildPage();
exit;
}
}
catch (EasyHttpException $ex) {
echo $ex;
}
So obviously I am using a package from composer called ShinePHP (it's one I've made, and I'm still working on the documentation, so I'm just using it for my own projects at the moment, composer just makes package management so easy!)
ANYWAYS...because I'm writing this question, I'm obviously getting the following error:
Fatal error: Uncaught Error: Class 'ShinePHP\EasyHttp' not found in /Applications/XAMPP/xamppfiles/htdocs/KRTY/src/index.php:11 Stack trace: #0 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/KRTY/src/index.php on line 11
Now, I haven't manually touched the composer.json file, so here it is:
{
"require": {
"adammcgurk/shine-php": "~0.0.1"
},
"autoload": {
"psr-4": {
"ShinePHP\\": "src/"
}
}
}
I'm not getting any errors on requiring the vendor/autoload.php file (and I've tried changing the path to something that doesn't exist like vendor/alkdjfladksf/autoload.php and it throws an error how it should), I'm running PHP version 7.2.7 on XAMPP on Mac OS Mojave. Here is the directory structure, the highlighted index.php file is the one with the code above:
And here is the output of composer dump-autoload -o:
Generating optimized autoload files
And so...to add more to the fire of this question on Stack...How can I get composer to autoload my ShinePHP namespace with the classes as shown in the code?
This dependency does not have any autoloading rules, so Composer does not know where to find ShinePHP\EasyHttp class. You need to add autoloading configuration in composer.json of shine-php package:
"autoload": {
"psr-4": {
"ShinePHP\\": "src/"
}
},

Composer Autoload Classes from outside vendor

I'm struggling to have my custom classes autoloaded with composer.
my directory structure:
--muttley
--library
--MyClass.php
--public
--index.php
--vendor
--composer.json
in my composer.json:
"autoload": {
"psr-4": {
"Library\\": "library/"
}
}
MyClass.php:
namespace Library\MyClass;
class MyClass {
}
in index.php:
use Library\MyClass;
require_once dirname(__FILE__).'/../vendor/autoload.php';
the root directory is defined using DocumentRoot /www/muttley/public/. I keep getting the error:
Fatal error: Class 'Library\MyClass' not found in /var/www/muttley/public/index.php on line 58
Is there anything that I might be missing?
Simple mistake. Change:
namespace Library\MyClass;
to
namespace Library;
Make sure you have ran composer dumpautoload too!

Composer won't autoload

I've got a new project that I'm trying to stand up with Composer from the start, [I'm a very late adopter] but autoloading will not work, I can't figure out why, and it's driving me nuts.
This is literally it for the project right now: [excluding /vendor/]
/
lib/
Client.php
composer.json
test.php
composer.json
{
"autoload": {
"psr-0": {
"Openstack\\": "lib/"
}
}
}
lib/Client.php
<?php
namespace Openstack;
class Client {
public function __construct() {
echo "hello world";
}
}
test.php
<?php
require('vendor/autoload.php');
$foo = new Openstack\Client();
Trying to run test.php gives me:
PHP Fatal error: Class 'Openstack\Client' not found
Even though:
# grep -r Openstack vendor/composer/
vendor/composer/autoload_namespaces.php: 'Openstack' => array($baseDir . '/lib'),
What does this thing want of me?!
When you use psr-0, you need to have a directory for each namespace level. So your directory structure needs to be this:
/
lib/
Openstack/
Client.php
composer.json
test.php
Alternatively, you can use psr-4 in your composer.json.
{
"autoload": {
"psr-4": {
"Openstack\\": "lib/"
}
}
}

Composer and PSR-0 class autoloading with namespaces

Hello guys i have problem with autoloading my class with composer. On Linux all work perfect, but now my boss change env and set Windows. All this work on linux but windows show newbie fatal error:
Fatal error: Class 'AbstractController' not found in
D:\xampp\htdocs\ikacFw\frontController.php on line 7
Common to see my composer.json and stucture for better picture on problem.
Stucture is :
frontController.php
-- vendor
----- Doctrine
----- Ikac
--------- Components
---------- Mvc
------------- Controller
Am trying to load all data from vendor directory.
Composer.json
{
"autoload": {
"psr-0": {
"vendor": ""
}
}
}
Also new component i add manual. Like this :
$loader = require_once 'vendor/autoload.php';
$loader->add('vendor', "Ikac");
Okay next when i try to call :
<?php
require_once 'vendor/autoload.php';
use Ikac\Mvc\Controller;
$a = new AbstractController();
I get error "not found".
My class AbstractController contain defined namespace but dont work again. Like test i do this:
<?php
//vendor/Ikac/Mvc/Controller/AbstractController.php
namespace Ikac\Mvc\Controller;
class AbstractController {
function __construct() {
echo __CLASS__;
}
}
?>
I do from cmd composer dump-autoload, install, but dont work. All this perfect work on linux but here wont. Any idea how to fix this or where i do mistake.
Thanks guys!
SLOVED:
{
"autoload": {
"psr-0": {
"": "vendor/"
}
}
}
Well you should do
<?php
require_once 'vendor/autoload.php';
use Ikac\Mvc\Controller\AbstractController;
$a = new AbstractController();
Your autoloading declaration is wrong.
You will NEVER ever need to include the vendor folder in any autoloading. The vendor folder will contain the autoloading for both all dependencies, and - if configured - for your own classes as well.
You can use Composer to create autoloading for your own classes. Just include the correct info. But from your current info I cannot deduct what would be correct.

Categories