badr's avatar
Level 1

how to pass URL String as parameter to Laravel routing

Hi , I have stuck on a problem in Laravel

I want to pass parameter with question mark , firstly I got question mark problem in URL but I solved by using

public function upload(Request $request)
{
return $request->query('v');
}

Route::get('/watch',"YoutubeController@upload");

for me the previous solution : the link will accept any URL like "locahost:8000/watch?v=youtubeId"

but the problem is when instead of youtube id I pass youtube video link ? what can I do ?

previous solution dosn't accept slash like

 http://localhost:8000/watch?v=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dq9FE7uNvvIk

or http://localhost:8000/watch?v=https://www.youtube.com/watch?v=q9FE7uNvvIk

how to solve this issue ? honestly I'm converting a project to laravel and it was fine with pure php

more info : I have input , And the parameter coming from user not from me, I only need to get what he type,

thanks

0 likes
8 replies
bashy's avatar
<a href="{{ url('watch', ['v' => 'youtubeId']) }}">Watch Video</a>
badr's avatar
Level 1

@Yoki how I will use those function , ? ? I can't even access to video url because it's coming from user form

badr's avatar
Level 1

@bashy I dont have youtubeId because the link is coming from user form . they will type YouTube link then I post or get what he did .

badr's avatar
Level 1

It seems like laravel issue , most of the people asked the same quastion and there was no real answer , for me I solved the problem by changing the way of submitting the form ,

what I did is : I used post action , then extracting video id form url and redirect to watch?v=youtubeId

Route::post('watch', "YoutubeController@redirectToId");

public function redirectToId()
{

    $query = parse_url(request('v'), PHP_URL_QUERY);
    parse_str($query, $params);
    $youtubeId = $params['v'];

    return redirect()->to(url('watch?v='.$youtubeId)) ;


}

this is the only solution that come to my mind :p

bashy's avatar

Well you didn't really explain much about where it was coming from.

If the user adds in the full URL to the video, you need to parse that input and take the ID after v=.

$video_id = str_after($request->input('v'), 'watch?v=');

Although having the user submit it via query string will cause it to parse it wrong (double ?v=)

badr's avatar
Level 1

@bashy I can't even access the input of the user . you can try , you will get this page is not available .

laravel routing dosnt even understand that this

http://localhost:8000/watch?v=https://www.youtube.com/watch?v=q9FE7uNvvIk

for this

Route::get('/watch',"YoutubeController@upload");

I'm getting not found page with previous example

bashy's avatar

It's because you're putting a URL into a URL... you need to urlencode() it. No way around it.

1 like

Please or to participate in this conversation.