Summer Developer
Summer Developer

Reputation: 2086

Laravel 5: Post - Pass Arguments To Controller

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' => 'verify@verifyIdToken'
]);

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' => 'verify@verifyIdToken ~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} );

Upvotes: 1

Views: 3946

Answers (2)

camelCase
camelCase

Reputation: 5598

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.

Upvotes: 1

Rajender Joshi
Rajender Joshi

Reputation: 4205

Controller method:

public function verifyIdToken(Request $request)
{
    // Not necessary but a better practice for ajax 'POST' responses.

    if($request->ajax() &&  $request->isMethod('post'))
    {
        return $request::get('token');
    }
}

Route:

Route::post('google', ['as' => 'some.alias', 'uses' => 'SomeController@verifyIdToken']);

Upvotes: 2

Related Questions