How can the below controller and view be setup to map multiple form input to api parameters
I have a view with a form that supports a single input criteria. The form input is then used by a controller to query a 3rd party API and return results from the response. I'm working with the Laravel Framework and am having trouble getting the controller to support multiple search criteria and map to additional API parameters.
Controller (handles the requests that are coming from the comics route.)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use GuzzleHttp\Client;
use Cache;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
class HomeController extends Controller
{
private $client;
public function __construct(){
$ts = time();
$hash = md5($ts . config('marvel.private_key') . config('marvel.public_key'));
$this->client = new Client([
'base_uri' => 'http://gateway.marvel.com/v1/public/',
'query' => [
'apikey' => config('marvel.public_key'),
'ts' => $ts,
'hash' => $hash
]
]);
}
public function comics(Request $request){
$search_term = '';
if($request->has('query')){
$search_term = $request->input('query');
$query = $this->client->getConfig('query');
$query['titleStartsWith'] = $search_term;
$response = $this->client->get('comics', ['query' => $query]);
$response = json_decode($response->getBody(), true);
$comics = $response['data']['results'];
}else{
$comics = Cache::get('comics');
shuffle($comics);
$comics = array_slice($comics, 0, 20);
}
return view('comics', ['comics' => $comics, 'query' => $search_term]);
}
Comcis View containing input form / rendering:
@extends('layouts.default')
@section('content')
<div id="content">
<h2>Comics</h2>
<form action="/comics">
<p>
<label for="query">Query</label>
<input type="text" name="query" id="query" value="">
<button>Search</button>
</p>
</form>
<div id="comics" class="results">
@foreach($comics as $com)
<article class="card">
<img src="/portrait_incredible.jpg" alt=" thumbnail">
<footer>
<h5>
<a href="/comics/" class="card-title"></a>
</h5>
<p>
</p>
</footer>
</article>
@endforeach
</div>
</div>
@stop
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire