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

你。 提交于 2020-01-06 03:45:08

问题


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


回答1:


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:


check your .env file. Is it exists?




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/52476623/php-artisan-keygenerate-gives-a-no-application-encryption-key-has-been-speci

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!