Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: capture everything after '/' in a route

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
like image 769
James Stewart Avatar asked Jan 31 '26 07:01

James Stewart


1 Answers

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 /.

like image 112
Anthony Aslangul Avatar answered Feb 02 '26 21:02

Anthony Aslangul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!