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.
Related
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 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
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/"
}
I wanted to start a new 3.3 project in Symfony and started as usual:
1.) Creating the new project: symfony new ArtProject
2.) Creating a new Bundle: php app/console generate:bundle (Paul/ArtBundle, yml, src/)
Then I run the local server and when I open 127.0.0.1:8000 I get this beautiful message:
(1/1) ClassNotFoundException
Attempted to load class "PaulArtBundle" from namespace
"Paul\ArtBundle". Did you forget a "use" statement for another
namespace? in AppKernel.php (line 19)
Which is strange and I haven't figured out why this happen so far. Before creating the Bundle, there was no error; I saw the typical startpage of symfony.
public function registerBundles()
{
$bundles = [
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
......
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new AppBundle\AppBundle(),
new Paul\ArtBundle\PaulArtBundle(),
];
}
<?php
namespace Paul\ArtBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class PaulArtBundle extends Bundle
{
}
Any idea whats going on there? I did not change a thing, I only ran these commands.
I just installed a fresh copy of S3.3.4 (latest version as of this writing) using:
composer create-project symfony/framework-standard-edition s334 "3.3.4"
bin/console generate:bundle
Share across multiple apps: yes
namespace: Paul\ArtBundle
bundle name: PaulArtBundle
Target Directory: src/
Refreshed the browser and sure enough I got the class not found message.
The generate:bundle command is not updating the autload section of composer.json when a new namespace is introduced. Edit composer.json and:
# composer.json
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle",
"Paul\\": "src/Paul"
},
"classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},
Then run
composer dumpautoload
And restart the server(maybe). That should fix the bundle class not found problem.
I have not used the generate:bundle command is quite some time ever since the standard was to put everything under AppBundle so I don't know how long this has been "broken". But at least three questions in the last week or so indicates it was something recent.
And by the way, when I refreshed the browser I got "Hello World" which threw me for a bit. Turns out the new bundle overrides the / route which is also sort of special.
And in case anybody is wondering why this started happening, Symfony 3.2 changed from
#composer.json
"psr-4": { "": "src/" },
To
"psr-4": { "AppBundle\\": "src/AppBundle" },
You could always just change it back though I think spelling out individual namespaces might be "better". Not sure.
And here is an issue with more details: https://github.com/symfony/symfony-standard/issues/1098
Looks like the maintainer favored a tiny speed improvement over breaking an existing command. Oh well. Symfony Flex is supposed to make everything great again.
If you generate a bundle for usage in multiple projects (with own namespace) you need to add it in the composer.json as follwed:
Lets assume your bundle name is CompanyFooBundle with namespace Company\Bundle\FooBundle then the composer autoload section should look like:
...
"autoload": {
"psr-4": {
"Company\\Bundle\\FooBundle\\": "src/Company/Bundle/FooBundle"
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
...
This works for me in:
Generate your Bundle with
./console generate:bundle
And follow the steps as always, now, do what you want in your composer.json file with the line
"AppBundle\\": "src/AppBundle"
Replace with "": "src/" or add your bundle, for example: "BackendBundle\\": "src/BackendBundle"
Here's the new part:
Install composer in your bin directory, copy and paste the steps from https://getcomposer.org/download/
Up a level in your project directory, and in your root folder (of your project) run the next command
php ./bin/composer.phar update
Removing vendor dir and again running composer install helped me with same problem.
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.