How to define a Laravel route with a parameter that contains a slash character

PhpLaravel

Php Problem Overview


I want to define a route with a parameter that will contain a slash / character like so example.com/view/abc/02 where abc/02 is the parameter.

How can I prevent Laravel from reading the slash as a separator for the next route parameter? Because of that I'm getting a 404 not found error now.

Php Solutions


Solution 1 - Php

Add the below catch-all route to the bottom of your routes.php and remember to run composer dump-autoload afterwards. Notice the use of "->where" that specifies the possible content of params, enabling you to use a param containing a slash.

//routes.php
Route::get('view/{slashData?}', 'ExampleController@getData')
    ->where('slashData', '(.*)');

And than in your controller you just handle the data as you'd normally do (like it didnt contain the slash).

//controller 
class ExampleController extends BaseController {

    public function getData($slashData = null)
    {
        if($slashData) 
        {
            //do stuff 
        }
    }

}

This should work for you.

Additionally, here you have detailed Laravel docs on route parameters: [ docs ]

Solution 2 - Php

I have a similar issue but my URL contains several route parameters :

/test/{param1WithSlash}/{param2}/{param3}

And here is how I managed that case :

    Route::get('test/{param1WithSlash}/{param2}/{param3}', function ($param1MayContainsSlash, $param2, $param3) {

        $content = "PATH: " . Request::path() . "</br>";
        $content .= "PARAM1: $param1WithSlash </br>";
        $content .= "PARAM2: $param2 </br>".PHP_EOL;
        $content .= "PARAM3: $param3 </br>".PHP_EOL;

        return Response::make($content);
    })->where('param1MayContainsSlash', '(.*(?:%2F:)?.*)');

Hope it can help.

Solution 3 - Php

urlencoded slashes do not work in Laravel due to what I consider a bug. https://github.com/laravel/framework/pull/4323 This pull request will resolve that bug.

Update.

Note that the change allows the route to be parsed BEFORE decoding the values in the path.

Solution 4 - Php

I've already upvoted Pierre's answer, it's correct, but in my opinion is longer than needed, here's a very short for-the-impatient sample route that does the trick:

Route::get('post/{slug}', [PostController::class, 'show'])->where('slug', '[\w\s\-_\/]+');

This is all you need. Indeed, the \/ in the regular expression above(in the where method) is all you need !

Now for example:

  • domain/A ---> "A" will be passed to the show method of the PostController.
  • domain/A/B ---> "A/B" will be passed to the show method of the PostController.
  • domain/A/B/C ---> "A/B/C" will be passed to the show method of the PostController.
  • and so on...

For more samples read this: Laravel Documentation - Regular Expression Constraints

Solution 5 - Php

In Laravel 8+

Route::get('/search/{search}', function ($search) {
    return $search;
})->where('search', '.*');

Reference:

https://laravel.com/docs/routing#parameters-encoded-forward-slashes

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhserusvView Question on Stackoverflow
Solution 1 - PhpGadomaView Answer on Stackoverflow
Solution 2 - PhpPierreView Answer on Stackoverflow
Solution 3 - PhpArtistanView Answer on Stackoverflow
Solution 4 - PhpaderchoxView Answer on Stackoverflow
Solution 5 - PhpusernotnullView Answer on Stackoverflow