I see that my dependency use
"autoload": {
"psr-4": {"Ion\\": "src/"}
}
And at the src/container folder
<?php
namespace Ion;
class Container
And, I try to load it using the autoloader (of course I've composer require that library)
<?php
require 'vendor/autoload.php';
use Ion\Container;
$ion = new Container();
But, it turns out to be a fatal error
Fatal error: Uncaught Error: Class 'Ion\Container' not found in C:\UniServerZ\www\projects\playground\ion\test.php on line 6
What's wrong with this? Anyway to fix it?
Please help
This is the package (which I code) : https://packagist.org/packages/terrydjony/ion
I have downloaded your package and troubleshoot the issue. Actually, you need to place your file in a folder called Ion. It should be src/Ion/Container.php instead of ion/src/Container.php.
+-- src
| +-- Ion
| +-- Container.php
+-- vendor
+-- composer.json
Another small mistake you made, your class filename is in lowercase container.php but you defined it in uppercase.
class Container
{
}
Related
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!
I installed composer and laravel and installed some packages
all work fine, but now I created my own class under the folder services
I gave it name space of Services like that:
namespace Services;
And the class name is UploadToImgurService
I run the composer command:
composer dump-autoload
And in my controller I wrote:
use Services\UploadToImgurService;
But I get this error:
Class 'Services\UploadToImgurService' not found
What did I did wrong?
Is there anything else that I should do with composer for autoloading the service class?
EDIT
I found a solution
I edited y composer.json file and added to psr-4 the line
"Services\\" : "app/services"
But why It didn't workt before? The line :
"App\\": "app/",
was there, maybe it loaded the class but under the app namespace?
If you are using a case sensitive file system, you will need to have the Services folder with upper case S.
Your idea is right, but let me try to explain how the psr-4 autoloading works.
You can define root namespaces in your composer.json file and map it to any project directory. Inside the defined directories, your classes should get the root namespace. The namespace segments after the root are build by your sub directory structure and the class name is equal to the file name (PSR-4 Autoloading).
E.g
"MyNamespace\\WithSubNamespace\\": "cool/project"
cool/project/MyClass.php -> MyNamespace\WithSubNamespace\MyClass
cool/project/SubDirectory/AnotherClass.php -> MyNamespace\WithSubNamespace\SubDirectory\AnotherClass
In Laravel, the app directory is mapped to the App namespace as default. Optionally, you can change the root namespace with the command php artisan app:name [NewRootNamespaceName], but the autoloader only finds classes inside the app directory.
If you create a new directory outside of "app", you have to add the directory to your psr-4 namespace mapping in the composer.json file.
In your example, you define a new root namespace in the existing app directory, so your issue was that the root namespace was unknown and you solved it by adding the line in your composer.json.
This is possible, because psr-4 provides a huge flexibility. But personally, i would not recommend to use different root namespaces in the same project.
I hope i could help and maybe this is also interesting for you: composer.json PSR-4.
maybe you forgot to add this line of code
require_once('vendor/autoload.php');
Once I was getting the error:
Fatal error: Uncaught Error: Class "..." not found
in /var/www/html/... on line ...
I was requiring autoload file this way:
require 'vendor/autoload.php';
After I changed the line to:
require __DIR__.'/vendor/autoload.php';
it started working...
Weird, since the the vendor folder was in the same directory as the file which was calling it.
BTW, I was using Composer 2 and PHP 8.1.
I have following library structure
C:\WWW\WEBS
| users.php
|
\---lib
| Api.php
| Server.php
| TimeSync.php
|
\---TimeSync
Ntp.php
Protocol.php
Sntp.php
In server.php I have
<?php
namespace lib;
class Server extends Api
{
}
?>
In users.php I am using it as
<?php
use lib\Server;
$objServer = new Server();
?>
I also tried it using like use lib\Server;
But in both cases it is saying
Fatal error: Class 'lib\Server' not found in
C:\www\Experimentation\webserviceserver\users.php on line 7
Where I am going wrong .Should I user a autoloader?
You will need to use an autoloader.
You can use the one delivered in Composer.
Once you've installed it (at the root of your project), you put a composer.json file, and inside you copy the following code :
{
"autoload":{
"psr-4":{
"Lib\\" : "lib/"
}
}
}
Then you use the command composer dumpautoload. That will copy the autoloading files in a folder named "vendor" at the root of your project.
Then in users.php you require the autoloader :
require_once "vendor/autoload.php"
And you use the namespacing :
use Lib;
$objServer = new Lib\Server();
It might work.
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.
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.