0

In Lumen I have to get the url first segment as a $source property and all other parts as a $path. It is solved by this:

Route::get('/{source}/{path:.*}', 'HomeController@index');

By this the /database/path/to/folder URL will be parsed as the $source = 'database'; and the $path = 'path/to/folder'; It's great!

But what when I would have optional query parameters too, for example: /database/path/to/folder?attr1=foo&attr2=bar

How can I define to get the whole query part in a property as a route parameter?

1 Answer 1

2

Since they are optional and it is a query string, you do not have to define them in your route registration.

You can simply pass them when you generate the url. For instance:

Route::get('/{source}/{path:.*}', [
    'as' => 'home',
    'uses' => 'HomeController@index'
]);

Then you could do:

route('home', [
    'source' => 'database',
    'path' => 'path/to/folder',
    'attr1' => 'foo',
    'attr2' => 'bar'
]);

And the output would be:

http://example.com/database/path/to/folder?attr1=foo&attr2=bar

Update

Since you want to put all into a route parameter, you could achieve the same like this:

Route::get('{source}/{path:[a-z0-9/]+}[/{query:[a-z0-9=&]+}]', [
    'as' => 'home',
    'uses' => 'HomeController@index'
]);

Then you would generate the route like this:

route('home', [
    'source' => 'database',
    'path' => 'path/to/folder',
    'query' => 'attr1=foo&attr2=bar'
]);

And the output would be:

http://example.com/database/path/to/folder/attr1=foo&attr2=bar`
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks @chin, it would be great, but in my case all the parameters are fully could be anything. I have to find out them dynamically. They name and they number too is could be anything.
What do you mean @szatti1489? Where's the problem?
The problem is that I don't know nor the names neither its number exactly. This is why I would get the whole query part into a variable to able to be parsed in php.
@szatti1489 Please have a look at my updated answer :)
Thanx a lot, It looks much better! My last problem is that I cannot use the where() method in route since I use Lumen actually. Do you have an ide how could the optional query part be separated from the optional path part? It could be something like this but of course it doesn't work due to the ? mark: Route::get('/{source}/{path:.*}?{query:.*}', 'HomeController@index');
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.