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.
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.
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.
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.
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 ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With