1

I'm using Laravel 8.53 and Vuejs. I want to set language for specific controller based on api parameter. (to send password reset email in desired language) I have this route in api.php which works:

Route::post('forgot-password', [NewPasswordController::class, 'forgotPassword'])->name('password.reset');

Now I wanted to create something like this:

Route::post('forgot-password/{locale}', function ($locale) {App::setLocale($locale);}, [NewPasswordController::class, 'forgotPassword'])->name('password.reset');

Now when I send to /api/forgot-password/en I get 200 OK but no response. My VS Code is showing me this error: Undefined type 'App'. Do I need to define "App" in api.php? How?

2
  • 1
    try this: app()->setLocale($locale) Commented Aug 6, 2021 at 9:37
  • Give a \ as \App::setLocale($locale) Commented Aug 6, 2021 at 9:38

1 Answer 1

1

I am not sure if it's correlated but maybe the problem is in different place. You passing three parameters to post method. According to laravel docs you should pass only two. In this case you pass callback, or you pass controller path with method. Try to move App::setLocale() to your controller method and use your first syntax. Remember to import App facade before using it.

// api.php
Route::post('forgot-password', [NewPasswordController::class, 'forgotPassword'])->name('password.reset');

// NewPasswordController.php
use \App;

class NewPasswordController {
    public function forgotPassword( $locale ) {
         App::setLocale( $locale );

         /* rest of code */
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.