vendredi 5 juillet 2019

Laravel: pass var to controller to retrieve item from storage dynamically

I have the following function in my FileController that retrieves a video from my storage (local):

function getVideo() {

    $video = Storage::disk('local')->get("uploads/map_name/name_of_video.mp4");
    $response = Response::make( $video, 200 );
    $response->header( 'Content-Type', 'video/mp4' );

    return $response;
}

In my web.php i have the following route:

Route::get('/get-video/', 'FileController@getVideo')->name('get-video');

Last, in my view, i have the following:

<video controls>

    <source src="" type="video/mp4">
    Your browser does not support the video tag.

</video>

This works fine. The video is shown in my view. However, i would like to make this function dynamic but i can't seem to get this working. I have made the following changes:

First, i passed a var to the getVideo function and pass it to the get function:

function getVideo($video_name) {

    $video = Storage::disk('local')->get("uploads/{$video_name}");
    $response = Response::make( $video, 200 );
    $response->header( 'Content-Type', 'video/mp4' );

    return $response;
}

Then, i changed the route to take a var:

Route::get('/get-video/{video_name}', 'FileController@getVideo')->name('get-video');

And finally, in my view, i pass the var to the route

<video controls>

    <source src="" type="video/mp4">
    Your browser does not support the video tag.

</video>

This, however, results in a 404 not found. I get this route: https://mysite.local/get-video/map_name/name_of_video.mp4.

Notice that the /upload part doesn't get rendered. However, if i add this manually, the route fails as well.

I already did the php artisan storage:link as well.

Sidenote: the /map_name/name_of_video.mp4 part is something I can retrieve from the database for each video.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire