laravel “php artisan key:generate”给出“未指定应用程序加密密钥”。错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52476623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:05:04  来源:igfitidea点击:

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

phplaravelkeyartisan

提问by Ruben Hensen

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:

我有一个克隆的 Laravel 应用程序,但是当我尝试通过 php artisan key:generate 生成 APP_KEY 时,它给了我一个错误:

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

更新:通过这篇文章laravel 4: key not being generated with artisan修复了它

If I fill APP_KEY in my .env file php artisan key:generateworks. But a newly created app via laravel newwith a deleted APP_KEY can run php artisan key:generatewithout issue for some reason.

如果我在我的 .env 文件中填写 APP_KEYphp artisan key:generate有效。但是由于某种原因,新创建的应用程序通过laravel new删除 APP_KEY 可以毫无问题地运行php artisan key:generate

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

出于某种原因, php artisan key:generate 认为它需要一个 app_key 而它不需要。它也不会执行任何其他命令,它们都错误“未指定应用程序加密密钥”。

回答by Ruben Hensen

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

php artisan key:generate需要现有的密钥才能工作。用 32 个字符填充 APP_KEY 并重新运行命令以使其工作。

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

编辑:通过 laravel new 新创建的应用程序删除了 APP_KEY 可以运行 php artisan key:generate 出于某种原因没有问题。

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.

一年后编辑:真正的问题在于增加了 2 个提供商服务。boot() 函数写得不好导致了这个问题。仍然不确定为什么它不起作用,但我会尝试为以后可能遇到同样问题的人找出答案。

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

回答by Egretos

check your .envfile. Is it exists?

检查您的.env文件。它存在吗?

回答by Bi Hero

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

php artisan key:generate 是一个在 .env 文件中创建 APP_KEY 值的命令。

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.

当你运行 composer create-project laravel/laravel 命令时,它会在 .env 文件中生成一个 APP_Key,但是当你通过 git checkout 一个新分支或克隆一个新项目时,.env 文件不会包含,所以你必须运行 artisan key:generate 创建一个新的APP_KEY。

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

你改变了你的问题。在这种情况下,您可以尝试一下。php artisan 密钥:生成 php artisan 配置:缓存

回答by Sreejith BS

If you don't have a vendorfolder then,

如果你没有vendor文件夹,那么

1) Install composer dependencies

1)安装composer依赖

composer install

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

2)需要用命令生成一个应用密钥APP_KEY

php artisan key:generate

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

3)打开项目的代码编辑器,重命名.env.example,以.env和修改数据库名,用户名,密码,您的环境。

4) php artisan config:cacheto effect the changes.

4)php artisan config:cache影响变化。