我在laravel中申请了护照auth。我也在我的本地机器和AWS服务器上完成了这项工作。现在我正在尝试将其应用于共享主机,在那里我无法访问终端。所以我的好奇心只是知道是否可以申请没有php artisan passport: install
的护照?
答案 0 :(得分:0)
尝试使用连接的HTTP路由创建一个控制器,然后输入
Artisan::call('passport:install');
那里。然后转到url运行命令。
答案 1 :(得分:0)
将命令放在composer.json post安装脚本中:
"post-install-cmd": [
"php artisan passport:install"
]
除了安装后,您还可以使用许多不同的事件。 Composer events
答案 2 :(得分:0)
通常,您将在控制器中使用以下代码执行Artisan呼叫:
Artisan::call('passport:install');
但是,这不适用于passport:install,您将收到错误消息:
“护照”名称空间中没有定义命令
要解决此问题,必须在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,
]);
}
答案 3 :(得分:0)
此代码没有错误
shell_exec('php ../artisan passport:install');
答案 4 :(得分:0)
如果要在控制器中运行命令,这是您需要的完整代码,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',
],
];
答案 5 :(得分:0)
当不从控制台调用命令时,您的通行证迁移将不会运行,因为通行证仅在应用程序以控制台模式运行时才注册其命令。
要解决此问题,我们需要注册迁移和命令。
在您的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,
]);
}
}
}