Problem to set up Composer / autoloading my classes - php

I'm learning how Composer is working (new to dev ^^) But i'm struggling to fix my autoloading...
here's my composer.json :
"autoload": {
"psr-4": {
"OCFram\\": "/../lib/",
"App\\": "/../",
"Model\\": "/../lib/vendors/",
"Entity\\": "/../lib/vendors/",
"FormBuilder\\": "/../lib/vendors/",
"Slug\\": "/../lib/vendors/"
}
},
So for example :
Fatal error: Uncaught Error: Class 'App\Frontend\FrontendApplication'
not found
Well, FrontendApplication path (from composer.json) : **
../App/Frontend/FrontendApplication.php
Here's the FrontendApplication.php with the namespace :
<?php
namespace App\Frontend;
use \OCFram\Application;
class FrontendApplication extends Application
{
public function __construct()
{
parent::__construct();
$this->name = 'Frontend';
}
public function run()
{
$controller = $this->getController();
$controller->execute();
$this->httpResponse->setPage($controller->page());
$this->httpResponse->send();
}
}
Plus, i've , noticed this file (autoload_psr4.php) on vendor/composer :
<?php
// autoload_psr4.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Slug\\' => array('/lib/vendors'),
'OCFram\\' => array('/lib'),
'Model\\' => array('/lib/vendors'),
'FormBuilder\\' => array('/lib/vendors'),
'Entity\\' => array('/lib/vendors'),
'App\\' => array('/'),
);
Would appreciate some help :)
[EDIT]
So i changed the path from
"App\": "/../" (which was no sense)
to :
"App\": "../",
NOW after another composer dump-autoload i get this :
<?php
// autoload_psr4.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Slug\\' => array($baseDir . '/../lib/vendors'),
'OCFram\\' => array($baseDir . '/../lib'),
'Model\\' => array($baseDir . '/../lib/vendors'),
'FormBuilder\\' => array($baseDir . '/../lib/vendors'),
'Entity\\' => array($baseDir . '/../lib/vendors'),
'App\\' => array($baseDir . '/..'),
);
But still same problem when i try php index.php i get :
Fatal error: Uncaught Error: Class
'App\Frontend\FrontendApplication' not found

As of your statement:
Well, FrontendApplication path (from composer.json) : **
../App/Frontend/FrontendApplication.php
You folder structure seems to be:
/App
/<some-dir>/composer.json
It seems you just missed App in the path, you don't need leading or trailing slashes.
"autoload": {
"psr-4": {
"OCFram\\": "../lib",
"App\\": "../App",
"Model\\": "../lib/vendors",
"Entity\\": "../lib/vendors",
"FormBuilder\\": "../lib/vendors",
"Slug\\": "../lib/vendors"
}
},

Related

Class not found after composer require [duplicate]

