for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace App\Http\Controllers;
use GuzzleHttp\Client;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class NytController extends Controller
{
/**
* Instance of Guzzle Client
* @var object
*/
protected $client;
* BaseUrl
* @var string
protected $baseUrl;
* Initialize the Controller with necessary arguments
public function __construct()
$this->baseUrl = 'http://api.nytimes.com/svc';
$this->client = new Client(['base_uri' => $this->baseUrl]);
$relativeUrl = '/books/v3/lists/overview.json?api-key=' . env('NYT_BOOKS_API_KEY');
$this->setGetResponse($relativeUrl);
}
* Get the response from New York times API
* @param string $relativeUrl
private function setGetResponse($relativeUrl)
$this->response = $this->client->get($this->baseUrl . $relativeUrl, []);
response
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
* Get the whole response from a get operation
* @return array
private function getResponse()
return json_decode($this->response->getBody(), true);
* Get the data response from a get operation
private function getData()
return $this->getResponse()['results']['lists'][0]['books'];
* Return all the data to the New York times API dashboard
public function getPage()
$data = $this->getData();
return view('api.nyt')->withData($data);
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: