Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

laravel

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.

like image 924
hserusv Avatar asked Feb 04 '14 12:02

hserusv


People also ask

What is prefix in Laravel route?

Path prefixes are used when we want to provide a common URL structure. We can specify the prefix for all the routes defined within the group by using the prefix array option in the route group.

What is route parameter in Laravel?

Laravel routes are located in the app/Http/routes. php file. A route usually has the URL path, and a handler function callback, which is usually a function written in a certain controller.

What is the difference between URL and route in Laravel?

For a start it is using a named route. This means that the link between the form and its controller are not dictated by the url that the user sees. The next advantage is that you can pass additional parameters into the route helper and it will put those in the correct place in the form action.


1 Answers

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 ]

like image 141
Gadoma Avatar answered Sep 18 '22 08:09

Gadoma