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
Related
Laravel version 5.6
I'm attempting to add some classes to my project via files under autoload.
Composer.json
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
},
"files" : [
"app/helpers/dbUsers.php",
"app/helpers/dbMoodChart.php"
]
},
Whenever I do a composer dump-autoload i get errors on both files saying files not found.
PHP Warning: Uncaught ErrorException:
require(C:\laravel-projects\project\vendor\composer/../../app/helpers/dbUsers.php):
failed to open stream: No such file or directory in
C:\laravel-projects\project\vendor\composer\autoload_real.php:66
Stack trace:
Warning: Uncaught ErrorException:
require(C:\laravel-projects\MoodWatch\vendor\composer/../../app/helpers/dbUsers.php):
failed to open stream: No such file or directory in
C:\laravel-projects\MoodWatch\vendor\composer\autoload_real.php:66
Stack trace:
The strange thing is its trying to load the files from the composer directory not the apps root folder. I've done this in another project and didn't have any issues. I've also compared my composer.json autoload sections on both projects and I can't see any syntax differences so I'm a bit puzzled as to why it's loading at the wrong location.
Has anyone seen anything similar before and be able to shed light on why it's doing this?
I found out the name of the file had a typo and once fixed both errors went away
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.
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.
Using Laravel 5.2, I created a folder on my local machine inside of the app directory. The name of the folder is mailers. Inside of that folder are 2 classes.
I updated my composer.json file like this:
"autoload": {
"classmap": [
"database",
"app/foo"
],
"psr-4": {
"Acme\\": "app/"
}
},
After running composer update and composer dump-autoload, everything works fine on the local machine. However, after uploading the code to the live server, I run composer dump-autoload and receive the following error:
[RuntimeException]
Could not scan for classes inside "app/foo" which does not appear to be a file
nor a folder
How can I get my custom class recognized on the live server? Any help with this would be greatly appreciated. Cheers :)
Background
I know what I'm trying to do sounds a bit wrong but I do have my reasons.
Basically I have a central core app that's a default laravel app with a few tweaks and boilerplate code, I have then developed a series of packages that can be used to extend the app through composer. These packages are not meant to function without the core framework so a dependency upon it is fully expected.
What I want to do
What I would like to do is have a BaseController in my core app and have the various controllers in my package extend this BaseController to provide universal functionality throughout the various module packages.
I was expecting to be able to place the base controller in app/controllers/BaseController.php
and then extend it from my package using:
class PackageController extends \BaseController{}
Unfortunately when I do this it still looks within the package (currently workbenched) for the controller and I get the error:
include(/var/www/l4core.dev/workbench/myvendor/mypackage/src/controllers/BaseController.php):
failed to open stream: No such file or directory
Can anyone tell me what I'm doing wrong here. I am looking for a solution which allows me to easily move my packages between vendor dir and workbench for development. Any help greatly appreciated
Update
The previously mentioned error message appears to have been due to an include in my packages /vendor/composer/classloader.php - I have now deleted the vendor directory and done a fresh composer install. This has not solved the problem but it has at least shifted it as I now get the following error message:
Class 'BaseController' not found
My Packages composer.json
{
"name": "modules/sesame",
"description": "",
"authors": [
{
"name": "any",
"email": ""
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "4.0.x",
"zizaco/confide": "dev-master",
"zizaco/entrust": "dev-master",
"conarwelsh/mustache-l4": "dev-master"
},
"autoload": {
"classmap": [
"src/controllers",
"src/models",
"src/migrations",
"src/seeds"
],
"psr-0": {
"Modules\\Sesame": "src/"
}
},
"minimum-stability": "dev"
}
Be sure to execute:
php artisan dump-autoload
And verify that your class BaseController is in /vendor/composer/autoload_classmap.php.
OR like the OP stated, removing the vendor directory and running composer install again could sometimes solve the problem.