Wraith
Wraith

Reputation: 501

How to pass whole url as params?

I want allow user to scan QR code and go to website. This is search results with params in url address like

http://example.com/qr-code/http://example.org/search/user_id=12&model_id=4&role_id=8

My route is set as

Route::get('qr-code/{data?}/{size?}', 'QrController@qrCode')->name('qr-code');

so i hope my $data will get url:

http://example.org/search/user_id=12&model_id=4&role_id=8

My Controller has

public function qrCode(Request $request, $data = null, $size = 60) {
    dd(88);
    $data = (empty($data)) ? env('APP_URL') : $data;
    return view('qr.code')->with('qr', QrCode::size($size)->generate($data));
} 

But i can't even see dd(88) route redirect to main page. How can i fix that ?

Upvotes: 0

Views: 338

Answers (2)

Wraith
Wraith

Reputation: 501

Ok i took i added to helper

function base64url_encode($s) {
    return str_replace(array('+', '/'), array('-', '_'), base64_encode($s));
}

function base64url_decode($s) {
    return base64_decode(str_replace(array('-', '_'), array('+', '/'), $s));
}

And then i am getting URI from request on view like

<a href="#" data-url="{{ base64url_encode($request->getRequestUri()) }}">Click me</a>

From JS i send params to QrController

public function qrCode(Request $request, $data = null, $type = null, $size = 200) {
    $data = (empty($data)) ? env('APP_URL') : $data;
    if ($type == 'url') $data = env('APP_URL') . base64url_decode($data);

    return view('qr.code')->with('qr', QrCode::size($size)->generate($data));
} 

And works fine. Base64 has + or / sometimes so thats why need to replace both - and _ and second is i add base url from env to make whole address.

Thx

Upvotes: 0

Maurice
Maurice

Reputation: 280

The thing is that Laravel doesn't know that you will provide an entire URL as a parameter. He could interpret everything like that: http://example.com/qr-code/http:/ /search/user_id=12&model_id=4&role_id=8 . Try encoding the URL diffrently. Your $data will be "http:/" and your size will be 60.

Upvotes: 1

Related Questions