This question already has answers here:
PHP class not found when using namespace
(2 answers)
Closed 3 years ago.
I'm learning php and composer following the article
I try to use external dependancy composer require phpunit/php-timer.
Here is my composer.json:
{
"name": "ypapax/composer_php_hello_world_log4php",
"minimum-stability": "dev",
"require": {
"php": ">= 7.2",
"phpunit/php-timer": "^2.1#dev"
},
"autoload": {
"psr-0": {
"HelloWorld": "src/"
},
"classname": {
"PHP_Timer": "src/"
}
}
}
and my test.php:
<?php
// Autoload files using Composer autoloader.
require_once __DIR__ . '/../vendor/autoload.php';
use HelloWorld\Greetings;
echo Greetings::sayHelloWorld();
Where greetings.php is
<?php
namespace HelloWorld;
use PHP_Timer;
class Greetings
{
public static function sayHelloWorld()
{
$timer = new PHP_Timer();
$timer . start();
return 'Hello World\n' . $timer->resourceUsage() . "\n";
}
}
When I run the test php tests/test.php
it gives me an error:
PHP Fatal error: Uncaught Error: Class 'PHP_Timer' not found in composer_php_hello_world_log4php/src/HelloWorld/Greetings.php:11
Stack trace:
#0 composer_php_hello_world_log4php/tests/test.php(8): HelloWorld\Greetings::sayHelloWorld()
#1 {main}
thrown in composer_php_hello_world_log4php/src/HelloWorld/Greetings.php on line 11
Fatal error: Uncaught Error: Class 'PHP_Timer' not found in composer_php_hello_world_log4php/src/HelloWorld/Greetings.php:11
Stack trace:
#0 composer_php_hello_world_log4php/tests/test.php(8): HelloWorld\Greetings::sayHelloWorld()
#1 {main}
thrown in composer_php_hello_world_log4php/src/HelloWorld/Greetings.php on line 11
I guess something wrong is in composer.json:
"classname": {
"PHP_Timer": "src/"
}
PHP version:
$ php --version
PHP 7.3.9 (cli) (built: Sep 14 2019 18:07:55) ( NTS )
Link to my test repo
Update
Here is my file autoload_namespaces.php:
<?php
// autoload_namespaces.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'HelloWorld' => array($baseDir . '/src'),
);
And autoload_classmap.php:
<?php
// autoload_classmap.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'SebastianBergmann\\Timer\\Exception' => $vendorDir . '/phpunit/php-timer/src/Exception.php',
'SebastianBergmann\\Timer\\RuntimeException' => $vendorDir . '/phpunit/php-timer/src/RuntimeException.php',
'SebastianBergmann\\Timer\\Timer' => $vendorDir . '/phpunit/php-timer/src/Timer.php',
);
You are not loading the right namespace. I suggest you to checkout the examples on the package page
I think you don't need this in your composer.json:
"classname": {
"PHP_Timer": "src/"
}
According to https://github.com/sebastianbergmann/php-timer/blob/master/src/Timer.php you need
use SebastianBergmann\Timer\Timer as PHP_Timer;
in your greetings.php file.
The use statement was missing from the Greetings class:
<?php
namespace HelloWorld;
use SebastianBergmann\Timer\Timer;
class Greetings
{
public static function sayHelloWorld()
{
$timer = new Timer();
$timer::start();
return 'Hello World\n' . $timer->resourceUsage() . "\n";
}
}
and this can be removed from composer.json:
"classname": {
"PHP_Timer": "src/"
}

Problem with loading the class with php composer

I have such a directory website:
- my application
- apps
--- Backend
--- Core
---- Core \ Config
---- Core \ Drivers (Db.php)
---- Core \ Main
--- Frontend
My composer.json file:
{
"autoload": {
"psr-4": {
"Core\\": "apps/Core/",
"Web\\": "apps/Frontend",
"Cms\\": "apps/Backend"
}
},
"require": {
"php": ">=7.0",
"phpmailer/phpmailer": "~6.0",
"monolog/monolog": "~1.23",
"mpdf/mpdf": "~7.0",
"twig/twig": "~2.5"
},
"config": {
"vendor-dir": "apps/vendor"
}
}
The moment I want to call my application \ secret \ index.php in the file:
require_once ("../apps/vendor/autoload.php");
use Core\Drivers;
use Core\Main;
$bl = new Core\Drivers\Db();
The Db.php file looks like:
namespace Core\Drivers;
class Db
{
...
}
I'm getting an error:
Fatal error: Uncaught Error: Class 'Core \ Drivers \ Db' not found in
Why?
EDIT
// autoload_psr4.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname(dirname($vendorDir));
return array(
'Web\\' => array($baseDir . '/apps/Frontend'),
'Twig\\' => array($vendorDir . '/twig/twig/src'),
'Symfony\\Polyfill\\Mbstring\\' => array($vendorDir . '/symfony/polyfill-mbstring'),
'Symfony\\Polyfill\\Ctype\\' => array($vendorDir . '/symfony/polyfill-ctype'),
'Psr\\Log\\' => array($vendorDir . '/psr/log/Psr/Log'),
'PHPMailer\\PHPMailer\\' => array($vendorDir . '/phpmailer/phpmailer/src'),
'Mpdf\\' => array($vendorDir . '/mpdf/mpdf/src'),
'Monolog\\' => array($vendorDir . '/monolog/monolog/src/Monolog'),
'DeepCopy\\' => array($vendorDir . '/myclabs/deep-copy/src/DeepCopy'),
'Core\\' => array($baseDir . '/apps/Core'),
'Cms\\' => array($baseDir . '/apps/Backend'),
);
Everything looks pretty cool but I would suggest you change the contents of secret/index.php to:
require_once __DIR__.'/../apps/vendor/autoload.php'; //A cleaner approach for requiring files
use Core\Drivers\Db; //So, you don't have to use slash when using your class in your code.
use Core\Main;
$bl = new Db();
use from root directory like this:
use \Core\Drivers;
use \Core\Main;
$bl = new \Core\Drivers\Db();
Use from the root directory the php will search for the given classes
in the current namespace

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

