Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my app crash when I change my namespace?

I'm following the material given in the book Easy Laravel 5 to learn Laravel. So I created the app as directed, and when I visited localhost:8000, I saw the welcome page.

Then I gave the name change command:

php artisan app:name todoparrot

To which the system responded with Application namespace set!. But now when I reload localhost:8000, I see nothing, and the terminal serving the app gives me a long list of errors:

PHP Fatal error:  Uncaught exception 'ReflectionException' with message 'Class todoparrot\Console\Kernel does not exist' in /media/common/htdocs/todoparrot/vendor/laravel/framework/src/Illuminate/Container/Container.php:776
Stack trace:
#0 /media/common/htdocs/todoparrot/vendor/laravel/framework/src/Illuminate/Container/Container.php(776): ReflectionClass->__construct('todoparrot\Cons...')
#1 /media/common/htdocs/todoparrot/vendor/laravel/framework/src/Illuminate/Container/Container.php(656): Illuminate\Container\Container->build('todoparrot\Cons...', Array)
#2 /media/common/htdocs/todoparrot/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(613): Illuminate\Container\Container->make('todoparrot\Cons...', Array)
#3 /media/common/htdocs/todoparrot/vendor/laravel/framework/src/Illuminate/Container/Container.php(229): Illuminate\Foundation\Application->make('todoparrot\Cons...', Array)
#4 /media/common/htdocs/todoparrot/vendor/laravel/framework/src/Illuminate/Container/Container.php(773): Illuminate\Container\Con in /media/common/htdocs/todoparrot/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 776

Now I'm not able to even run artisan, as I keep getting the same error. Any idea what might be broken because of this extremely simple change?

like image 376
ankush981 Avatar asked Apr 12 '15 05:04

ankush981


2 Answers

Try this command after renaming:

composer dump-autoload

Composer has autoload files at vendor/composer/ directory with your namespaces. when you change any you need to clear it and generate it again, and that command does it.

like image 93
ryanwinchester Avatar answered Sep 18 '22 17:09

ryanwinchester


If you run command below, it will change all the namespace and path to todoparrot. In your case Laravel not found "Class todoparrot\Console\Kernel does not exist". Make sure the namespace was changed to todoparrot

php artisan app:name todoparrot

The command will modify all the files below

modified:   app/Commands/Command.php
modified:   app/Console/Commands/Inspire.php
modified:   app/Console/Kernel.php
modified:   app/Events/Event.php
modified:   app/Exceptions/Handler.php
modified:   app/Http/Controllers/Auth/AuthController.php
modified:   app/Http/Controllers/Auth/PasswordController.php
modified:   app/Http/Controllers/Controller.php
modified:   app/Http/Controllers/HomeController.php
modified:   app/Http/Controllers/WelcomeController.php
modified:   app/Http/Kernel.php
modified:   app/Http/Middleware/Authenticate.php
modified:   app/Http/Middleware/RedirectIfAuthenticated.php
modified:   app/Http/Middleware/VerifyCsrfToken.php
modified:   app/Http/Requests/Request.php
modified:   app/Http/routes.php
modified:   app/Providers/AppServiceProvider.php
modified:   app/Providers/BusServiceProvider.php
modified:   app/Providers/ConfigServiceProvider.php
modified:   app/Providers/EventServiceProvider.php
modified:   app/Providers/RouteServiceProvider.php
modified:   app/Services/Registrar.php
modified:   bootstrap/app.php
modified:   composer.json
modified:   config/app.php
modified:   config/auth.php

Example in app/Console/Kernel.php the command will change this

namespace App\Console;

protected $commands = [
    'App\Console\Commands\Inspire',
];

To

namespace todoparrot\Console;

protected $commands = [
    'todoparrot\Console\Commands\Inspire',
];

Please check file app/Console/Kernel.php if "App\" path still available. If available please change to "todoparrot\".

I already try run command "php artisan app:name todoparrot". It not have any problem. I don't know why it will thrown error at yours.

like image 36
Faiz Avatar answered Sep 19 '22 17:09

Faiz