0

On laravel site making Http Post request inside of job handle I need to pass locale parameter. And this locale must be used in controller.

I use astrotomic/laravel-translatable and defined in config/translatable.php with default 'en' :

'locales' => [
    'en',
    'fr',
...



'locale' => null,
...

My BannersCRUDTest job

<?php

namespace App\Jobs;

class BannersCRUDTest implements ShouldQueue
{
    public function handle()
    {
        $currentLocale = 'fr'; // I path French

        App::setLocale($currentLocale);
        session()->put('locale', $currentLocale); // That does not work...

        $response = Http::post(route('banners.filter' ));

But checking in related Controller with getLocale method I see English

class BannerController extends Controller
{

    public function filter(): array
    {
        \Log::info(' -1 BannerController app()->getLocale()::'); // I see 'en' value
        \Log::info(app()->getLocale();
        ...

How have I to pass locale from Job to Controller ? I prefer to use App::setLocale, not as parameter in url of request...

"laravel/framework": "^9.19",
"guzzlehttp/guzzle": "^7.2",
"astrotomic/laravel-translatable": "^11.11",
"mxl/laravel-job": "^1.3",

Thanks in advance!

Win
  • 353
  • 1
  • 8
Petro Gromovo
  • 1,755
  • 5
  • 33
  • 91
  • If you only need it in filter, why not pass it as a route param in the post request? `Http::post(route('banners.filter', ['locale' => $currentLocale]));` and then in the controller: `public function filter(string $locale): array {}` – geertjanknapen Dec 27 '22 at 11:51
  • I search a decision with use App::setLocale, not as parameter in url of request... – Petro Gromovo Dec 30 '22 at 11:11

1 Answers1

0

You use a job (BannersCRUDTest) to set locale from 'en' to 'fr'

This job of yours is set to be queued.

  • Make sure you have call / dispatch this job properly.
  • And also make sure the queue process is properly run.
Win
  • 353
  • 1
  • 8
  • Could you please to detalize what do you mean "properly run" ? – Petro Gromovo Dec 27 '22 at 21:24
  • 1
    have you run queue:work either using supervisor or cron job and check your database table jobs, is your queue still there? – Win Dec 28 '22 at 08:25
  • I run command : php artisan job:dispatchNow BannersCRUDTest so it must be run immediately Actually I need to write some tests for my API and I used jobs as container to run requests with Http::post. Maybe jobs are not good place for it? I know there are also actions, console commands but not sure. I prefer to have locale already set in my controller( with use of App::setLocale in job) , not as parameter in url of request... about difference... – Petro Gromovo Dec 28 '22 at 08:40
  • 1
    @PetroGromovo if you need to run it immediately, either remove extends ShouldQueue, or indeed like what you said, use Action class (best for a single purpose class). Where did this job getting dispatch? Called within a POST controller ? Or another solution is by using middleware, checking default locale selected by a user. – Win Dec 28 '22 at 08:48
  • As Action class is best for a single purpose class - how can I run it from console? I choose jobs as they can be run from console, but looks like there are no any command like "php artisan make:action ActionName". What xan be used here that can be run from console ? – Petro Gromovo Dec 28 '22 at 15:52
  • 1
    you can use php artisan make:command, and call this action class from that particular command and as for create Action class, you need to create it manually – Win Dec 29 '22 at 05:20
  • 1
    @PetroGromovo here is a link to help you start create an artisan command https://www.cloudways.com/blog/custom-artisan-commands-laravel/#custom – Win Dec 29 '22 at 05:23