Ad

Laravel 5: Post - Pass Arguments To Controller

- 1 answer

I think what I want is quite basic, I just can't find the proper syntax as I am still learning Laravel.

So, I am using google verification for sign ins on my website. This entails a post request to my backend that has to be handled, I have put this logic in a controller. My routes.php:

Route::post('google' ,  [
    'as' => 'verify.index',
    'uses' => '[email protected]'
]);

My controller (verify.php):

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;

class verify extends Controller
{
    public function verifyIdToken($token)
    {
        $token = $_POST['id'];
        $email = $_POST['email'];
        return $this->getAuth()->verifyIdToken($token);
        echo $this->getAuth()->verifyIdToken($token);
        return view('aviewII')->with(['verify' => json_encode(verifyIdToken($token)),'email'=> json_encode($email)]);
    }
}

Of course, because of how the function in the controller is written, I get the following error Missing argument 1 for App\Http\Controllers\verify::verifyIdToken() My question is, how do I tell the function in the controller to take $_POST['id'] as the argument for $token?

Something like this:

Route::post('google' ,  [
        'as' => 'verify.index',
        'uses' => '[email protected] ~with $_POST['id'] as $token'
    ]);

For additional reference, my actual post request looks like this:

$.post( "http://example.com/google", {email:profile.getEmail(),id:id_token} );

Ad

Answer

You are looking for Laravel's request class. You should type-hint the class on your method, which then allows loads of options to actually obtain the data. Something like:

use Illuminate\Http\Request;

public function verifyIdToken(Request $request)
{
    $token = $request->input('id');
    $email = $request->input('email');
    return $this->getAuth()->verifyIdToken($token);
    echo $this->getAuth()->verifyIdToken($token);
    return view('aviewII')->with(['verify' => json_encode(verifyIdToken($token)),'email'=> json_encode($email)]);
}

The documentation on it has tons more useful information.

Ad
source: stackoverflow.com
Ad