问题
I have applied the passport auth in laravel. I have done this on my local machine and AWS server too. Now I am trying to apply the same to a shared hosting, where I won't be able to access the terminal. So my curiosity is just to know is it possible to apply for the passport without php artisan passport: install
?
回答1:
Try to make a controller with a connected HTTP route, and put
Artisan::call('passport:install');
there. Then go to the url to run the command.
回答2:
Usually you would use the following code in your controller to excute an Artisan call:
Artisan::call('passport:install');
However, this doesn't work on passport:install and you will get the error:
There are no commands defined in the "passport" namespace
To fix this you must add the following code to boot method at 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:
This code works with no error
shell_exec('php ../artisan passport:install');
回答4:
Put the command in your composer.json post install script:
"post-install-cmd": [
"php artisan passport:install"
]
There are many different events you can hook into other than post install as well. Composer events
回答5:
if you want to run commands in controller this is the complete code that you need, two commands will not run with php artisan and you should run it with 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>';
}
This is list of all commands
$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',
],
];
回答6:
Your passport migrations wont run when not calling a command from the console as passport only registers its commands when the application is running in console mode.
To get around this, we need to register the migrations and commands.
Do the following in your 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,
]);
}
}
}
来源:https://stackoverflow.com/questions/49295106/passport-without-php-artisan-passport-install-on-deploying-server