Artisan output not working for route:cache - laravel

Why doesn't this code show any output result?
$exitCode = \Artisan::call('route:cache');
$artisanOutput = \Artisan::output();
dd($artisanOutput);
Another artisan console code example shows a result, but this one does not work.

if your app environment is production then you should run
$exitCode = \Artisan::call('route:cache',['--force'=> true]);
$artisanOutput = \Artisan::output();
dd($artisanOutput);
Hope this helps

As per your comment i read. Redirect is what you are trying to achieve.
why not use this instead?
return redirect('adminSettings')->with('success', $successMessage);
And your route should look like this:
Route::get('adminSettings', 'AdminController#adminSettings');
Looking at your original question
$exitCode = \Artisan::call('route:cache');
$artisanOutput = \Artisan::output();
dd($artisanOutput);
Can you try 'php artisan route:cache' from your terminal? What is the output? I am using 5.6 and the output gives me some error. So i think that's the issue.

Related

How to get Artisan::call('command:name') output in a variable?

Is it possible to get $output as follows:
$output = Artisan::call('command:name');
I have tried many solutions in different posts but it didn't work on my Laravel 5.2.
You can call the output method on Artisan
Artisan::call('command:name');
$output = Artisan::output();
Make sure you are using one of the available output methods like $this->line('output'); in the actual Artisan command. More info in the docs.
There are several ways to accomplish this, but as your are using such old version of Laravel maybe the best for your case is one that will not require a rewrite when you finally migrate to a newer version. Have you tried perhaps the vanilla PHP methods system, shell_exec and passthru?
system will return just the last line of the command on succes and FALSE on failure
shell_exec will return the entire output but without a way to fetch the output status code
passthru will not return any output instead it will output all on stdout. Although it can be put into a variable using output cache methods (i.e. ob_start and ob_get_contents)
In any case you should call those methods using as argument the CLI version of the command you wish to run:
$output = shell_exec("php artisan command:here");
P.S. If you by any chance have a user input that you want to pass as parameter to a artisan command, make sure you escape it with escapeshellcmd first.

Laravel, call Artisan from php differs from command line

I have a route which calls the Artisan facade to execute:
Artisan::call('queue:work --once');
And I get :
But it's strange because in the command line, if i do:
php artisan queue:work --once
Works everything ok:
I can use other routes to call for example:
Artisan::call('config:clear');
And works ok too. Any idea?
the options value staring with -- are not passed to the string
you can try :
Artisan::call('queue:work', ['--once' => true]);
Laravel 5.8 introduced this new method of calling artisan commands:
Artisan::call('queue:work --once');
In previous releases use this:
Artisan::call('queue:work', ['--once' => true]);
to call an artisan command from the code and passing some options you need to use an array as a 2nd argument to Artisan::call()
like so :
Artisan::call('queue:work', ['--once' => true]); // or whatever options you need

Php artisan - custom command

I would like to create my own php artisan command so when I write
php artisan env:[variable]
I want to get that variable, so I can check fast if I'm getting the right variable from .env file
Is this possible? How to do it?
You can create a console command with:
php artisan make:console
As per the docs.
You are probably better off creating a static signature and passing in the field you want to return.
protected $signature = 'env:return {field}';
This will allow you to type php artisan env:return APP_DEBUG.
You can retrieve the input like so:
public function handle()
{
$envVariable = $this->argument('field');
//
}
You can then write some code to output the field, perhaps something like:
$this->info(env($envVariable));
This could all be condensed down into something like the below, as an example only:
protected $signature = 'env:return {field}';
public function handle()
{
$this->info(env($this->argument('field')));
}
I would suggest you read the docs I linked above as this will help you understand what is happening here a lot more.
This should get you started though, and you can then shape it to be exactly what you need.

How to send an argument when using Artisan::call?

I'm trying to run an Artisan command with an argument, but I can't figure out how. How do you do it? If I run php artisan video:webmtomp4 N in the terminal, it works fine.
Artisan::call(
'video:webmtomp4',
[$data['videoId']],
new StreamOutput(fopen(storage_path() . '/logs/artisan.log', 'w'))
);
How do you send an argument to the command when using Artisan::call? $data['videoId'] is set, so that is not a problem.
Changing my comment to an answer after checking the documentation. :)
The parameters array should be associative, e.g. ['argument' => $data['videoId']],.

Doctrine 2 CLI commands from within PHP

Hey all. I am writing a program that will transform some data in our database, and then call Doctrine to build YAML files from said Mysql Database structure. I have Doctrine working from within PHP. However I can't figure out how to call the CLI commands from within PHP. Following is the Doctrine 2 CLI command that does what I need.
php ./doctrine orm:convert-mapping --filter="users" --from-database yml ./test
This command works from the Linux command line, but how to I do this same thing via Doctrine objects? I don't want to just use the PHP exec statement to send a command to the shell. I wish to use the Doctrine object model.
Don!:
Apparently this is not a very common programming method. However, I have used Doctrine from PHP by calling it via the PHP EXEC command. I know you said that you would not like to do it this way. However, it actually works quite well. Below is an example of such a solution.
$cmd_string = "php ./doctrine orm:generate-entities --generate-annotations=1 --regenerate-entities=1 $this->entity_file_dir";
$result = array();
exec($cmd_string, &$result);
Hope this helps,
-Don!
I stumbled upon this question when trying to execute a command directly from a PHP script, without using the CLI.
Particularly, I was needing to call orm:ensure-production-settings.
Each Doctrine command has its own class: http://www.doctrine-project.org/api/orm/2.4/namespace-Doctrine.ORM.Tools.Console.Command.html
I solved it the following way:
$entityManager = ...; // Get the entity manager somehow in your application
// Creates the helper set
$helperSet = \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($entityManager);
// Initializes the desired command and sets the helper set
// In your case it should be ConvertMappingCommand instead
$command = new \Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand();
$command->setHelperSet($helperSet);
// Initializes the input
// Alternatives: http://api.symfony.com/2.0/Symfony/Component/Console/Input.html
$input = new \Symfony\Component\Console\Input\ArgvInput(); // Input coming from the CLI arguments
// Initializes the output
// Alternatives: http://api.symfony.com/2.0/Symfony/Component/Console/Output.html
$output = new \Symfony\Component\Console\Output\ConsoleOutput(); // Prints the output in the console
// Runs the command
$command->run($input, $output);
I'm new to Doctrine so I'm not exactly sure how this works, but it does. Any comment is appreciated.

Resources