Why is $this->argument() Showing the Name and the Value? - php

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.

Related

Too many arguments, expected arguments "command"

I'm trying to run a custom command through the terminal, but keep getting the error: Too many arguments, expected arguments "command".
When searching for similar issues all i've been able to find was people using the scheduler, and nothing about calling the command from terminal.
The signature for the command i'm trying to run is:
class MigrateSiteMysiteCommand extends Command
{
protected $signature = 'migrate:site:mysite.dk {--from=} {--to=}';
...
And then i use php artisan help migrate:site:mysite.dk i get this:
Usage:
migrate:site:mysite.dk [options]
Options:
--from[=FROM]
--to[=TO]
-h,--help Display this help message
-q,--quiet Do not output any message
-V,--version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n,--no-interaction Do not ask any interactive question
--env[=ENV] The environment the command should run under
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose
output and 3 for debug
I've tried running the command in different ways formatting the arguments, but get the same error either way:
php artisan migrate:site:mysite.dk --from=2019-02-27 16:22:10 --to=2019-02-28 23:59:59
php artisan migrate:site:mysite.dk --from='2019-02-27 16:22:10' --to='2019-02-28 23:59:59'
php artisan migrate:site:mysite.dk --from '2019-02-27 16:22:10' --to '2019-02-28 23:59:59'
php artisan migrate:site:mysite.dk --from 2019-02-27 16:22:10 --to 2019-02-28 23:59:59
Any suggestions to what i'm missing, or messing up in the command ?
I've been able to run other commands, which didn't need any arguments passed.
The parameters had to be wrapped in " instead of ', so the working command looks like this:
php artisan migrate:site:mysite.dk --from="2019-02-27 16:22:10" --to="2019-02-28 23:59:59"

Get all Laravel artisan commands for interface

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

Getting Undefined index: REQUEST_URI - When Run Artisan Commands in Laravel

I keep getting the error below every time I try to run an artisan command on Laravel, I'm on the project directory.
For example, I run this command:
php artisan make:migration create_stats_table
And I get this error:
[ErrorException]
Undefined index: REQUEST_URI
No matter what command I run, I get the same error, even php artisan --version returns this error. How can I solve this problem?
Your code expects to have this index, but you're running PHP in CLI mode.
REQUEST_URI variable of $_SERVER superglobal is only available if you're reaching script by browser.
As mentioned already this variable doesn't exist if you run by the CLI. If you can't get around the issue and avoid using it a work around is to use isset()
if (isset($_SERVER['REQUEST_URI'])){
...
}

Symfony console, file paths, /dev/fd/0 trick

I have my own symfony console commands, some of which take a filename as the argument.
eg.
$ php app/console mycommands:doTheThing /path/to/my/file.txt
I have tried using the /dev/fd/0 trick to use a string instead of a file here, however it's still not working.
eg.
$ php app/console mycommands:doTheThing php://dev/fd/0 <<<'myFileContents'
Could anybody provide any insight or help?
You can use process substitution:
$ php app/console mycommands:doTheThing <(echo "foobar")

Cakephp ShellDispatcher error in Centos Cronjob

Im trying to run a cronjob in Centos with crontab -e, but I can't figure out when the job runs why it does not find any parameters as -app in the ShellDispatcher. I have read
Cake PHP Cronjobs and have:
Edited .bashrc and added export PATH="$PATH:/home/phonekar/public_html/app/Console"
Added a cronjob to my useraccount with crontab -e
53 23 * * * /home/useraccount/public_html/lib/Cake/Console/cakeshell Tracking -cli "/usr/bin" -console "/home/useraccount/public_html/lib/Cake/Console" -app "/home/useraccount/public_html/app" >> /home/useraccount/public_html/tracking.log 2>&1
Manually running this command works fine, but when I let the job run i get this error on a loop:
PHP Warning: array_search() expects parameter 2 to be array, null given in /home/useraccount/public_html/lib/Cake/Console/ShellDispatcher.php on line 320
PHP Warning: array_splice() expects parameter 1 to be array, null given in /home/useraccount/public_html/lib/Cake/Console/ShellDispatcher.php on line 324
I looked in to the file and it seems like it is expecting the -app value but instead is getting a null value passed to it. I have also verified that the Tracking shell works. Any clarification will help.
Warning: array_splice() expects parameter 1 to be array, null given in ShellDispatcher.php
I hit this error using console of cakephp 2.3.8 but you can encounter this issue on other versions as well.
The solution is to edit (command line arguments are not seen)
app/Console/cake and on on the line:
exec php -q "$CONSOLE"/cake.php -working "$APP" "$#"
after -q add
-d register_argc_argv=1
so the line will look like:
exec php -q -d register_argc_argv=1 "$CONSOLE"/cake.php -working "$APP" "$#"
ref:
http://www.ecommy.com/programming/cakephp/warning-array_splice-expects-parameter-1-to-be-array-null-given-in-shelldispatcher-php

Categories