I was try on a beginners course php. I had a issue about autouploading with composer. I had Fatal error: Uncaught Error: Class 'app\Email' not found in...
My directory structure looks like this
autoloading
-app
Email.php
Person.php
-src
-composer.json
-index.php
in index.php
require_once "vendor/autoload.php";
$email = new app\Email();
$person = new app\Person();
And in composer.json had
"autoload": {
"psr-4": {
"Dung6\\Autoloading\\": "src/",
"App\\": "app/"
}
},
in "App\\": "app/" I was try ./app, /app/,... and I was composer dump-autoload every time I change. But nothing work at all. Some one can check it for me. Thank you!
Related
i've already been looking for solutions to my problem but could'nt find any so far.
{
"name": "petersil98/thresh",
"version": "1.0.0",
"type": "library",
"autoload": {
"psr-4": {
"Thresh\\": "src/"
}
}
}
This is my public/test.php:
<?php
require_once '../vendor/autoload.php';
use Thresh\Helper\Config;
Config::setPlatform("euw1");
And this is my src/Helper/Config.php:
<?php
namespace Thresh\Helper;
class Config{...}
This is the error i get: Fatal error: Uncaught Error: Class 'Thresh\Helper\Config' not found
First i had my psr-4 autoload registered with the prefix 'src'
"autoload": {
"psr-4": {
"src\\": "src/"
}
}
After changing the psr-4 autoload prefix to Thresh (and updating the namespaces) and running composer dump-autoload it doesnt work anymore
PS: composer dump-autoload returns Generated autoload files containing 0 classes
I must be missing something ; I am trying to run some tests but the classes are never found due to namespace.
Here is my structure.
-app
-tests
-Unit
-TestInterface.php
-common
-MyTest.php
Here is my TestInterface.php:
namespace App\Tests\Unit;
interface TestInterface
{
}
Here is my MyTest.php:
namespace App\Tests\Unit\common;
use App\Tests\Unit\TestInterface;
class MyTest implements TestInterface
{
}
Here is the relevant part of composer.json:
"autoload": {
"psr-4": {
"App\\": "src/",
"spec\\": "spec/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
Here is the error:
PHP Fatal error: Interface 'App\Tests\Unit\TestInterface' not found
What am I missing here?
Answering myself, the code is fine.
I was loading PHPUnit from another.phar. Installed PHPUnit via composer:
composer require --dev phpunit/phpunit ^8
and used it from ./bin and it worked fine.
I have my own little MVC framework and I use composer psr-4 autoloading.
On my own computer it works perfectly fine, but when I deployed it to my Ubuntu server it did not work anymore. (it doesn't find any classes anymore) I have tried a lot of things but it just won't work whatever I try...
What I have tried:
composer dump-autoload
composer update
removing everything and uploading again
searching on internet for a couple hours... :(
This is my composer.json:
{
"autoload": {
"psr-4": {
"App\\": "app",
"Core\\": "core",
"Magister\\": "vendor/Magister"
}
},
"require": {
"philo/laravel-blade": "^3.1"
}
}
I just don't get it why it's not working on my server....
I am using an other version of php on my server: 7.1, and I am using 5.6 on my computer, but this shouldn't make any difference right?
How do I fix this problem? I just don't get it why it happens.... :(
EDIT:
My code:
Index.php:
<?php
require "core/app.php";
$app = new \Core\App();
echo $app->start();
app.php:
<?php
namespace Core;
require "./vendor/autoload.php";
class App
{
function start()
{
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL ^ E_DEPRECATED);
$MC = new Routing();
// This is where it fails. Get the error: "class Core\Routing not found"
Routing.php:
<?php
namespace Core;
Use App\routes;
class Routing
{
private $parameters = [];
public function GetMC($Getroute){
}
}
File structure on server:
I have excluded the vendor map from the tree
okay... I have fixed it.
I have changed my composer.json to this:
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Core\\": "core/",
"Magister\\": "vendor/Magister/"
},
"classmap": [
"app/",
"core/",
"vendor/Magister/"
]
},
"require": {
"philo/laravel-blade": "^3.1"
}
}
If you want to use psr-4 you need to capitalize your directories to
app
- Modules
- Controllers
- Views
-- Layouts
...
Please refer to this post as to why your autoloading isn't working.
I am having very hard times understanding how to use autoloading by psr-4.After loading vagrant and setting and testing all variables in Homestead.yaml I have prepared a file structure as the following:
\app
\folder
-- test.php
\vendor
\composer
-- autoload.php
-- index.php
-- composer.json
and the following are my codes:
index.php
<?PHP
namespace app;
require('vendor/autoload.php');
$object = new folder\test();
composer.json
"autoload":{
"psr-4":{
"app\\": "app"
}
}
test.php
<?php
namespace app\folder;
class test
{
function __construct ()
{
echo 'construction done right.';
}
}
But, after trying to visit the page, these are the error message displayed on the page:
(!) Fatal error: Uncaught Error: Class 'app\folder\test' not found in /home/vagrant/web/sites/app/index.php on line 6
( ! ) Error: Class 'app\folder\test' not found in /home/vagrant/web/sites/app/index.php on line 6
Would you help me understand and fix this error?
It doesn't work because you have told Composer that the classes from the app namespace are in the app subdirectory but there is no app subdirectory.
The entire application is stored in the app directory and it's name doesn't really matter for the application. The classes of the app namespace are stored in the current directory and the sub-namespaces are stored in subdirectories with the same name.
Accordingly, your composer.json file should read:
"autoload": {
"psr-4": {
"app\\": ""
}
}
Or, to be more clear, you can put . (the current directory) as the location of the app\ namespace:
"autoload": {
"psr-4": {
"app\\": "."
}
}
After you make the change run composer dump-autoloader in the main application directory and it will start working.
To fix it for your current setup, use the following:
"autoload":{
"psr-4":{
"app\\": ""
}
}
Your composer.json is in the app-directory, so there's no subdirectory named app to reference.
I would actually recommend to change your directory structure to the following:
\app
\src
\folder
-- test.php
-- index.php
\vendor
\composer
-- autoload.php
-- index.php
-- composer.json
And then in composer.json, set the following:
"autoload":{
"psr-4":{
"app\\": "src"
}
}
This makes sure that all files belonging to your 'app' namespace are contained within a single subdirectory.
Finally, I would recommend you to use a vendor namespace to prevent conflicts, and to use the naming guidelines from PSR-2.
I think you won't need PSR-4 just add classmap
classmap parameter for example :
"autoload": {
"classmap": [
"app"
]
}
After adding this run composer dump-autoload and you should see a number of classes being added.
Hope this helps.
in composer.json try to set
"autoload":{
"psr-4":{
"": "src/"
}
}
then do execute this command
composer dump-autoload -o
I'm looking at examples, and I cannot get my code to work.
Directory Structure
app
src
company
FileExport
FileExport.php
FileExportInterface.php
Validator
vendor
...
My composer.json
"require": {
"monolog/monolog": "1.9.1",
"ilya/belt": "2.1.1"
},
"autoload": {
"psr-4": {"Company\\": "src"}
}
Namespace is Company\FileExport.
Classes in vendor work fine, but not mine. I've run composer update as well.
Your autoload should look like so
"autoload": {
"psr-4": {"Company\\": "src/company/"}
}