Ad

Pass Parameter To Controller From Route

- 1 answer

In my routes file, I have;

Route::get('/{token}/student', [
    'uses' => '[email protected]',
    'as' => 'student',
]);


Route::get('/{token}/city', [
    'uses' => '[email protected]',
    'as' => 'city',
]);

So the route is either "student" or "city". How do I determine which one in my controller method? Should I even be structuring my routes like this? Should I just point them to two different methods?

I can easily pass in {token} for example with just;

public function resumeSurvey($token)
{
Ad

Answer

Inside a controller you can get a current route name by getting a route object Illuminate\Routing\Route, at first place, then calling its method getName.

The next two ways are the same.

public function resumeSurvey($token)
{
    $routeName = Route::getCurrentRoute()->getName();

    $routeName = $this->getRouter()-> getCurrentRoute()->getName());
}
Ad
source: stackoverflow.com
Ad