Domen
Domen

Reputation: 284

"Undefined type 'App'" in api.php Laravel

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?

Upvotes: 1

Views: 1250

Answers (1)

Kishieel
Kishieel

Reputation: 2053

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 */
    }
}

Upvotes: 1

Related Questions