Variables don't work like that. Very hard to explain all of this, I would suggest looking up examples in the the php manual.
Creating a custom URL for an API (edited)
Im trying to pass a variable to a view but everytime I try to access in the browser I get a 404. I wanted to create a Laravel Playground but it's beyond me how to use it, i'll try to explain as much as I can and hopefully you could see the error and understand me, hopefully :-)
Via a search input, a user might type a keyword, when that happens a URL is generated (videos/keyword/magicword) , the route generated should then take them to a view (videos.keyword) and this is where 2 variables ($keyword['Title]), ($keyword['ID']), should take the form of some data from an associative array which points to the ID of a Youtube video and it's title.
The URL is generated thanks to a Livewire component but what makes the request to the associative array is in fact inside a Controller. The component that generates the URL, both the view and the class works fine, as expected.
The request however is formed from a method, this makes an HTTP GET request to an API like this:
public function keywordSearch($keyword)
{
$keyword++;
$keywordVideos = Http::get('http://thvid-api.herokuapp.com/videos/keyword/'. $keyword )->json();
$keywordVideo = $keywordVideos[0];
return view ('videos.keyword', compact('keywordVideo'));
}
So this is where I have set up the request to the endpoint, you can see that im concatenating my variable and replacing it so that it can match a valid (if provided) URL to the API.
The user should be able to watch a video with this method, once they clicked on a result, they should be taken to the view where the video player is rendered.
In reality this is not giving me the expected behaviour, it's instead redirecting to a missing resource or whatever it might be, it's giving me a 404.
Route: Route::get('/videos/keyword/{key}', [PlayVideosController::class, 'keywordSearch']);
It's a Laravel 8 project, im using Firefox on a Windows 10 machine (can't believe im still working on a Windows hehe, I miss my Mac environment)
That's all guys, I hope you can see the error I have no idea what could be wrong given that I have created other methods from this Controller and they work fine. If you need to see other kind of information please tell me! Thanks, appreciate you all!
I was able to fix my 404 issue, thanks to everybody for sharing their take on this, it was very much appreciated.
Turns out the endpoint that I was trying to make was wrong, I was attempting to make my GET request like this; videos/keyword/{keyword} where instead it had to be done like this: videos/{_id}, since I wanted to fetch a single array so that I could display their values on a page, I had to know the id for each array, so targeting the _id column was crucial.
The initial GET request was alright, the below URL will bring me in fact a collection of 21 results in 1 page,
$keywordResponse = Http::get('http://thvid-api.herokuapp.com/videos/keyword/'.$this->search.'/1/21')->json();
First numbers refers to the page(s) and the latter the total resources shown: /1/21/
Since I only wanted to target a specific video and I had to know their _id, I had to have a URL regarding that.
foreach($keywordResponse as $key => $keyword) {
$keywordResponse[$key]['localUrl'] = 'videos/keyword/' . $keyword['_id'];
}
I had to change this part specifying that I am only looking for the _id and this should be part of my "localUrl", which im rendering on a href in a forelse conditional.
By targeting this column now the URL reflects something like:
videos/keyword/magicsummer
I am still using the word "keyword", this is just my preference also this is where my custom URL starts to differ from the original way of fetching a single record on the API, anyway in my case I have a page called "keyword" where a template renders a player and this is in relation to the "keyword" concept, but DRYing up my code is still required, at this stage this "works" and I can see where I have some more work to do.
Wrapping things up after this was done the Laravel Controller now should be able to take it from here. The method ended looking like this:
public function keyword($id)
{
$keywordVideo = Http::get('http://thvid-api.herokuapp.com/videos/'. $id)->json();
return view ('videos.keyword', compact('keywordVideo'));
}
The URL will now take an $id and $keywordVideo will be used in to return the values on the template. So the problem was both in the Livewire, Laravel Controller and in my knowledge of fetching a single video. And I still need to dry up that code but for now this should be good, once again, as always this community has always been there for me, I hope this can serve as a starting point if somebody wishes to create a custom URL from an API. Thanks!
Please or to participate in this conversation.