3

I have a route that looks like this:

Route::namespace('Api\HelpGuides')->prefix('help')->group(function () {
    Route::get('/{userInput}', 'FetchArticlesController')
    ->where('userInput', '.*');
});

And the controller that looks like this:

    public function __invoke(Request $request, string $userInput) ...

I want to be able to capture the $userInput so if I receive a request from the front-end that is like:

/api/help/anything/could/be/in/here?including=params

That I can consume in the FetchArticlesController.

When I dump out $userInput in my controller, I am given everything up to the query string, so in the above example, it dumps out

/anything/could/be/in/here

Is there a way I can make it dump out everything after my defined route? e.g. ANYTHING after /api/help/ ? I would ideally like that $userInput string to look like:

/anything/could/be/in/here?including=params
James Stewart
  • 869
  • 12
  • 33
  • Does this answer your question? [Laravel - Using (:any?) wildcard for ALL routes?](https://stackoverflow.com/questions/13297278/laravel-using-any-wildcard-for-all-routes) – ManojKiran A Sep 16 '21 at 15:57

3 Answers3

1

If you want to capture anything:

Route::pattern('anything', '.*');

Route::namespace('Api\HelpGuides')->prefix('help')->group(function () {
    Route::get('/{anything}', 'FetchArticlesController');
});

The $anything argument in your controller method will be... anything, including /.

Anthony Aslangul
  • 3,589
  • 2
  • 20
  • 30
1

You can get query string with $request->getQueryString().

So you can do

Route::namespace('Api\HelpGuides')->prefix('help')->group(function () {
    Route::get('/{userInput}', 'FetchArticlesController')->where('userInput', '.*');
});
public function __invoke(Request $request, string $userInput)
{
    $userInput = trim("{$userInput}?{$request->getQueryString()}", '?');
}

I trim question mark if it there is no query string on the end.

MichalOravec
  • 1,560
  • 3
  • 5
  • 20
1

try $request->fullUrl() or $request->getRequestUri()

JimloveTM
  • 33
  • 5