mardi 11 septembre 2018

Accessing an Outer Variable in a laravel function

I tried using global $variable in the function. That did not work out. DISCLAIMER: This is not a duplicate question. This question is in the Laravel Context.


routes/api.php:

Route::get('/getUser/{userId}', 'AuthController@getUser');


AuthController@getUser:

public function getUser(Request $request, $userId){
    return MiniUtils::authenticateAPI($request, function(){
        global $userId; 
        return response()->json(['id' => $userId], 200);
    });
}

It returns this JSON RESPONSE: { id: null } at the route api/getUser/2


BUT...

When I run this at the same route:

public function getUser(Request $request, $userId){
     return response()->json(['id' => $userId], 200);
}

I get this response: { id: 2 }


I want this variable to be accessed from a function. How can I achieve that?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2xaO9qv
via IFTTT

Aucun commentaire:

Enregistrer un commentaire