Ruben Hensen
Ruben Hensen

Reputation: 846

"php artisan key:generate" gives a "No application encryption key has been specified." error

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."

Upvotes: 1

Views: 24782

Answers (6)

Isti115
Isti115

Reputation: 2756

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=

Upvotes: 0

Harpreet Singh
Harpreet Singh

Reputation: 51

  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.

Upvotes: 4

Ruben Hensen
Ruben Hensen

Reputation: 846

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;
    }
}

Upvotes: 4

Sreejith BS
Sreejith BS

Reputation: 1203

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.

Upvotes: 0

Quan Van
Quan Van

Reputation: 339

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

Upvotes: 0

Egretos
Egretos

Reputation: 1175

check your .env file. Is it exists?

Upvotes: -1

Related Questions