5

First off, apologies if this is a bad question/practice. I'm very new to Laravel, so I'm still getting to grips with it.

I'm attempting to pass a variable that contains forward slashes (/) and backwards slashes () in a Laravel 5 route and I'm having some difficulties.

I'm using the following: api.dev/api/v1/service/DfDte\/uM5fy582WtmkFLJg==.

Attempt 1:

My first attempt used the following code and naturally resulted in a 404.

Route:

Route::group(array('prefix' => 'api/v1'), function() {
    Route::resource('service', 'ServiceController');
});

Controller:

public function show($service) {
    return $service;
}

Result:

404

Attempt 2:

I did a bit of searching on StackOverflow and ended up using the following code, which almost works, however, it appears to be converting \ to /.

Route:

Route::group(array('prefix' => 'api/v1'), function() {
    Route::get('service/{slashData}', 'ServiceController@getData')
    ->where('slashData', '(.*)');
});

Controller:

public function getData($slashData = null) {
    if($slashData) {
        return $slashData;
    }
}

Result:

DfDte//uM5fy582WtmkFLJg==

As you can see, it's passing the var but appears to be converting the \ to /.

I'm attempting to create an API and unfortunately the variable I'm passing is out of my control (e.g. I can't simply not use \ or /).

Does anyone have any advice or could point me in the right direction?

Thanks.

tombraider
  • 1,127
  • 1
  • 11
  • 19
  • 4
    If `api.dev/api/v1/service/DfDte\/uM5fy582WtmkFLJg==` is meant to be a request URL, then note that it is obviously invalid. A back slash is _not_ a valid character in a URL, you'd have to "percent encode" it. Or, if in reality you want to escape the following forward slash, then likewise: don't use a backslash, but URL encode the slash instead. Just as the formal definition for URLs request. – arkascha Mar 31 '16 at 12:37
  • Unfortunately the \ is actually part of ID that I'm trying to pass as a variable. I've tried encoding the variable, e.g. `DfDte%5C%2FuM5fy582WtmkFLJg%3D%3D` but that just leads to another 404 using the code provided in "Attempt 2". – tombraider Mar 31 '16 at 13:03
  • 1
    Certainly I do not know your data model, but an ID containing the literal sequence `\/` sounds extremely unlikely to me. Are you really sure this is not just a notation meant to escape the contained forward slash? – arkascha Mar 31 '16 at 13:06
  • That's a good point, I haven't thought about it that way before. I've just done a bit of testing and it looks like you're right. For some reason the response is returning `\/` for some reason, so going to have to try and filter that first `\` out. Thank you! – tombraider Mar 31 '16 at 13:14
  • 1
    What "response"? If you mean you receive that ID from some prior request, then certainly the forward slash is escaped to differ that forward slash from "normal", folder separating forward slashes. Which indeed does not change that fact that the notation is _not_ a valid URL. – arkascha Mar 31 '16 at 13:17
  • The variable I'm attempting to use is the result of a prior API request. It's me being an idiot. I'm receiving an object response from the API, which I'm then using `json_encode` on. `json_encode` is rightfully attempting to escape any forward slash that is added, which confused me a little. I'm now using `json_encode` with the `JSON_UNESCAPED_SLASHES` flag and everything is working perfectly. – tombraider Mar 31 '16 at 13:22
  • 1
    Ah, wow, that is an interesting outcome! Congratulations! And... you certainly are not an idiot. An important part of our daily work as programmers simply is to make sense of artefacts we have to handle and deal with. It is a good thing sometimes to carry an issue out into public to receive back new ideas and insights. Sometimes the only way to make progress and learn. Which is what we all do all the time in the end :-) Take care and good luck with the project! – arkascha Mar 31 '16 at 13:26
  • @arkascha I think it's a good idea to add an answer to this question based on your comments. It'll make easier for others with a similar problem to find the solution than reading the comments thread. ;) – Gustavo Straube Apr 27 '16 at 13:31
  • http://stackoverflow.com/questions/21552604/how-to-define-a-laravel-route-with-a-parameter-that-contains-a-slash-character – Samuca Apr 04 '17 at 09:10

2 Answers2

0

You should not do that. Laravel will think it is a new parameter, because of the "/". Instead, use htmlentitites and html_entity_decode in your parameter, or use POST.

Brane
  • 3,257
  • 2
  • 42
  • 53
Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29
0

As you can see from the comments on the original question, the variable I was trying to pass in the URL was the result of a prior API call which I was using json_encode on. json_encode will automatically try and escape forward slashes, hence the additional \ being added to the variable. I added a JSON_UNESCAPED_SLASHES flag to the original call and voila, everything is working as expected.

tombraider
  • 1,127
  • 1
  • 11
  • 19