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.

9
  • 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. Commented Mar 31, 2016 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". Commented Mar 31, 2016 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? Commented Mar 31, 2016 at 13:06
  • 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. Commented Mar 31, 2016 at 13:17
  • 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! Commented Mar 31, 2016 at 13:26

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.