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
Related
I'am trying to retrieve EXAMPLE_URL=www.google.com from .env in controller and everytime get null. Where is the problem because the same code works on another application. This function doesn't work after php artisan cache:clear.
Controller code
$hostname = env("EXAMPLE_URL");
dump($hostname);
You should not use env() outside of the config files.
Read: https://laravel.com/docs/8.x/configuration
You should add the env variable to a config file and use config('example.url');.
The example.php would look like:
return [
'url' => env('EXAMPLE_URL', 'https://example.com'),
];
Always use the app file as the middleman
see : https://laravel.com/docs/9.x/configuration
I'm using Laravel Page Speed.
This library create many problems with some styles in my admin pages and i want to skip that.
//config/laravel-page-speed.php
//You can use * as wildcard.
'skip' => [
'*.pdf', //Ignore all routes with final .pdf
'*/downloads/*',//Ignore all routes that contain 'downloads'
'assets/*', // Ignore all routes with the 'assets' prefix
];
But when i want skip my admin url, nothing changed
'skip' => [
'*/admin/*',
];
Do you have any idea about this problem?
Update: I also run these command after any change
php artisan config:cache
php artisan cache:clear
You have wrong syntax there. It should be like this :
'skip' => [
'admin\*'
];
Check this issue from Github :
https://github.com/renatomarinho/laravel-page-speed/issues/45
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.
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']],.
I'm creating a custom artisan command (foo) for my Laravel 4 application. I can see from the userguide how to accept arguments and options, e.g.
php artisan foo argument --option
But what if I want to have a custom artisan command that has many 'methods', in the same style as some built-in artisan commands, e.g. migrate:install?. I want to make something like:
php artisan foo:baz argument --option
What is the recommended way to implement different methods based on the : colon separator? All I've found to try so far is to make an entirely new artisan command for each 'method'. Is there a more efficient way?
You're correct, you do need a new Artisan command (or rather a class) for each method. However all you have to do is register each file in app/Console/Kernel.php, if you decide to change the syntax later than Laravel will pick it up any changes to $signature automatically without you needing to rename any files.
For the Laravel example you mention, migrate, there's a directory with a separate file for each command name (including the one that has no colon):
/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations
migrate -> MigrateCommand.php
migrate:install -> InstallCommand.php
migrate:refresh -> RefreshCommand.php
migrate:reset -> ResetCommand.php
migrate:rollback -> RollbackCommand.php
migrate:status -> StatusCommand.php
If you have code you want to reuse (DRY) note that if you examine the above commands, some of them use traits, e.g. Illuminate\Console\ConfirmableTrait – which contains a confirmToProceed() method which, if it's running in production, will ask the user if they really want to continue.
NB: the Artisan syntax changed in 5.1, from $name (with a rather complicated way of specifying arguments and options) to the much simpler $signature, it's backwards compatible. More info
You just have to set the name:
protected $name = 'foo:baz';