1

I'm using Laravel 5.5 and have a question about routing.

My route is like this

Route::get('/folder/{param}', 'PageController@getFolderTree');

And I want to get all parameters after /folder/:

http://example.com/folder/com/example/app -> I get /example/app

How can it possible ?

1
  • function getFolderTree($params){//here you can access params} Commented Mar 19, 2018 at 4:44

3 Answers 3

2

If you know in advance what the maximum number of parameters will be you can do one of the next 2.

a) If all are neccessery:

Route::get('/folder/{a}/{b}/{c}', 'PageController@getFolderTree');

b) If not all are neccessery:

Route::get('/folder/{a?}/{b?}/{c?}', 'PageController@getFolderTree');

And retrieve them like this:

public function getFolderTree($a, $b, $c) {...}

And if you don't know the maximum you will need to do a regex and explode. This would be the route:

Route::get('/folder/{any}', 'PageController@getFolderTree')
    ->where('any', '.*'); // Indicates that {any} can be anything

And do the explode in the controller:

public function getFolderTree($any) {
{
    $params = explode('/', $any); // $params will be an array of params
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can explode the URL. For example:

$url = 'http://example.com/folder/com/example/app';
$explodedText = explode('/folder/', $url);
$textBeforeFolder = $explodedText[0];
$textAfterFolder = $explodedText[1]; // This is the one you want

Comments

0
request->url() //will return the whole url

and

request()->uri() //will return the URI

Now if you want to access the full URL u can access

request()-fullUrl() //will return the full url

Now you can use str_replace()

 str_replace(request()->url.'folder/com/','', request->fullUrl());

 //it will return example/app

Comments

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.