Mine Composer Autoload PSR-4 is not working - php

I have a problem with my composer and I don't know where to run. I took several classes on how to do this, but my composer doesn't work well. It manages to import my database connection class, but the rest of the classes, in other files, cannot.
My composer.json it is like this:
{
"autoload":{
"psr-4":{
"Src\\": "src/"
}
}
}
The namespace in the file looks like this:
namespace Src\Model;
And the instance happens this way:
$instance = new \Src\Model\CrudUsuario();
The error is as follows:
Fatal error: Uncaught Error: Class 'Src\Model\CrudUsuario' not found in C:\wamp64\www\Src\Controller\crud_usuario.php on line 79

Related

php composer - my package with namespace and class

i have maybe primitive problem. I created my first package in composer. It is just one class in one namespace.
composer.json:
...
"autoload": {
"psr-4": {
"UrlParser\\": "src/"
}
},
...
and i have this in: src/UrlParser/url.php
<?php
namespace UrlParser;
class Url{
...
everything is OK, i uploaded my package into composer. I install it into my project, but when i call this:
<?php
require_once 'vendor/autoload.php';
$a = new UrlParser\Url("http://localhost/aaa.html");
i get this: Fatal error: Class 'UrlParser\Url' not found in C:\xampp\htdocs\ccc\01\index.php on line 3
I am new in composer and i try to google my problem, but i am lost :)
Thanks
Try this
namespace UrlParser;
$a = new Url("http://localhost/aaa.html");
If it doesn't work, there's probably a problem with autoload that you didn't get the right way
the problem was, that i didnĀ“t do this:
composer dump-autoload -o

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 autoload Class not found exception

I create a file named Util.php and there are two class in the Util.php file.
class StringUtil {}
class ArrayUtil {}
how can I make the Util.php into composer library for other project include? now I got a Class Not Found Exception.
I create a file named Util.php and there are two class in the Util.php
file.
First of all this is bad practice
Anyway, it can help:
https://getcomposer.org/doc/04-schema.md#files
{
"autoload": {
"files": ["src/MyLibrary/functions.php"]
}
}

Composer autoload custom classes

I try to autoload my custom pdo class with composer.
Ran the following command to update autoload:
compser update
composer install
Both seem to work, no error prompted. But,
vendor/composer/autoload_namespaces.php
Does not list the custom namespace added to composer.js.
File structure
-Root
->classes
->pdo
->class.php
->vendor
->various extensions loaded with composer
index.php
PHP Class
namespace Classes\Pdo;
Class DB {
//Do some stuff...
}
Composer.js
"autoload": {
"psr-4": {
"Classes\\Pdo\\": "classes/pdo"
}
}
Index.php
$pdo = new \Classes\Pdo\DB(); //Fatal error: Class 'Classes\Pdo\DB' not found
Old question, but I just ran across this myself.
For future Googlers, in my case the issue turned out to be the name of the class file did not exactly match the class name.
See this post: Why does 'composer dumpautoload -o' fix 'Class not found' PHP error?

Symfony2 composer vendor namspace not found

I'm trying to use a vendor lib that I created my self. For now I am not able to put it in GIT or SVN so I am trying to get it running without.
This is my directory structure(borrowed from an answer below):
vendor/
ISTlibraries/
Saml2Handler/
src/
Saml2Handler/
In my composer.json I have added
"autoload": {
"psr-0": {
"": "src/",
"Saml2Handler": "vendor/ISTlibraries/Saml2Handler/src/"
}
},
vendor/ISTlibraries/Saml2Handler/src/ is the path to my sourcecode. The class I am trying to get is called Saml2Controller which have this namespace defined
namespace Saml2Handler;
When I try from inside my symfony2 controller to initiate the class I get an error:
FatalErrorException: Error: Class 'Saml2Handler\Saml2Controller' not found in ...
In the controller I try a simple new Saml2Controller and I have written
use Saml2Handler\Saml2Controller;
Where am I going wrong?
The error is expected. I believe that you need the following directory structure:
vendor/
ISTlibraries/
Saml2Handler/
src/
Saml2Handler/ <--- you don't have this
Saml2Controller.php
... in order to have namespace Saml2Handler within your Saml2Handler.php.

Categories