Composer won't autoload - php

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/"
}
}
}

Related

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

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();

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!

ServiceProvider not found laravel 5.2

I'm creating a composer installable project inside vendor.
This is my service provider file,
<?php
namespace vimuths123\gitpack;
use Illuminate\Support\ServiceProvider;
class GitpackServiceProvider extends ServiceProvider {
public function register() {
$this->app->bind('gitpack', function ($app) {
return new Gitpack;
});
}
public function boot() {
// loading the routes file
require __DIR__ . '/Http/routes.php';
// define the path for the view files
$this->loadViewsFrom(__DIR__ . '/../views', 'gitpack');
}
}
This is the structure,
vendor
|
vimuths123
|-gitpack
|-src
| |-GitpackServiceProvider.php
|
|-composer.json
I already added my service provider in app/config.php
vimuths123\gitpack\GitpackServiceProvider::class,
and my root composer.json I have following code.
"psr-4": {
"App\\": "app/",
"vimuths123\\gitpack\\" : "vendor/vimuths123/gitpack/src"
}
This is my package composer file,
{
"name": "vimuths123/gitpack",
"autoload": {
"psr-4" : {
"vimuths123\\gitpack\\" : "src"
}
},
"require": {
"composer/installers": "~1.2"
}
}
but all I'm getting is this error,
Class 'vimuths123\gitpack\GitpackServiceProvider' not found
It would be great help someone can help me on this.
You should not put any files into vendor/ by hand. If you are developing a library it must be composer installable library (which once installed end up in vendor/.
Your composer.json seems wrong, especially vendor/vimuths123/gitpack/src name space in psr4. This's smells from a mile as I'd bet you not using vendor/vimuths123/gitpack/src namespace.
Finally, after adding new class you should update class loader to let it know about that:
composer dumpautoload
which solves most of problems with "cannot find my class" issues.
EDIT
It seems your problems are in your library package, not the project using it. From comments it looks that you need to edit your package's composer.json. Assuming package is using vimuths123\gitpack namespace (note, namespace does NOT have to be the same as package name - these are two different things) and its sources sit in src subfolder (so it would be <project>/vendor/vimuths123/gitpack/src) then I'd rework autoload section to look like this:
"autoload": {
"psr-4" : {
"vimuths123\\gitpack\\" : "src"
}
}
and then composer dumpautoload.
Run the following artisan command:
php artisan optimize
Then see if the class can be found by Laravel.

PHP - Doctrine - How to autoload entities classes using a single namespace

I have a group of PHP files containing classes (entities). Each class has the same namespace:
// src/App/Entity/Actions.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Actions
*
* #ORM\Entity
*/
class Actions
{
// SOME CODE
I autoload the PHP files containing the classes with composer:
"autoload": {
"psr-0": {
"App": "src/"
}
}
And in my bootstrap.php file, I add this line:
use App\Entity;
So I figured that because told the app to use the App\Entity namespace, that I can just call the entity classes like this: $entity = new Actions();
but when I try that, I get this error:
Fatal error: Class 'Actions' not found in C:\wamp64\www\spider\chebi2\inc\orm_tools.php on line 49
If I do this:
use App\Entity; use App\Repository;
if (class_exists('Actions')) { dump('exists'); } else { dump('not exists'); }
if (class_exists('\App\Entity\Actions')) { dump('exists'); } else { dump('not exists'); }
Heres what it outputs:
PS C:\wamp64\www\spider\chebi2> php .\get_actions.php
"not exists"
"exists"
So it can only find the class when I provide the full namespace. And weirdly enough, when I tried this:
// Direct path to the Actions.php file
use App\Entity\Actions;
if (class_exists('Actions')) { dump('exists'); }
else { dump('not exists'); }
if (class_exists('\App\Entity\Actions')) { dump('exists'); }
else { dump('not exists'); }
I get the same result:
PS C:\wamp64\www\spider\chebi2> php .\get_actions.php
"not exists"
"exists"
So now I'm even more confused. What is the point in using: use App\Entity; if it doesn't actually make the classes in that namespace directly available? And why is assigning the direct path to the class use App\Entity\Actions; not even working?
Am I doing something wrong here? Is there a correct way to use namespaces that I'm not understanding?
PSR-0 is depracated you should use PSR-4
in PSR-4
composer.json
"autoload": {
"psr-4": {
"App\\": "src/",
}
}
in directory src/ which is on same level as composer.json add directory Entity so in path src/Entity add class file Actions
namespace App\Entity;
class Actions
{
}
you can also use composer dump-autoload and check vendor/composer/autoload* fiels and see if namespaces are registered there.'
Regarding class_exists() it does not work with short names or aliases you need to provide the full name of class. I'd suggest using ::class operator So in your case it would be:
<?php
use App\Entity\Actions;
class_exists(Actions::class);
Thanks! I changed the auto loader to psr-4, and attached it to this:
"psr-4": {
"App\\": "src/"
}
dump-autoload is exactly what I was looking for, but I don't see any included files or classes listed:
PS C:\wamp64\www\spider\chebi2> composer dump-autoload -vvv
Reading ./composer.json
Loading config file ./composer.json
Checked CA file C:\Users\horse\AppData\Local\Temp\composer-cacert-12fdaece071ee9515fa28aabed5ab089876ae257833106e15a583e060eaff6b5.pem: valid
Executing command (C:\wamp64\www\spider\chebi2): git branch --no-color --no-abbrev -v
Executing command (C:\wamp64\www\spider\chebi2): git describe --exact-match --tags
Executing command (C:\wamp64\www\spider\chebi2): git log --pretty="%H" -n1 HEAD
Reading C:/Users/horse/AppData/Roaming/Composer/composer.json
Loading config file C:/Users/horse/AppData/Roaming/Composer/composer.json
Reading C:\wamp64\www\spider\chebi2/vendor/composer/installed.json
Reading C:/Users/horse/AppData/Roaming/Composer/vendor/composer/installed.json
Running 1.2.2 (2016-11-03 17:43:15) with PHP 5.6.25 on Windows NT / 10.0
Generating autoload file
I still can't find the entity classes.
To clarify, I should have the folder structure like this:
- src (contains only subdirectories)
- Entity (contains the entity files)
- Repositories
- App (empty)

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