I am making my own framework but I have a problem with psr4 autoload and composer.
I can not make classes available and apparently everything is fine
This is the error, I don't want to use include for class files:
Fatal error: Uncaught Error: Class 'System\Template\get_template_files_class' not found in /var/www/html/fsib/system/layout-compiler.php:7 Stack trace: #0 {main} thrown in /var/www/html/fsib/system/layout-compiler.php on line 7
composer.json
{
"require": {
"illuminate/database": "*",
"illuminate/routing": "~5.1.16",
"illuminate/events": "~5.1.16"
},
"autoload": {
"psr-4": {
"System\\" : "system/libs/"
}
}}
layout-compiler.php
<?php
require_once '../vendor/autoload.php';
use System\Template\get_template_files_class;
$templates = new get_template_files_class;
?>
get_templates_files_class.php
<?php
namespace System\Template
class get_template_files_class {
}
?>
Related
I'm using phpunit for test.
I've put the autoload, psr-4 in cofiguration.json
{
"require-dev": {
"phpunit/phpunit": "^7.5"
},
"autoload": {
"psr-4":{
"Src\\": "src/"
}
}
}
After that, I executed this command in cmd composer dump-autoload, and the test doesn't work. The outputs are that:
C:\xampp\htdocs\phpUnitPractice>.\vendor\bin\phpunit tests\EasyTest.php
PHP Fatal error: Uncaught Error: Class "PHPUnit\TextUI\Command" not found in C:\xampp\htdocs\phpUnitPractice\vendor\phpunit\phpunit\phpunit:61
Stack trace:
#0 {main}
thrown in C:\xampp\htdocs\phpUnitPractice\vendor\phpunit\phpunit\phpunit on line 61
Fatal error: Uncaught Error: Class "PHPUnit\TextUI\Command" not found in C:\xampp\htdocs\phpUnitPractice\vendor\phpunit\phpunit\phpunit:61
Stack trace:
#0 {main}
thrown in C:\xampp\htdocs\phpUnitPractice\vendor\phpunit\phpunit\phpunit on line 61
That information goes inside composer.json not configuration.json, have a look at this composer.json example. Then run composer dump-autoload.
So, you should have a file like this:
{
"require": {
YOUR REQUIRES
},
"require-dev": {
"phpunit/phpunit": "^7.5"
},
"autoload": {
"psr-4":{
"Src\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
}
I am trying to create a shopping cart for my website, I am following the tutorial below: https://www.youtube.com/watch?v=tRh467FX12U&list=PLfdtiltiRHWH9JN1NBpJRFUhN96KBfPmd&index=2
In the tutorial, he uses 3 different imports from GitHub and this is how installed them in the terminal using composer:
rosscurrie = Name of ~user
#Ross-Air = Name of Macbook
MobileMastersNew = Name of the folder that holds all website files
composer = is installed globally
require <...> = the imports from GitHub
rosscurrie#Rosss-Air MobileMastersNew % composer require slim/slim:^4.0
rosscurrie#Rosss-Air MobileMastersNew % composer require slim/twig-view:^3.0
rosscurrie#Rosss-Air MobileMastersNew % composer require php-di/slim-bridge
rosscurrie#Rosss-Air MobileMastersNew % composer require illuminate/database
I have limited experience with Laravel but not completely unfamiliar. When I try to load the index.php page it gets this error:
Fatal error: Uncaught Error: Class 'DI\Bridge\Slim\App' not found in /Users/rosscurrie/Sites/MobileMastersNew/app/App.php:8 Stack trace: #0 /Users/rosscurrie/Sites/MobileMastersNew/vendor/composer/ClassLoader.php(444): include() #1 /Users/rosscurrie/Sites/MobileMastersNew/vendor/composer/ClassLoader.php(322): Composer\Autoload\includeFile('/Users/rosscurr...') #2 [internal function]: Composer\Autoload\ClassLoader->loadClass('Cart\\App') #3 /Users/rosscurrie/Sites/MobileMastersNew/bootstrap/app.php(9): spl_autoload_call('Cart\\App') #4 /Users/rosscurrie/Sites/MobileMastersNew/public/index.php(3): require('/Users/rosscurr...') #5 {main} thrown in /Users/rosscurrie/Sites/MobileMastersNew/app/App.php on line 8
My folder directory is as follows:
My ../MobileMasters/app/App.php is:
<?php
namespace Cart;
use DI\ContainerBuilder;
use DI\Bridge\Slim\App as DIBridge;
class App extends DIBridge
{
protected function configureContainer(ContainerBuilder $builder)
{
$builder->addDefinitions([
'settings.displayErrorDetails' => true,
]);
//
}
}
My ../MobileMasters/bootstrap/app.php is:
<?php
session_start();
use Cart\App;
require __DIR__ . '/../vendor/autoload.php';
$app = new App;
My ../MobileMasters/public/.htaccess file is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
My ../MobileMasters/public/index.php is:
<?php
require __DIR__ . '/../bootstrap/app.php';
$app->run();
My ../MobileMasters/vendor/autoload.php is:
<?php
// autoload.php #generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit2e9ebb4be0f95ed0dbfb26486b8ba4b7::getLoader();
Lastly, my ../MobileMasters/composer.json is:
{
"require": {
"slim/slim": "^4.0",
"slim/twig-view": "^3.0",
"php-di/slim-bridge": "^3.0",
"illuminate/database": "^7.2"
},
"autoload": {
"psr-4": {
"Cart\\": "app"
}
}
}
You should use use statement after autoloading:
<?php
namespace Cart;
use DI\ContainerBuilder;
use DI\Bridge\Slim\App as DIBridge;
class App extends DIBridge
{
protected function configureContainer(ContainerBuilder $builder)
{
$builder->addDefinitions([
'settings.displayErrorDetails' => true,
]);
//
}
}
And change this file also:
session_start();
require __DIR__ . '/../vendor/autoload.php';
use Cart\App;
$app = new App;
You are using wrong namespace use DI\Bridge\Slim\App as DIBridge;. There is no App class in DI package anymore.
Instead
use the following code use DI\Bridge\Slim\Bridge as DIBridge;
Your composer.json needs to look almost exactly like the one in the tutorial. I had the exact same problem. To require the older versions, you just have to do something like composer remove slim/slim then composer require slim/slim ^3.0.
In particular, the your php-di needs to be lower than v1.1. My composer.json:
{
{
"require": {
"slim/slim": "^3.0",
"slim/twig-view": "^2.1",
"illuminate/database": "^5.2",
"php-di/slim-bridge": "v1.0.2"
},
"autoload": {
"psr-4": {
"Cart\\": "app/"
}
}
}
This is a problem that drove me crazy for over one week. I installed php-di via composer and added my own project to the composer.json file:
{
"name": "mypackage/platform",
"description": "MyPackage Platform",
"type": "project",
"require": {
"php-di/php-di": "^6.0"
},
"autoload": {
"psr-4": {
"MyPackage\\": "src/"
}
}
}
Then I created mine /public/index.php:
<?php
require_once dirname(__DIR__) . '/vendor/autoload.php';
use DI\ContainerBuilder;
use function DI\create;
use MyPackage\Base\Service;
$containerBuilder = new ContainerBuilder();
$containerBuilder->useAutowiring(false);
$containerBuilder->useAnnotations(false);
$containerBuilder->addDefinitions([
Service::class => create(Service::class)
]);
$container = $containerBuilder->build();
$service = $container->get('Service');
$service->showMessage();
And this is /src/Base/Service.php content:
<?php
namespace MyPackage\Base;
class Service
{
public function __construct()
{
}
public function showMessage()
{
echo "<BR>Inside Service";
}
}
When I troy to load index.php, Apache says:
[Thu Dec 05 18:57:47 2019] [error] PHP Fatal error: Uncaught DI\\NotFoundException: No entry or class found for 'Service' in /var/www/html/vendor/php-di/php-di/src/Container.php:135
Stack trace:
#0 /var/www/html/public/index.php(22): DI\\Container->get('Service')
#1 {main}
thrown in /var/www/html/vendor/php-di/php-di/src/Container.php on line 135
Whats is wrong with my approach?
$container->get('Service') asks the container for the entry 'Service'. This is a string. There is no namespace.
In your configuration, you configured the service under the Service::class key: Service::class returns the full class name, which is MyPackage\Base\Service.
So the error is: you have the service under MyPackage\Base\Service, and you try to fetch it via the key Service.
I keep getting this error I checked the composer.json file and everything seems to be alright I'm following a tutorial for a shopping cart online in php. Any help would be greatly appreciated it.
I checked a similar question and he corrected in the composer.json file and that's not the issue with my problem.
bootstrap-app.php
<?php
session_start();
$app = new app;
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../app/routes.php';
index.php
<?php
require __DIR__ . '/../bootstrap/app.php';
$app->run();
composer.json
"autoload": {
"psr-4": {
"Cart\\": "app/"
}
},
I've also have dumped the file before and no changes. I'm new to php and I'm just trying to complete this tutorial so I can complete an e-commerce website.
Fatal error: Uncaught Error: Class 'app' not found in
C:\Users\Logan\Cart\bootstrap\app.php:4 Stack trace: #0 C:\Users\Logan\Cart\public\index.php(3): require() #1 {main} thrown in C:\Users\Logan\Cart\bootstrap\app.php on line 4
Edited to include this:
<?php
namespace Cart;
use DI\ContainerBuilder;
use DI\Bridge\Slim\App as DIBridge;
class App extends DIBridge
{
protected function configureContainer(ContainerBuilder $builder)
{
$builder->addDefintions (
['settings.displayErrorDetails' => true,] );
$builder->addDefinitions(__DIR__ .'/container.php');
}
}
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/"
}