I work with Laravel Task Scheduling, but I have a problem when I call some method from my controller.
protected function schedule(Schedule $schedule)
{
$schedule->call('UserController#deleteInactiveUsers')->everyMinute();
//$schedule->call('App\Http\Controllers\UserController#deleteInactiveUsers')->everyMinute();
}
When I call with uncommented line i get this error:
[ReflectionException]
Class RecurrenceInvoiceController does not exist
and then I insert fully qualified namespace path and then I get this error:
[PDOException] SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
And
[ErrorException] PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known
Where is the problem? Which way is correct to call method from Controller from Laravel Task Scheduling.
I stumbled months ago with the same problem, until I could fix it.
I use laravel 5.2 and the kernel call my drivers this way:
$schedule->call('App\Http\Controllers\MyController#MyAction')->everyMinute();
I hope this will help someone else ;)
Directly put this in schedule function in kernel.php.
$schedule->call('App\Http\Controllers\YourController#function')->everyMinute();
This will run after every minute.
Note:
In localhost, you have to run it manually but on the server, you should try this command in your cronjob this will automatically call your controller function after every minute
php -d register_argc_argv=On /home/iflasity/public_html/foldername /artisan schedule:run > /dev/null 2>&1
cd /home/iflasity/public_html/foldername && php artisan schedule:run /dev/null 2>&1
For me, the first error looks like you need to run composer update, then composer dump-autoload.
If it works you will also get the second error, the 2002 error meaning is:
Can't connect to local MySQL server through socket" (see (Client Error Codes and Messages in MySQL docs).
You need to set your database configuration in your .env file
OK. I solved this problem. The problem was with Docker Cron container. Cron container was not linked with MySQL container.
Thanks for all answers.
Also now I test with more simple function to insert new currency in database. Here is code:
public function handle()
{
$currency = new Currency();
$currency->currency_name = 'EUR';
$currency->save();
$this->info('New currency is successfully generated!');
}
This function is from laravel/app/Console/Commands. I call from Kernel > schedule(). Not work.
When I insert simple log write to handle() function like:
File::put(base_path() . '/storage/logs/test_logs.txt', 'Test log ' . Carbon::now()->format('Y-m-d H:i:s') . PHP_EOL);
this works. But when I try to create new object from Models and save into db -> this not work.
Related
On the command line I can run:
aws s3 sync s3://my_bucket . --dryrun
just fine.
However, I'm trying to run it as a command in Laravel:
public function handle()
{
$command = 'aws s3 sync s3://my_bucket . --dryrun';
shell_exec($command);
}
and am getting:
fatal error: An error occurred (AccessDenied) when calling the
ListObjectsV2 operation: Access Denied
Why does the same command work on the command line but not within my PHP application?
I followed the advice of #Sammitch, and used
--profile my_config
where my_config was a named set of configuration parameters created using:
aws configure --profile my_config
Source: AWS creating a named profile
I've created a custom command functionality for my project, but there is something strange happening at the moment.
First i thought it might had something to do with the composer dump-autoload but after adding the exec command it still didn't work.
I do have the
seeds as autoload in my composer.json
$this->className = "MyDbSeeder"
$this->call('make:seeder', ['name' => $this->className]);
exec("composer dump-autoload");
sleep(5);
$this->call('db:seed', ['--class' => $this->className]);
/* Response */
// -- ReflectionException : Class MyDbSeeder does not exist --
/* But when i'm running the exactly same command but not through the `$this->call()` function it works perfectly fine. */
exec("php artisan db:seed --class {$this->className}", $output);
/* Response */
// -- "Database seeding completed successfully." --
Of course it does work with the exec command, but i don't feel like this is the solution that i need to deliver.
Anyone knows why the regular laravel command ain't working?
While setting up Rabbitmq in laravel, it returns this message when I try to queue a work.
It displays the error :
php artisan queue:work --tries=1
In QueueManager.php line 172:
No connector for [rabbitmq]
Instead of using the VYuldashev driver, I used https://github.com/AaronJan/Laravel-Rabbit - this driver.
and added the src/LaravelRabbit files in my vendor folder.
Add
CLOCKWORK_ENABLE=false
in your .env file.
After upgrading php from 7.0.14 to 7.0.26 php artisan serve throws this error
Warning: Unknown: failed to open stream: No such file or directory in
Unknown on line 0
Fatal error: Unknown: Failed opening required '/Applications/XAMPP/xamppfiles/htdocs/school-dashboard/public/server.php'
(include_path='.:') in Unknown on line 0
Ok, after hours of pulling my hair out I finally found out what the issue was.
In laravel 4 php artisan serve does this under the hood
<?php
namespace Illuminate\Foundation\Console;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
class ServeCommand extends Command {
public function fire()
{
$this->checkPhpVersion();
chdir($this->laravel['path.base']);
$host = $this->input->getOption('host');
$port = $this->input->getOption('port');
$public = $this->laravel['path.public'];
$this->info("Laravel development server started on http://{$host}:{$port}");
passthru('"'.PHP_BINARY.'"'." -S {$host}:{$port} -t \"{$public}\" server.php");
}
}
That is essentially this in plain php:
php -S 127.0.0.1:8000 -t public serve.php - see the docs for php built in server for more info.
And this worked well and dandy before php 7.0.26, where the last parameter for the php -S built in server was changed to a flag as well, so you have to call it like this php -S 127.0.0.1:8000 -t public -f serve.php.
If you want to serve it with php artisan serve you will have to override the ServeCommand and change the last line of the fire() method to this:
passthru('"'.PHP_BINARY.'"'." -S {$host}:{$port} -t \"{$public}\" -f server.php");
Or you can change it directly in the ServeCommand, but if you do a composer update or install you will have to do it again.
That's what it happened to me today running a Laravel project. After i have tried all the possible solutions finally i got one that it's work for me . So first of all check that your anti-virus block your server.php and it also deleting it . Then check if you server.php is missing from your project, and I think that is probably yes . Just copy it (server.php) from other project (build also from laravel) but before that just turn off your anti-virus until your next restart and make you sure that you will stop it every time before running. I hope that it helps you.
I've found this page :
https://gist.github.com/rtconner/40b415073378da28b2bf
where it's shown a Service provider to be added in L5 in order to manage sftp connection driver in filesystem configs. Which is supposed to solve a issue.
Anyway once I executed via console:
php artisan make:provider SftpServiceProvider
And edited the SftpServiceProvider class as shown in the above webpage.
I went to config/app.php to add
'providers' => [
...
'App\Providers\SftpServiceProvider',
...
]
I tried a
composer dump-autoload --optimize (with and without the option)
But always I get a error:
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'App\Providers\SftpServiceProvider' not found
In fact, I cannot see in my vendor/composer/autoload_classmap.php
the line addressing to SftpServiceProvider
Also executing a
composer update #will not be ok.
Again the errors:
- Script php artisan clear-compiled handling the post-update-cmd event returned with an error
- [RuntimeException] Error Output: PHP Fatal error: Class 'App\Providers\SftpServiceProvider' not found
What do I miss in the ServiceProvider creation procedure?
Thanks