Very recently, my application stopped working properly for some reason and I had to change my application namespace, so it is projectname\Model instead of App\Model.
Now after this change, everything started working normally, except php artisan commands.
When I call php artisan, I can have the lists of artisan commands but none works, I always get [RuntimeException] Unable to detect application namespace..
For exemple, I tried running php artisan make:controller ShoppingCartController and I get this error.
I looked online and a lot of people say it's a problem with the composer.json, but I tried composer diagnose and nothing stands out, and I updated composer and also tried composer dump-autoload.
And in my composer.json, I have this part I changed recently:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"projectName\\": "App/"
}
},
And I carefully looked to be sure there was no extra comma in the .json.
I'd like to still be able to use php artisan commands so I'd like to solve this.
Thanks,
When you rename your default namespace, the second parameter should be the name of the 'app' folder. so instead of
"projectName\\": "App/"
You should have
"projectName\\": "app/"
Related
I decided to run "composer update" command another time to get the app back up to date but now it's completely broken as it can't find a certain class anymore.
Fatal error: Class 'App\Application' not found in /home/rlvpr/public_html/webroot/index.php on line 33
All files in the vendor folder have been properly uploaded, so yes also all the composer autoload class mappings ...
I really have no clue what I should do now :/
Anyone got any suggestions?
Thanks!
Try running composer dumpautoload.
If that doesn't work, check your composer.json for the autoload section:
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
Then dump autoload again. Of course, tweak the source folder path to suit where your namespace folder is!
Run:
composer dump-autoload
See also dump-autoload
In a project (on which work 2), we are forced to make a "php composer.phar dump-autoload" and "php composer.phar dump-autoload -o" every time we add a new file (a new class in our web project).
If we do not type these lines of command, the autoloader dial tells us that this class is not found ...
Whereas with personal projects, I have never been compelled to type these command lines for each new class of creation.
In my composer.json I have put this:
"autoload": {
"psr-4" : {
"App\\": "app/",
"Database\\": "app/",
"Helpers\\": "app/",
"": [
"app/",
"lib/"
]
}
}
Thus I does not understand, why, for every time we add a file in the folder " app ", we have to make "php composer.phar dump-autoload" and "php composer.phar dump-autoload -o" ...
ps : I have Classes without Namespace in directories "app" and "lib".
Will you have an idea of where it could come please?
Thank you.
The only reason for having to dump the autoloader after adding a new class is when you are using classmap autoloading, and no PSR-0 or PSR-4.
This should be clearly indicated in the composer.json file of that application, which would be something like "autoload":{"classmap":[...]}.
If this is NOT present, there would be no requirement to dump the autoloader.
If however composer dump-autoload does not really work, and composer dump-autoload -o is needed, you have errors in your PSR definition of autoloading.
I'm confused whether your excerpt from your composer.json is from your nonworking example or not - all I can say is that it is supposed to work without dumping. It still is not optimal, though.
I created folder name - Birds and in this folder i created class Birds.php then add this to composer.json,
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Birds\\": "Birds/",
}
},
To load i have to run - composer dump-autoload -o and that's fine for first time to take that folder, But then i created new interface class like this :
namespace Birds\Validator;
interface BadgeInterface
{
public function test();
}
Interface class not working until in run composer dump-autoload,
My question is why i need to run this every time ? i am using laravel as framework.
Thanks
That's just how composer works. Laravel is just a PHP framework, so it is not able to run composer or other tool all the time for you.
If you need to automatically add files all the time, you can just use cron or Laravel's schedule command. It will do all the work for you every 5 minutes, for example.
This is cause when you create any class composer folder autoload.php can't update . that cause you need to run it manually . it clear you when you install any package at last command there run is composer autoload to update all reference in autoload.php file
I am developing a library for Laravel which contains a service provider. I have added this library to another project's composer.json file.
The composer.json file for the "main project" contains the following scripts.
"scripts": {
"post-root-package-install": [
"php -r \"copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"php artisan key:generate"
],
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"pre-update-cmd": [
"php artisan clear-compiled"
],
"post-update-cmd": [
"php artisan optimize"
]
},
I can include the library dependency just fine, except for one thing; the pre-update-cmd and post-update-cmd scripts throw an error and cause me a lot of headaches. When running sudo composer update to update the dependencies, I get the following error.
$ sudo composer update
> php artisan clear-compiled
PHP Fatal error: Class 'MyName\MyProject\MyAwesomeServiceProvider' not found in /Users/Me/dev/MyProject/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php on line 146
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'MyName\MyProject\MyAwesomeServiceProvider' not found
Script php artisan clear-compiled handling the pre-update-cmd event returned with an error
[RuntimeException]
Error Output: PHP Fatal error: Class 'MyName\MyProject\MyAwesomeServiceProvider'
not found in /Users/Me/dev/MyProject/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php on line 146
I have Googled around quite a bit before asking this question and read through pretty much everything related that I could find. Apparently this is a known issue that has been discussed in multiple GitHub issues within the Laravel repository. However, I have yet to find a workaround, even after having tried multiple ones.
It appears that the issue is that the Artisan commands bootstrap Laravel, which leads to an error because the service provider is not available at this point - or something like that. Moving the clear-compiled command to post-update-cmd causes the same error, which surprises me a bit because I thought the service provider would be available at this point.
The only thing that works for me is to manually comment out the line that includes the service provider in config/app.php before running composer update and then adding it again afterwards. I have been doing this for a few hours, and it is already bothering the heck out of me, and I really cannot believe that this issue is around.
Does anyone know how to work around this error so that I don't get the error that my service provider is not found when updating the Composer dependencies for my project?
EDIT:
Here is the composer.json file for the library.
{
"name": "my-name/my-project",
"type": "library",
"authors": [
{
"name": "My Name",
"email": "test#example.com"
}
],
"require": {
"php": ">=5.5.0",
"laravel/framework": "~5.2"
},
"autoload": {
"classmap": [],
"psr-4": {
"MyName\\MyProject\\": "src/"
}
}
}
Edit
This issue has finally been resolved as of laravel/framework:v5.2.25 and laravel/laravel:v5.2.27, and backported to laravel/framework:v5.1.33 and laravel/laravel:v5.1.33.
This fix includes a change to the Laravel application (laravel/laravel), in addition to the Laravel Framework (laravel/framework). To implement, you will need to:
1) Update the scripts section of your composer.json file to match that in the laravel/laravel package. Specifically:
remove the pre-update-cmd section
in the post-install-cmd section, replace "php artisan clear-compiled" with "Illuminate\\Foundation\\ComposerScripts::postInstall"
in the post-update-cmd section, replace "php artisan clear-compiled" with "Illuminate\\Foundation\\ComposerScripts::postUpdate"
2) Once you have updated your composer.json, run a composer update. If you only want to update the framework, you can run composer update laravel/framework.
Original
After looking over the Github issue you posted in the comments, as well as the related issues, you may be in for a bit of a wait. Taylor would like to put a script in vendor/bin and change composer.json to run that, but it looks like they are waiting for a PR from the community, and won't actually implement this themselves.
You haven't done anything wrong; your autoloading is setup correctly. The issue is with Laravel right now.
Moving the command to the post-update-cmd script doesn't work because artisan will always try to load the cache files when they exist. When running the clear-compiled command, artisan loads the cache files (part of startup) before it ever tries to delete them.
Your best bet is to manually delete the cache files before artisan gets run. And, you need to do it outside of Laravel/Artisan. So, you can manually delete the files, or you can create a little script to do it and add that to your composer.json file (for your main project, not your package).
Files to delete:
Laravel 5.2:
bootstrap/cache/compiled.php
bootstrap/cache/services.php
Laravel 5.1:
bootstrap/cache/compiled.php
bootstrap/cache/services.json
Laravel 5.0:
vendor/compiled.php
storage/framework/compiled.php
vendor/services.json
storage/framework/services.json
When using composer install or composer update you can use --no-scripts option to skips execution of scripts defined in composer.json.
e. g.: composer update --no-scripts.
Source: https://getcomposer.org/doc/03-cli.md#install
composer update --no-scripts
this command will ignore command defined in composer.json,otherwise it will excute laravel command which will check if serviceProvider is loaded.
It looks like you're simply not including your ServiceProvider. Put this in your root project's composer.json:
{
"autoload": {
"psr-4": {
"App\\": "app/",
"MyName\\MyProject\\": "../relative/path/to/serviceprovider/"
}
}
}
Run This
composer install --ignore-platform-reqs
I have an app I've been working on for a few weeks; I'm using Vagrant+Homestead for local development, and Forge+Linode for a staging environment; until a few days ago, my workflow was working fine, however now when the deployment script runs in Forge (in particular composer install), it gets an error:
[RuntimeException]
Error Output: PHP Fatal error: Class 'Reputationhub\ReputationhubServiceProvider' not found in /home/vagrant/Sites/reputationhu
b/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php on line 157
PHP Stack trace:
PHP 1. {main}() /home/vagrant/Sites/reputationhub/artisan:0
PHP 2. require_once() /home/vagrant/Sites/reputationhub/artisan:30
PHP 3. require() /home/vagrant/Sites/reputationhub/bootstrap/start.php:60
PHP 4. Illuminate\Foundation\ProviderRepository->load() /home/vagrant/Sites/reputationhub/vendor/laravel/framework/src/Illumin
ate/Foundation/start.php:210
PHP 5. Illuminate\Foundation\ProviderRepository->compileManifest() /home/vagrant/Sites/reputationhub/vendor/laravel/framework/
src/Illuminate/Foundation/ProviderRepository.php:57
PHP 6. Illuminate\Foundation\ProviderRepository->createProvider() /home/vagrant/Sites/reputationhub/vendor/laravel/framework/s
rc/Illuminate/Foundation/ProviderRepository.php:121
It is actually the subcommand php artisan clear-compiled that composer runs afterward that is failing. If I run that on it's own, same problem. Running composer install --no-script runs ok.
When I look at vendor/composer/autoload_classmap.php, it's missing all sorts of things, and is only a few lines long (it's normally much larger), so whatever Laravel is doing is not properly finding the correct classes.
The odd thing is that running composer dump-autoload -o seems to fix it; the autoload_classmap.php file finds everything, and the app runs fine, until php artisan ... tries to do anything, then it breaks the app.
The end result is that the Forge deployment is broken; I can manually jump onto the server and run composer dump-autoload -o to fix it, but that seems wrong.
I think I've been rather thorough about researching solutions, but so far I can't find anything, so any help would be appreciated.
UPDATE: further clarification
This is Laravel 4.2.
My composer.json autoload section looks like this:
...
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-4": {
"Reputationhub\\": "app/"
},
"files": []
},
...
The provider has been added to the "providers" array in app.php like this:
...
'Reputationhub\ReputationhubServiceProvider',
...
And my provider file (at app/Reputationhub/ReputationhubServiceProvider.php) looks like this:
<?php namespace Reputationhub;
use Reputationhub\EventSubscribers\MetricsEventSubscriber;
use Illuminate\Support\ServiceProvider;
class ReputationhubServiceProvider extends ServiceProvider {
...
}
Update 2: more testing
Not sure what it does, but composer install seems to do something bad that breaks the app. To fix it, rather than using composer install in the deployment script (which will subsequently call php artisan clear-compiled and php artisan optimize, both of which will throw errors), if I use this it will repair itself:
...
composer install --no-scripts
composer dump-autoload -o
php artisan dump-autoload
...