Laravel - How to start artisan automatically only once? - laravel

If I have some artisan command and want to fire an event only once, is it possible run only if HTTP started first time?
Note: There are while-loop so it should be maybe good run as shell sh script (not via browser), like exec() command without return data.
I found solution using posix_getpgrp() and compare with cache::forever var. If is not same than call event and remove forever cache. Forever cache remember even if restart apache service.

You can call artisan commands from your app using
Artisan::call('your-command',array());
For example to create schema I would have route /create-schema then in the method I would have something like this
public function createSchema()
{
Artisan::call('migrate:install');
Artisan::call('migrate');
}

Find a solution. Use event listener and check is stored data into session then if not set, call script once, store cache data that is called. If you restart apache or want to force call script change your cache data.
What you need is to combine:
Cache
Events

Related

Laravel: AppServiceProvider to register an API Key?

I'm pretty new to Larvel, so apologies in advance for what I assume is a trivial question.
I'm building a store page using Stripe as my payment processor, and their PHP library requires users to register an API key.
Up to this point, I have been setting my API key in the first line of my Controller that creates the checkout session. This seems unnatural to me, and I thought that there would be a better way to "globally" set this API key upon the application boot up.
I then came across the AppServiceProvider, which I understand can be used to perform tasks on startup. I am now setting my API key in it's register() function, like so:
public function register() {
\Stripe\Stripe::setApiKey(env('STRIPE_SECRET_KEY'));
}
This works but I am wondering if this is normal and best-practice. The Laravel documentation mentions that this is a good place for registering event listeners or even routes, but doesn't mention anything similar to API keys or setting up the registration for third-party libraries.
Thanks in advance!
One suggestion is dont call env anywhere in your code other than config file.Better create config file in config folder.
For example create a file called stripe.php in config folder
<?php
return [
'stripe_secret_key'=>env('STRIPE_SECRET_KEY')
];
Then you can access
\Stripe\Stripe::setApiKey(config('stripe.stripe_secret_key'));
This helps when you cache config.If you run php artisan config:cache then it wont call env
As per documentation
If you execute the config:cache command during your deployment
process, you should be sure that you are only calling the env function
from within your configuration files. Once the configuration has been
cached, the .env file will not be loaded and all calls to the env
function will return null.
Ref: https://laravel.com/docs/8.x/helpers#method-env
difference between register and boot method
“After all providers have been registered, they are “booted”. This
will fire the boot method on each provider. A common mistake when
using service providers is attempting to use the services provided by
another provider in the register method. Since, within the register
method, we have no gurantee all other providers have been loaded, the
service you are trying to use may not be available yet. So, service
provider code that uses other services should always live in the boot
method. The register method should only be used for, you guessed it,
registering services with the container. Within the boot method, you
may do whatever you like: register event listeners, include a routes
file, register filters, or anything else you can imagine.”
So the register one is just for binding. The boot one is to actually
trigger something to happen.
Ref: https://laracasts.com/discuss/channels/general-discussion/difference-between-boot-and-register-method

Laravel: what does schedule:finish do?

I discovered for an incident that Laravel 6 schedule:run has a brother, called schedule:finish
But using artisan list it's not documented.
What does this console command do?
This hidden command added on Laravel 5.4 to handle the after callbacks of a given command.
Check Taylor Otwell's explanations on this PR:
This PR adds improvements to the scheduler. Previously, when
->runInBackground() was used "after" hooks were not run, meaning
output was not e-mailed to the developer (when using emailOutputTo.)
This change provides a new, hidden schedule:finish command that is
used to fire the after callbacks for a given command by its mutex
name. This command will not show in the Artisan console command list
since it uses the hidden command feature which is new in Symfony 3.2.
schedule:finish is used to setup actions for a process after its finishes execution,
if you have two or three level of processes that you want them to be executed one after another and depending on each other you use the schedule:finish command

Laravel - Auxiliary method where to put it?

I'm developing a logging method, and I'd like to run this log every time an x-screen is opened.
In the log I will check if there is already a log on that day, if there is no saved and if there is only increment ...
But this function I will use in several controllers, what is the best place for me to put this methodo and make it accessible in all controllers?
Note: Using laravel 5.4

How do I monitor file system access in Laravel?

I'm trying to find a way to monitor basic information regarding the access of files on my applications filesystem. The files are stored using the public driver, and are available publicly through the appropriate symlink. I want to add a record to a databased whenever a file is accessed. Is there a way to accomplish this without creating a separate controller? Maybe a filesystem middleware type mechanism?
First, static files don't get handled by your PHP application (in this case Laravel), those are handled by your web server (Nginx, Apache, etc).
The usual configuration will only route the request to PHP if is a PHP request and will just serve the file if is a static file.
You could tell the web server to route all static files through a specific URL of your application and then in your controller serve that file and save the statistics, but I advice you against this, because static files are very efficient served by the web server and you would add a lot of work that affects the performance if you process them through a PHP application.
A solution
You could create a Laravel console command that gets call with each new line of the log file and use maybe SupervisorD to make sure the command is always called.
So, you could have something like this to call your command:
tail -F -n 1 /path/to/access.log | /usr/bin/php /path/to/application/artisan register:log
Where -F will handle the log rotation, -n 1 will use the first last line (or maybe you could try with -n 0) to make sure you don't consider old lines if for some reason the process gets restarted and register:log can be signature of your console command in Laravel.
That way, each new line in the access log will call your command with the log line as input.

Executing a Route (Controller/Action) using PHP CLI and Detecting a CLI Request

Is there a way in Laravel 4 to run my controller/action using PHP-CLI? I have a controller/action that I would like to extend to perform an alternative action if the request comes from the CLI, so is there a way to identify the request as a CLI request?
The Laravel documentation on this site seems to suggest that there is a method Request::cli() for determining if the current request is via the Artisan CLI but when I used the method in Laravel 4, it throws an error:
Call to undefined method Illuminate\Http\Request::cli()
Basically, I have just moved from CakePHP to Laravel and would like to accomplish something similar to as what's described in this article (for CakePHP) : Calling controller actions from cron and the command line
I understand that I can work with Laravel 4 Artisan Commands, but is the approach I would like to use possible? And if so, how?
As Rob already said, to determine if the current script is being run in the console use App::runningInConsole() or simple plain PHP php_sapi_name() == 'cli'.
As for running controller#action from console, you could use curl or wget to request one of your routes but I think the proper way of doing it would be to use a custom artisan command. Your controllers are classes so you can instantiate them and use as you please from within your artisan command:
$controller = new SomeController;
$controller->someAction();
Watch this video for an introduction to easily developing your own artisan commands.

Resources