When you use php artisan in the command line you get a list of all commands. Are there any opportunity to get all Laravel artisan commands programmatically? I need it for example to use it in a select field in the interface.
To get all command objects:
\Artisan::all()
To get all command names:
array_keys(\Artisan::all())
use Illuminate\Contracts\Console\Kernel;
...
$commands = resolve(Kernel::class)->all();
Use shell-exec command.
http://php.net/manual/en/function.shell-exec.php
$output = shell_exec('php artisan');
echo $output;
if I understand the query correctly, you can use CLI (eg bash in debian) to:
&& for individual calls - each subsequent call will be made after the previous performance, eg:
php artisan migrate:fresh && php artisan fixtures:up && php artisan db:seed && php artisan serv
aliases entered in .bash_profile
alias migrate = "php artisan migrate"
and using command migrate in Laravel project.
info: https://gist.github.com/JeffreyWay/5542264
You can put them in your routes like this:
Route::get('/foo', function()
{
$exitCode = Artisan::call('command:name', ['--option' => 'foo']);
//
});
https://laravel.com/docs/5.0/artisan#calling-commands-outside-of-cli
Related
when i run php artisan key:generate in cmd it's return
file_get_contents(/project/positiv/core/vendor/psy//.env): failed to open stream: No such file or directory
at
/project/positiv/core/vendor/laravel/framework/src/Illuminate/Foundation/Console/KeyGenerateCommand.php:96
{
file_put_contents($this->laravel->environmentFilePath(), preg_replace(
$this->keyReplacementPattern(),
'APP_KEY='.$key,
file_get_contents($this->laravel->environmentFilePath())
));
}
I am trying to generate APP_KEY!!!
At first generate APP_KEY with the command php artisan key:generate --show. It will print contents on your terminal which you can copy and paste wherever you want. In this case APP_KEY=value on your .env file.
Generated Key format will be something like base64:xxxxxxxxxxxxxxxxxxxxxxx.
It seems that you are trying to run artisan command outside the project.
Make sure your terminal is targeting the exact project you want to trigger
I tried this command
Route::get('/updateapp', function()
{
\Artisan::call('dump-autoload');
echo 'dump-autoload complete';
});
and the page display this error:
The command "dump-autoload" does not exist.
I can't use exec() system() so I need to create this method/command.
I reviewed other questions but I'm confused help me please! I'm newbie
\Artisan::call('dump-autoload');
This command called above command
php artisan dump-autoload
You must use from command
And write in anywhere from php script
exec('composer dump-autoload');
I need to execute shell commands from controller , but not only for files inside the project , ex. system('rm /var/www/html/test.html') or system('sudo unzip /var/www/html/test.zip');
I call the function but nothing happen , any idea how to execute external shell commands from controller like removing one file in another directory?
system('rm /var/www/html/test.html');
//or
exec('rm /var/www/html/test.html')
If you're wanting to run commands from your PHP application I would recommend using the Symfony Process Component:
Run composer require symfony/process
Import the class in to your file with use Symfony\Component\Process\Process;
Execute your command:
$process = new Process(['rm', '/var/www/html/test.html']);
$process->run();
If you're using Laravel, you should be able to skip Step 1.
Alternatively, (if the process running php has the correct permissions) you could simply use PHP's unlink() function to delete the file:
unlink('/var/www/html/test.html');
I would do this with what the framework already provide:
1) First generate a command class:
php artisan make:command TestClean
This will generate a command class in App\Console\Commands
Then inside the handle method of that command class write:
#unlink('/var/www/html/test.html');
Give your command a name and description and run:
php artisan list
Just to confirm your command is listed.
2) In your controller import Artisan facade.
use Artisan;
3) In your controller then write the following:
Artisan::call('test:clean');
Please refer to the docs for further uses:
https://laravel.com/docs/5.7/artisan#generating-commands
Use fromShellCommandline to use direct shell command:
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
Process::fromShellCommandline('rm /var/www/html/test.html');
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
In Laravel, I am making a console command. Per the docs, you should be able to get the value of the argument using $this->argument():
$userId = $this->argument('user');
I have an argument that is an integer 1. However, $this->argument('some_name') is returning a string such as some_name=1, instead of simply 1
Is there a setting or something that I missed?
Arguments don't get named, unlike options. For example:
$ php artisan command argument1 argument2 argument3
$ php artisan command --option1=foo --option2=bar
So, I'd either change the definition of your argument to an option so that you can run:
$ php artisan command --some_name=1
Or, you can keep using this as an argument and run:
$ php artisan command 1
Note: artisan and command in the above examples are arguments of the php executable.
I have an external PHP script, that is processing an XML array to insert, update or delete rows in a database. This script lies in the root of the project in a folder called scripts and I can run and execute it via terminal with no problems whatsoever and it updates the database accordingly:
php index.php
I have also set up a schedule in Laravel (using October CMS syntax)
public function registerSchedule($schedule)
{
$schedule->exec(public_path() . '/script/index.php')->everyMinute();
}
This however is doing nothing. I tried manually running the schedule with artisan in command line by:
php artisan schedule:run
And the output is
Running scheduled command: /Users/x/x/x/x/scripts/index.php > '/dev/null' 2>&1 &
Nothing happens in the database tho.
Did you try to generate a new key?
php artisan generate:key