2

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/[email protected]&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?

1
  • Route::get("/activate/{parameter1}/{parameter2}", "Controller@getMethod");, then public function getMethod($parameter1, $parameter2){ ... }; laravel.com/docs/5.4/routing#route-parameters For more info. Keep in mind that Route parameters are not the same as GET parameters (ie those in the Query String)
    – Tim Lewis
    Mar 1, 2017 at 19:13

6 Answers 6

4

To pass the parameters as query parameters you can get them in the request object that's you can inject into your controller method:

Route

Route::get('/activate', 'YourController@controllerMethod');

Controller

public function controllerMethod(Request $request)
{
    $email = $request->input('email');
    $code = $request->input('code');
}

In addition you could also pass a 2nd parameter to input to use as a default value if either one of those aren't there.

2
  • Can I specify the parameters in the route instead retrieve these parameter from the Request object= Mar 1, 2017 at 18:23
  • You can't specify them in the route definition without injecting them into the controller. You can access them in the request object by $request->only('email', 'code'); giving you a 2 item array as ['email' => '[email protected]', 'code' => 'somecode'] if email or code isn't in the request it will just leave it out of the array or if non exist you'll get an empty array. Mar 2, 2017 at 5:22
2

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"; // [email protected]
    echo "Token: $token"; // eb0d89ba7a277621d7f1adf4c7803ebc
    // do stuff
}
1
  • This is really bad practice as it makes it impossible to cache the routes. Also, the injection of the parameters seems like a ton of code replication, i think this answer is misguiding the OP to perform bad practices. Please just use Route::get('/activate', ['as' => 'activate', 'uses' => '\App\Http\Controllers\ActivateController@activate']); Then use $email = request()->input('email'); & $token = request()->input('token'); in your controller, it will make your life a lot easier by just doing what laravel.com/docs says.... Also you have two answers in the same question
    – MrK
    Feb 23, 2019 at 22:38
1

If you do not want to use URL rewriting (Pretty URL) simply declare the route as:

Route::get('/activate','YourController@yourFunction');

and check for token and email in the controller as:

if(Input::has('email') && Input::has('token))
{
  //YOUR CODE
}
3
0

The route would be like this:

Route::get('account/email/validate', 'AccountsController@validateEmail')->name('account.validate.email');

The controller method would be like the following:

public function validateEmail()
{
    // request()->email
    // request()->token
}

To generate the URL with the query string you must do the following:

<a href="<?= route('account.validate.email', ['email' => $email, 'token' => $token]) ?>">

Or you can just hardcode it as well.

1
  • No I can't use URI like "/activate/{email}/{token}". I need to use the URL with the GET parameter using the ?param=value as specifified in my post Mar 1, 2017 at 18:21
-1

You will actually use ONE of the two features. You do not have to use both features at once. My suggestion is to use only the routes. Example:

<?php Router::connect(
'/activate/:email/:token',
['controller' => 'Accounts', 'action' => 'activate'],
['email' => '.*', 'token' => '.*']); ?>

Then you will use the following url: your-site.com/accounts/activate/[email protected]/your-token

The routes codes must be placed in /config/routes.php

More details in: https://book.cakephp.org/3.0/en/development/routing.html

3
  • I need to use the GET parameter declared by ?paramName=value Mar 1, 2017 at 18:28
  • That's the same. And still have the advantage of using friendly url ... Mar 13, 2017 at 18:49
  • You can expand this idea to: / controler / whatever / you / want This way the system already gives you the data in the controller ready to be used. Example: Router :: connect ('/ activate /: p1 /: p2 /: p3 /: p4' ...) The limit is your imagination, and code optimization is unmatched. Mar 13, 2017 at 18:53
-1

Route:

Route::get('/activate/{email}/{token}', [ 'uses' => 'ActivationController@activate', 'as' => 'activate' ]);

Controller:

ActivationController.php

public function activate($email, $token)
{
    // $email would be '[email protected]'
    // $token would be 'eb0d89ba7a277621d7f1adf4c7803ebc'
    // do stuff...
}

Or if you must use query params:

Route:

Route::get('/activate', [ 'uses' => 'ActivationController@activate', 'as' => 'activate' ]);

Controller:

public function activate(Request $request)
{
    if ( $request->has('email') && $request->has('token') )
    {
        $email = $request->email;
        $token = $request->token;
    }
}
1
  • No I can't use URI like "/activate/{email}/{token}". I need to use the URL with the GET parameter using the ?param=value as specifified in my post Mar 1, 2017 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.