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
Related
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/"
}
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"
}
},
I just finished building a site locally via Xampp. Everything is working just fine, its using PHP version 5.6. I used composer to use some third party apps such as Guzzle and Stringy. After done I uploaded to my Godaddy webhosting account, which uses PHP 5.5. When I load the site I get this error:
Fatal error: Class 'Libs\Model\Site_Settings' not found in public_html/portal/conf/settings.php on line 81
However the vendor namespaces work just fine. I dont get any errors. Heck I dont get any errors with my custom classes. I am using composer to autoload everything. Everything works perfectly locally, just any classes that I use custom namespaces for do not work only on my web hosting account. In my classes I have at the top:
namespace Libs\Model;
I also tried using brackets
namespace Libs\Model {\\code here}
Tried researching the issue, coming up with nothing. Any suggestions? In the psr4 autoload file it shows:
'Libs\\' => array($baseDir . '/lib')
I verified $baseDir is pointed to the correct folder.
UPDATE
Here is code from the class Im trying to call. Very simple:
namespace Libs\Model;
class Site_Settings {
private $dbconn;
public function __construct($dbconn)
{
$this->dbconn = $dbconn;
}
public function findSiteSettings($domain)
{
//We clean any variables being passed to the query
$domain = $this->dbconn->escape($domain);
//We turn on query caching
$this->dbconn->cache_queries = TRUE;
//This is the query statement to run
$query = $this->dbconn->get_row("
SELECT
jp.*,
js.stateabb,
js.statename,
js.statecountry
FROM
job_site AS jp
INNER JOIN
job_state AS js
ON
jp.stateid = js.id
WHERE
jp.sitedomain = '$domain'
AND
jp.active = 1
LIMIT
1
");
//We turn off query caching
$this->dbconn->cache_queries = FALSE;
//We now return any rows found
return $query;
}
}
This is how Im calling it:
//We include the autoloader that is needed to load all vendors for this site
include(VENDORS .'autoload.php');
//We get the site settings for this job site
$settings = new Libs\Model\Site_Settings($global_db);
$site_settings = $settings->findSiteSettings($global_sitedomain);
This is my autoload file for psr4 from composer:
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname(dirname($vendorDir));
return array(
'Symfony\\Polyfill\\Mbstring\\' => array($vendorDir . '/symfony/polyfill-mbstring'),
'Stringy\\' => array($vendorDir . '/danielstjules/stringy/src'),
'Psr\\Http\\Message\\' => array($vendorDir . '/psr/http-message/src'),
'Libs\\' => array($baseDir . '/lib'),
'League\\Plates\\' => array($vendorDir . '/league/plates/src'),
'GuzzleHttp\\Psr7\\' => array($vendorDir . '/guzzlehttp/psr7/src'),
'GuzzleHttp\\Promise\\' => array($vendorDir . '/guzzlehttp/promises/src'),
'GuzzleHttp\\' => array($vendorDir . '/guzzlehttp/guzzle/src'),
'Cocur\\Slugify\\' => array($vendorDir . '/cocur/slugify/src'),
);
UPDATE #2
Here is my composer file
"autoload": {
"classmap": [
"lib/vendor/ezsql/mysqli/ez_sql_mysqli.php",
"lib/vendor/ezsql/shared/ez_sql_core.php",
"lib/helper/url.php",
"lib/helper/html.php",
"lib/helper/form_message.php",
"lib/helper/email_generator.php",
"lib/helper/pagination.php"
],
"psr-4": {"Libs\\": "lib"}
},
"require": {
"league/plates": "^3.1",
"guzzlehttp/guzzle": "^6.2",
"phpmailer/phpmailer": "^5.2",
"cocur/slugify": "^2.1",
"danielstjules/stringy": "^2.3",
"wixel/gump": "^1.3",
"jwage/purl": "^0.0.7"
}
}
You haven't provided enough information to really answer your question. Where is the php file located for Libs\Model\Site_Settings? Are you working on a case insensitive system like OSX and uploading to a case sensitive one like Linux? What auto-loading standard are you using? It looks like you are using both PSR-0 and PSR-4.
You may need to add a autoload path to your composer.json:
"autoload": {
"psr-4": {
"Libs\\Model\\": "lib/src/model",
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/"}
}
}
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.