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.
Related
I am using gabrielbull/ups-api in my laravel project
composer.json as follows
"autoload": {
"psr-4": {
"App\\": "app/",
"Ups\\": "vendor/gabrielbull/ups-api/src"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
The controller code as follows :
use Ups\Rate;
$rate = new Ups\Rate($accessKey, $userId, $password);
but I am getting an error
Class 'App\Http\Controllers\Ups\Rate' not found
Your controller can't find the Ups\Rate.
You should be able to do:
$rate = new Rate($accessKey, $userId, $password);
If not: You should be able to debug this quick with the following code.
require __DIR__ . '/vendor/autoload.php'
use Ups\Rate;
new Rate()
echo Rate::class; // output
It's a PHP package, so once you install it via composer, it's already autoloaded. You don't have to mess with the composer.json file. After installing run:
composer dumpautoload
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 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've been testing getting my packages up to Packagist with a simple class I made. Whenever I require it in a different project, it says the class cannot be found. Is something wrong with my composer.json autoload block?
Here's my project repo file structure:
- src
- prodikl
- View.php
- .gitignore
- composer.json
And here's my composer.json:
{
"name":"prodikl/simple-view",
"description":"A simple to use, basic View object.",
"require" : {
"php" : ">=5.3.0"
},
"autoload": {
"psr-4": {"prodikl": "src/"}
}
}
And finally, in my View.php:
<?php
namespace prodikl;
class View
{
...
}
But whenever I require it into a project and do require "vendor/autoload.php" and use use prodikl\View; it it keeps saying not found
You just need to point your autoloader down one more directory:
"autoload": {
"psr-4": {"prodikl": "src/prodikl/"}
}
This means "classes that belong to the \prodikl namespace can be found in the src/prodikl/ directory."
You might need trailing backslashes on the namespace name too, not sure how picky Composer is about it:
"psr-4": {"prodikl\\": "src/prodikl/"}
I just need to autoload some classes, and I don't like the psr-0 namespace insanity (no offense).
This used to work just fine in my project:
"psr-0": {
"": [
"app/controller/",
"app/model/"
]
}
For some reason it doesn't work anymore, even though I'm using the same Composer version. I need it for a new project that is also using Silex. Could this be a conflict with Silex?
I know about the "classmap" option, but it's kind of useless because it requires that I run "composer install" every time I add a new class.
Any ideas?
Try to use "primitive" JSON properties; not an array (like in your example).
This works for me with psr-4 like you say, with "": "app/":
{
"autoload": {
"psr-4": {
"Robbie\\": "core/",
"": "app/"
}
},
"require": {
"monolog/monolog": "1.2.*"
}
}
This gives me the Robbie namespace under the directory core, as an example of sources not controlled by composer, the 3rd party (vendor) Monolog namespace and my default or non-namespace for sources underneath the app directory.
After a composer update, all of them are available when including the generated autoload.php:
<?php
require_once 'vendor/autoload.php';
// ...
?>
Use classmap in instead of psr-4:
"autoload": {
"classmap": ["models/"]
}
If you just want to regenerate the autoload file use composer dump-autoload.