manually entered path in autoload_namespace.php of vendor is not working

I put a path manually in the autoload_namespaces.php in the vendor directory of zf2 for custom library class file which is working in fine in windows local system but when I deployed this to linux server It stop working and giving the below error and please find the below code as I am using.
autoload_namespaces.php file
\vendor\composer\autoload_namespaces.php
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'phpDocumentor' => array($vendorDir . '/phpdocumentor/reflection-docblock/src'),
'ZendXml\\' => array($vendorDir . '/zendframework/zendxml/library'),
'Prophecy\\' => array($vendorDir . '/phpspec/prophecy/src'),
'Cron' => array($vendorDir . '/cron/cron/src'),
'' => array($vendorDir . '/bitweb/stdlib/src', $vendorDir . '/bitweb/stdlib/test', $vendorDir . '/bitweb/zf2-cron-module/src', $vendorDir . '/bitweb/zf2-cron-module/test'),
'Ikey' => array($vendorDir . '/'),
);
library class path path
\vendor\ikey\Mail\Mail.php
I am accessing in controller like
$ikey = new \Ikey\Mail\Mail();
error : \Ikey\Mail\Mail class not found
Note : Plese give me a solution why this is not working in linux server.
The file you mentioned has this line in its header:
// autoload_namespaces.php #generated by Composer
This means you are not supposed to edit it manually. What you should have been doing is editing the autoload section of your composer.json according to Composer documentation
Something along the lines of:
{
"autoload": {
"psr-0": {"Ikey\\": "Ikey/src/"}
}
}

Zend Framework 2 - Integrate PHP Addendum Library

I try to use the addendum library with Zend Framework 2 but I failed.
I tried to add it as a module by copying the addedum il to my \module\util directory. It dosent work.
Then I try something else. I copied the directory under \vendor and addendum like that:
<?php
// autoload_namespaces.php generated by Composer
$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);
return array(
'Zend\\' => $vendorDir . '/zendframework/zendframework/library/',
'ZendTest\\' => $vendorDir . '/zendframework/zendframework/tests/',
'Psr\\Log\\' => $vendorDir . '/psr/log/',
'Monolog' => $vendorDir . '/monolog/monolog/src/',
**'Addendum' => $vendorDir . '/addendum/',**
);
It dosen't work
So I tried to add it at the end od the init_autoloader.php like this:
$loader = new Zend\Loader\StandardAutoloader();
$loader->registerNamespace('Addendum', __DIR__ . '/vendor/addendum');
$loader->register();
When I try to instantiate a class like this:
$foo = new \ReflectionAnnotatedClass($obj);
I always have the same error:
PHP Fatal error: Class 'ReflectionAnnotatedClass' not found in MyClass.php
use composer - and add there:
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": "2.*",
"niktux/addendum": "dev-master"
}
then just php composer.phar install
https://github.com/Niktux/addendum
Then composer autoloader should sort it out for you.

Categories