I need to check if a Laravel Console Command exists and if it is in the protected command var to call them.
I need to call them from another Laravel console command. And I want to know if there are something like exists_command('mycommand:foo')
There are any way to achieve this?
Tested and working.
function command_exists($name)
{
return array_has(\Artisan::all(), $name);
}
if (command_exists('config:cache')) {
// success
}
Though #Sandeesh is right, we can check in this way:
function exists_command($name)
{
return array_key_exists($name, \Artisan::all());
}
if (exists_command('mycommand:foo')) {
// success
}
even in a shorter way,
function exists_command($name)
{
return $this->getApplication()->has($name);
}
php artisan list
Will bring up all possible artisan commands. There is a 'command' subsection with your own created commands.
You would call them as follows
php artisan command:MyCreatedCommand
Edit: To check if a command exists in your project you can use php class_exists function
if(class_exists('App\Console\Commands\MyCommandName')){
//Do whatever
}
Related
In my laravel app, I noticed that every route is executed twice, and can't figure out why
for example:
Route::get('called_twice', function () {
dump('---');
});
return string '---' twice
Edit:
trying to backtrace the source of the issue, I put a dump in file
src/Illuminate/Foundation/Http/Kernel.php
protected function sendRequestThroughRouter($request)
{
$this->app->instance('request', $request);
Facade::clearResolvedInstance('request');
$this->bootstrap();
dump('kernel');
return (new Pipeline($this->app))
->send($request)
->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
->then($this->dispatchToRouter());
}
and another dump in the constructor of the file
src/Illuminate/Pipeline/Pipeline.php
public function __construct(Container $container = null)
{
dump('pipeline');
$this->container = $container;
}
and I get this:
the Pipeline class is called many time
Laravel 6.8.0
I think $next($request) is probably called twice in a middleware. The reason is that the response is passed throught each middleware (pipes) before it is returned back.
So if $next($request) is called twice in one middleware it is normal that all pipes will be called again.
Found mine in the master. there was a script that was causing the page to reload in the background.
I built a little counter with admin access only and smoked it out. Came down to one script.
I am using two frameworks i.e. Laravel and Symfony for two applications which are interlinked with each other. Both are having bash script.
Now, I want to write 'php artisan down' command in Symfony's bash script so that if I merge code to Symfony then both the applications gets down.
How can I write Laravel artisan command in Symfony framework?
It should work like any other command:
#!/bin/bash
php /full/project/path/artisan down
Just write the full path.
You can do this in your Symfony project using Symfony Console:
//in your symfony project
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
public function processCmd() {
$process = new Process('php /absolute/path/to/project/artisan down');
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
}
Update:
Laravel also uses the same symfony's Console component!
So if you want to run something in Laravel, you can use the same code above.
Example:
//in your laravel project
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
public function processCmd() {
$process = new Process('supervisorctl stop all');
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
}
P.S:
You can pass an array if you want to run multiple commands:
$process = new Process(array('ls', '-lsa'));
i hope you can help me with my problem.
I have a Laravel application and i wanna optimize all my images.
I use a controller for that, but i have a lot of images, and i get execution_maxim_time_exceed.
I think, the best way to do that is to creat an artisan command.
So, i creat my with php artisan make:command Name command:example.
After, I move my code from controller to handle() from artisan command.
I can use Storage:move($oldPath, $newPath), but i can't use Image:make($filePath).
My file storage is "storage/app/images/image.png" and filePath is 'images/image.png'
When i use the controller, the method isFilePath() from Intervention\Image\AbstractDecoder return true, but in artisan command return false.
Method isFilePath() call the function is_file()
public function isFilePath()
{
if (is_string($this->data)) {
try {
return is_file($this->data);
} catch (\Exception $e) {
return false;
}
}
return false;
}
Why i get false from the function is_file() with the same path for file in artisan command, and in controller i get true? (for the same path).
Thanks
Ok, i found the solution.
In the controller is_file('images/image.png') return true, but in the cli, i need to use the full path
so, in CLI i use 'storage/app/images/image.png'.
I know create: makeDirectory(). Also, I know delete, copy, move (deleteDirectory, copyDirectory, moveDirectory). But, how can I verify if the folder exists with laravel. that´s my function
public function storeFile(Request $request)
{
$request->all();
$folders = $request->file01;
if($request->hasFile('file01'))
{
$request->file01->store('public/'.$folders);
return "Saved!";
}
}
You can use several methods.
With Laravel you can simply use File::isDirectory($dir). Laravel API
With PHP you can also just is is_dir, PHP Docs
you can use a php function: is_dir
is_dir($path)
currently I have the following set up, a route that is calling a function in my controller that is in turn queuing a job.
//My Route
Route::get('/testJob', 'Controller#testJob');
//My Controller
public function testJob()
{
$job = (new testJob())->delay(5);
$this->dispatch($job);
}
//My job
public function handle()
{
require 'testAPICall.php';
// echo $response;
return $response;
}
//testAPICall.php
$response = 'this is the response';
//Queue After
Queue::after(function (JobProcessed $event) {
echo var_dump($event->data);
});
What I would like to be able to do, is access the response returned by the job in Queue::after, or alternatively, pass a callback into the queue to be execute after the job, again with access to the response from the job.
Is this something that is possible with Laravel Queues, and if so how would I go about this?
Cheers, Jack.
Queue::after() is a global callback, that will run after each job. So this might not what you want.
In your case, I would depend on Events/Listeners to be triggered after finishing the job.
public function handle(Mailer $mailer)
{
//Your code
event(new JobDone($data));
}
Please let me know if you need more details for implementation.
I have done something like yours that log a message "queue.txt" in laravel 5 "app" folder
This code I've got from a youtube video and not my code , but I have tested it successfully
First thing you have to code in "Routes.php" as below
Route::get('/',function()
{
//$queue = Queue::push('LogMessage',array('message'=>'Time: '.time()));
$queue = Queue::later(20,'LogMessage',array('message'=>'Time: '.time()));
return $queue;
});
class LogMessage{
public function fire($job,$data){
File::append(app_path().'/queue.txt',$data['message'].PHP_EOL);
$job->delete();
}
}
Then you can run your project folder using "php -S localhost:8888 -t public"
at the same time you must open a another terminal window in windows or linux environment and pointed to same folder and issue the command "php artisan queue:listen"
I think this will be helpful for you!