1

I have a cloned laravel application but when I try to generate a APP_KEY via php artisan key:generate it gives me an error:

In EncryptionServiceProvider.php line 42:
No application encryption key has been specified.

Which is obvious because that is exactly what I'm trying to create. Does anybody know how to debug this command?

update: Kind of fixed it with this post laravel 4: key not being generated with artisan

If I fill APP_KEY in my .env file php artisan key:generate works. But a newly created app via laravel new with a deleted APP_KEY can run php artisan key:generate without issue for some reason.

For some reason php artisan key:generate thinks it needs a app_key when it doesn't. It won't do any other commands either, they all error "No application encryption key has been specified."

2
  • Are you working on laravel 4 ? Sep 24, 2018 at 10:11
  • @sumitsharma composer.json says laravel/framework: 5.6.* Sep 24, 2018 at 10:15

6 Answers 6

4

php artisan key:generate needs an existing key to work. Fill the APP_KEY with 32 characters and rerun the command to make it work.

Edit: A newly created app via laravel new with a deleted APP_KEY can run php artisan key:generate without issue for some reason.

Edit a year later: The real problems lays in 2 added provider services. The boot() functions are badly written which causes the problem. Still not exactly sure why it doesn't work but I'll try and figure it out for somebody who may have the same problem later.

The two files in question

<?php

namespace App\Providers;

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Routing\ResponseFactory;

class ResponseServiceProvider extends ServiceProvider
{
    public function boot(ResponseFactory $factory){
        parent::boot();
        $factory->macro('api', function ($data=null, $code=null, $message=null) use ($factory) {
            $customFormat = [
                'status' => 'ok',
                'code' => $code ? $code : 200,
                'message' => $message ? $message : null,
                'data' => $data
            ];

            if ($data instanceof LengthAwarePaginator){
                $paginationData = $data->toArray();
                $pagination = isset($paginationData['current_page']) ? [
                    "total" => $paginationData['total'],
                    "per_page" => (int) $paginationData['per_page'],
                    "current_page" => $paginationData['current_page'],
                    "last_page" => $paginationData['last_page'],
                    "next_page_url" => $paginationData['next_page_url'],
                    "prev_page_url" => $paginationData['prev_page_url'],
                    "from" => $paginationData['from'],
                    "to" => $paginationData['to']
                ] : null;

                if ($pagination){
                    $customFormat['pagination'] = $pagination;
                    $customFormat['data'] = $paginationData['data'];
                }
            }

            return $factory->make($customFormat);
        });
    }

    public function register(){
        //
    }
}
<?php

namespace App\Providers;

use App\Http\Controllers\Auth\SocialTokenGrant;
use Laravel\Passport\Bridge\RefreshTokenRepository;
use Laravel\Passport\Bridge\UserRepository;
use Laravel\Passport\Passport;
use Laravel\Passport\PassportServiceProvider;
use League\OAuth2\Server\AuthorizationServer;

/**
 * Class CustomQueueServiceProvider
 *
 * @package App\Providers
 */
class SocialGrantProvider extends PassportServiceProvider{
    /**
//     * Bootstrap any application services.
//     *
//     * @return void
//     */
    public function boot(){
        parent::boot();
        app(AuthorizationServer::class)->enableGrantType($this->makeSocialRequestGrant(), Passport::tokensExpireIn());
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register(){
    }

    /**
     * Create and configure a SocialTokenGrant based on Password grant instance.
     *
     * @return SocialTokenGrant
     */
    protected function makeSocialRequestGrant(){
        $grant = new SocialTokenGrant(
            $this->app->make(UserRepository::class),
            $this->app->make(RefreshTokenRepository::class)
        );
        $grant->setRefreshTokenTTL(Passport::refreshTokensExpireIn());
        return $grant;
    }
}
2
  1. Run following command:
php artisan config:cache
  1. Now run:
php artisan key:generate
  1. Again run:
php artisan config:cache

Now you can check in console if application key was set successfully.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    May 26, 2023 at 11:00
0

php artisan key:generate is a command that create a APP_KEY value in your .env file.

When you run composer create-project laravel/laravel command it will generate a APP_Key in .env file, but when you checkout a new branch by git or clone a new project, the .env file will not include, so you have to run artisan key:generate to create a new APP_KEY.

You changed your question. In this case, you can try it. php artisan key:generate php artisan config:cache

1
  • The problem is that with a .env file the commands error instead of giving me the app_key. For some odd reason it thinks it needs an app key to create a new one. Which is not true, because a fresh project is able to create a app_key without a different one present. Sep 24, 2018 at 10:11
0

If you don't have a vendor folder then,

1) Install composer dependencies

composer install

2) An application key APP_KEY need to be generated with the command

php artisan key:generate

3) Open Project in a Code Editor, rename .env.example to .env and modify DB name, username, password to your environment.

4) php artisan config:cache to effect the changes.

2
  • I do. composer install installs all the dependencies and then also errors out because there is no APP_KEY. Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover Script @php artisan package:discover handling the post-autoload-dump event returned with error code 1 In EncryptionServiceProvider.php line 42: No application encryption key has been specified. Sep 24, 2018 at 10:18
  • My DB name, username and password are all correct. (checked with a fresh project) Sep 24, 2018 at 10:21
0

It is really a weirdly worded error, as telling the user "I couldn't generate the key as it's missing." seems a bit silly given that the user requested the generation of the key in the first place precisely because of the fact that it is misssing... Anyway, after a bit of trial and error I have figured out that what it's really complaining about is not the actual value, but the place to put it.


TL;DR: Put an empty APP_KEY field into your .env file like shown below and php artisan key:generate will be happy to fill it for you!

APP_KEY=
-1

check your .env file. Is it exists?

1
  • Yes it does, I just edited my post for some more details. Sep 24, 2018 at 9:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.