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.
Advertisement
Answer
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 ]