Key is a duplicate in ./composer.json - php

Am trying structure my app in such a way that all my models will be in a dedicated directory(in my case Classified). I created the directory with Laravel app directory and added it to my my composer.json file. Below is the structure of my composer.json file:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"Classified\\": "app/",
"Classified\\": "app/Classified"
}
},
Then I run composer dump-autoload in my terminal but I keep getting "Key Classified\ is a duplicate in ./composer.json at line 29
" and when I tried viewing my app in the browser i get:
Fatal error: Uncaught exception 'ReflectionException' with message 'Class App\Http\Kernel does not exist' in /home/vagrant/Workspace/codulabproducts/classified/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 736.
Line 29 in my composer.json file is
"Classified\\": "app/Classified"
I don't know what am doing wrong because I have followed these steps in my other project and everything went well.

You can define more than one directory for a namespace prefix. But in that case the value for the key must be a list and not a string (see the second example in the documentation):
{
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"Classified\\": ["app/", "app/Classified"]
}
}
}

You can't have duplicated keys in your psr-4 mapping. It is supposed to define the root folder for given namespace and a namespace cannot have multiple roots.
Remove one of the mappings for Classified\ namespace.

Related

Laravel 5.5 - ReflectionException (-1) Class does not exist

I am trying to use ReflectionClass to get file name(from app directory) from a controller. For testing whether I can use the ReflectionClass or not i was trying in following way:
In my MyController.php
public function readContent()
{
$files = app_path() . DIRECTORY_SEPARATOR. "Drama.php";
// It returns "F:\xampp\htdocs\projectDirectory\app\Drama.php"
$class = new ReflectionClass($files);
echo "file name:: ". $class->getFileName();
}
I have a Drama.php file in this path. But when I am running the route for this method, I get following error
ReflectionException (-1)
Class F:\xampp\htdocs\projectDirectory\app\Drama.php does not exist
I have updated my composer.json file like following:
"autoload": {
"classmap": [
"app",
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
So that, i can read my app directory files.
I also ran the following commands
composer dump-autoload
composer update
php artisan config:clear
php artisan cache:clear
But i am still getting this error. Can anyone tell me how can i resolve this ?
Your issue here is ReflectionClass takes the class path as the constructor argument, not file path. try new ReflectionClass('\App\Drama') instead.

TestCase in laravel package not found

I'm converting my open source app as a package and it always throws error
PHP Fatal error: Class 'Laracommerce\Tests\TestCase' not found in .... on line 18
Based on other comments on every search I made, I just need to define it in my package composer.json's autoload-dev the location of my tests but still getting the error.
You are calling Laracommerce\Tests namespace but into the composer file you declared Laracommerce\Core\Tests
you need to:
a. change classes namespaces Laracommerce\Core\... to Laracommerce\...
b. or simply modify your composer file like this
"autoload":{
"psr-4": {
"Laracommerce\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Laracommerce\\Tests\\": "tests/"
}
},
Then you need to run composer dump-autoload
Try running a composer install, sometimes I get the same error but it gets fixed after that.

Class file not found by composer and psr-4

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

Composer runs locally but errors on production

I'm working within the Laravel framework, and have a file loading successfully, pulled in through my Composer's files array. I can run composer update locally without issue -- I'm using the functions in the file.
However, when deploying to Digital Ocean, Composer throws an error, suggesting that it's looking for the file in the vendors directory (even though it's not looking there locally).
Fatal error: composerRequire0b4716b00b8bec4a70dbf5ea5e415661(): Failed
opening required
'/home/forge/myapp.com/vendor/composer/../../app/helpers/myHelper.php'
(include_path='.:/usr/share/php') in
/home/forge/myapp.com/vendor/composer/autoload_real.php on line 66
And the autoload section:
"autoload": {
"classmap": [
"database"
],
"files": [
"app/helpers/myHelper.php"
],
"psr-4": {
"App\\": "app/"
}
},
I think this question probably has what I need but I don't know if the difference between loading classes and files makes it irrelevant to this issue (clearly I'm confused about more than just this issue!):
How to I use Composer to autoload classes from outside the vendor?
try to delete all text in composer.lock and update composer

Class not found after auto loading in Laravel

I am struggling with PHP Namespaces and Auto loading in Laravel 5.4. I actually decided to put the models that I need to use in a definite and named folder as app/Models:
php artisan make:model Models/Customer
I have set Customer model's namespace to namespace Models;
Then, I created a route as follows:
Route::get('customer', function () {
$customer = \App\Models\Cutomer::find(1);
echo $customer;
});
To do auto loading work I opened the composer.json file located in the very root folder of the Laravel project and made the autoload to be as follows:
"autoload": {
"classmap": [
"database",
"app/Models"
],
"psr-4": {
"App\\": "app/",
"App\\Models\\": "App/Models"
}
},
After all of this I checked if my composer is an update version and ran dump-autoload:
composer self-update
composer dump-autoload -o
There is also worth of notice the contents of vendor/composer/autoload_classmap.php:
'App\\Models\\Customer' => $baseDir . '/app/Models/Customer.php',
'App\\Models\\Test\\Test' => $baseDir . '/app/Models/Test.php',
My problem is that whenever I execute the url: http://127.0.0.1:8000/customer I will encounter the following error output:
(1/1) FatalThrowableError
Class 'App\Models\Cutomer' not found
Would you please help me understand where of my work has been incorrect, and how to fix it? Thank you very much in advance.
The namespace in the model should be namespace App\Model; (hence why you call the class as \App\Models\Cutomer)
When you use php artisan make:model Models/Customer it should have set the namespace correctly then.
Also, you don't need to edit your composer.json to do this. Remove the additions you've made to your composer.json file for the autoloading and then run composer dumpautoload from the command line.
Hope this helps!
First, you have an error in your PSR-4 setup.
"App\\Models\\": "App/Models"
This does not map to a valid directory. Secondly, and more importantly, there is no need to autoload a nested namespace of one that is already declared. By autoloading App you will already autoload any nested namespaces as long as they conform to PSR-4 standards i.e namespace as directory name and filename as class name.
Composer setup only needs to be the following:
"psr-4": {
"App\\": "app/"
}

Categories