Search code examples
phplaravellaravel-5laravel-routing

How can I declare a route taking these two parameters in Laravel?


I am pretty new in Laravel and I have the following problem.

I have to declare a route that handle requests like this:

http://laravel.dev/activate?email=myemail@gmail.com&token=eb0d89ba7a277621d7f1adf4c7803ebc

So basically it have to handle a GET request toward the /activate resource with two get parameters email and token.

How can I correctly declare this route? Then I only have to create the related controller method that takes these two parameter?


Solution

  • In routes.php (Laravel < 5.3) or web.php (Laravel 5.4+):

    Route::get('/activate', [ 'as' => 'activate', function()
    {
        return app()->make(App\Http\Controllers\ActivateController::class)->callAction('activate', $parameters = [ 'email' => request()->email, 'token' => request()->token ]);
    }]);
    

    So we are instantiating the ActivateController class and calling the method 'activate' which is the first argument, then supplying a list of parameters the method receives in the form of an array.

    public function activate($email, $token)
    {
        echo "Email: $email"; // myemail@gmail.com
        echo "Token: $token"; // eb0d89ba7a277621d7f1adf4c7803ebc
        // do stuff
    }