没有 php artisan 护照的护照:在部署服务器上安装

Passport without php artisan passport: install on deploying server

我已经在laravel申请了护照认证。我也在我的本地机器和 AWS 服务器上完成了这个。现在我试图将相同的应用到共享主机,在那里我将无法访问终端。所以我的好奇心只是想知道是否可以在没有php artisan passport: install的情况下申请护照?

尝试制作一个连接了 HTTP 路由的控制器,然后把

Artisan::call('passport:install');

那里。然后转到url到运行命令。

将命令放入您的 composer.json post 安装脚本中:

"post-install-cmd": [
    "php artisan passport:install"
]

除了 post 安装之外,您还可以挂接许多不同的事件。 Composer events

通常您会在控制器中使用以下代码来执行 Artisan 调用:

Artisan::call('passport:install');

但是,这不适用于 passport:install,您将收到错误消息:

There are no commands defined in the "passport" namespace

要解决此问题,您必须将以下代码添加到 AppServiceProvider.php 的启动方法中:

<?php

namespace App\Providers;

use Laravel\Passport\Console\ClientCommand;
use Laravel\Passport\Console\InstallCommand;
use Laravel\Passport\Console\KeysCommand;
use Laravel\Passport\Passport;

use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Schema::defaultStringLength(191);
    Passport::routes();

    /*ADD THIS LINES*/
    $this->commands([
        InstallCommand::class,
        ClientCommand::class,
        KeysCommand::class,
    ]);
}

这段代码没有错误

shell_exec('php ../artisan passport:install');

如果你想在控制器中使用 运行 命令,这是你需要的完整代码,两个命令不会 运行 和 php artisan,你应该 运行 它与 shell

public function getCommand($command)
    {
        echo '<br> php artisan ' . $command . ' is running...';
        $output = new BufferedOutput;
        if(strpos($command, 'api') === false && strpos($command, 'passport') === false){
            Artisan::call($command, [], $output);
        }else{
            shell_exec('php ../artisan ' . $command);
            dump('php ../artisan ' . $command);
        }
        dump($output->fetch());
        echo 'php artisan ' . $command . ' completed.';
        echo '<br><br><a href="/admin/setting/advance">Go back</a>';
    }

这是所有命令的列表

$commands = [
            [
                'id' => 1,
                'description' => 'recompile classes',
                'command' => 'clear-compiled',
            ],
            [
                'id' => 2,
                'description' => 'recompile packages',
                'command' => 'package:discover',
            ],
            [
                'id' => 3,
                'description' => 'run backup',
                'command' => 'backup:run',
            ],
            [
                'id' => 4,
                'description' => 'create password for passport',
                'command' => 'passport:client --password',
            ],
            [
                'id' => 5,
                'description' => 'install passport',
                'command' => 'passport:install',
            ],
            [
                'id' => 6,
                'description' => 'create a document for api',
                'command' => 'apidoc:generate',
            ],
            [
                'id' => 7,
                'description' => 'show list of routes',
                'command' => 'route:list',
            ],
            [
                'id' => 8,
                'description' => 'recompile config cache',
                'command' => 'config:cache',
            ],
            [
                'id' => 9,
                'description' => 'clear config cache',
                'command' => 'config:clear',
            ],
            [
                'id' => 10,
                'description' => 'run lastest migrations',
                'command' => 'migrate',
            ],
            [
                'id' => 11,
                'description' => 'run seeders',
                'command' => 'db:seed',
            ],
            [
                'id' => 12,
                'description' => 'recompile route cache',
                'command' => 'route:cache',
            ],
            [
                'id' => 13,
                'description' => 'clear route cache',
                'command' => 'route:clear',
            ],
            [
                'id' => 14,
                'description' => 'recompile view cache',
                'command' => 'view:cache',
            ],
            [
                'id' => 15,
                'description' => 'clear view cache',
                'command' => 'view:clear',
            ],
            [
                'id' => 16,
                'description' => 'optimize all configurations',
                'command' => 'optimize',
            ],
        ];

如果不从控制台调用命令,您的 passport 迁移不会 运行,因为 passport 仅在应用程序 运行ning 处于控制台模式时注册其命令。

为了解决这个问题,我们需要注册迁移和命令。

AuthServiceProvider 中执行以下操作:


<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Laravel\Passport\Console\ClientCommand;
use Laravel\Passport\Console\InstallCommand;
use Laravel\Passport\Console\KeysCommand;
use Laravel\Passport\Passport;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * @var array
     */
    protected $policies = [
        // 'App\Model' => 'App\Policies\ModelPolicy',
    ];

    public function boot(): void
    {
        Passport::routes();

        if ($this->app->environment !== 'production') {

            $this->loadMigrationsFrom(base_path('vendor/laravel/passport/database/migrations'));

            $this->commands([
                InstallCommand::class,
                ClientCommand::class,
                KeysCommand::class,
            ]);
        }        
    }  
}