I am a bit stuck with loading in a file for Laravel Queue/Job
I am using Laravel queued/async jobs (lets call it job for easy use)
Oke lets start from the beginning, we have our own translation function and we also named it __()
just like the default from Laravel, don't ask me why etc. (easy solution is to just rename it, I know) but this is what I have to stick with (unless this is unfixable maybe).
So to declare the function before Laravel does we insert the function just before the autoload in the index.php
like this.
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/
require __DIR__.'/../app/Helpers/localization.php'; <-- yes this file
require __DIR__.'/../vendor/autoload.php';
And this works fine for the website and if we execute jobs with SomeJob::dispatchNow()
(not async)
But when we want to do a async job like SomeJob::dispatch()
the index.php
is not called so the file is never required so neither is the function. (or am I wrong?)
I tried it with composer.js
autoload
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
],
"files": [
"app/Helpers/helpers.php", <-- works fine
"app/Helpers/Localization.php" <-- it does include tho
]
},
now in the file we use the if (! function_exists('__'))
but at that point the function is already declared, on the website itself it doesn't work either.
So short saying, require at the index.php only works from the website directly and not from async jobs cause index.php is never called when a queue execute the job.
Using the composer autoload doesn't work for both the website or the job cause the function is already declared by Laravel before we declare it.
So where should I require the file/declare the function so that both the website directly and the async job can use our version of the function.
P.S. I know my english isn't that great, so if anything is unclear or even if I miss any information, please ask me and I try to edit the post to be more clear.
You can add your function definition to the start of the bootstrap file of laravel bootstrap/app.php
Or (since i suspect SomeJob::dispatch()
to use artisan), you can require your file in index.php
and in artisan
#!/usr/bin/env php
<?php
define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/
require __DIR__.'/app/Helpers/localization.php';
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';