apisyouwonthate / build-apis-you-wont-hate Goto Github PK
View Code? Open in Web Editor NEWResources for the book: Building APIs You Won't Hate
Home Page: http://apisyouwonthate.com
License: MIT License
Resources for the book: Building APIs You Won't Hate
Home Page: http://apisyouwonthate.com
License: MIT License
Hi there,
First of all, thank for writing that book i'm really enjoying reading it.
But I'm getting stuck in the ApiController (i think). I'm getting the error Undefined property: ArticleController::$fractal
.
Well my ApiController.php is copied from Chapter 6, my Transformer are loaded by my composer just like yours and my controller is something similar:
public function index()
{
$articles = Article::take(10)->get();
return $this->respondWithCollection($articles, new ArticleTransformer);
}
Fractal is well loaded as well I think because I already test it standalone inside that controller function.
I'm using "laravel/framework": "4.2.*"
and "league/fractal": "0.8.*"
. Could this be a version issue?
Thanks in advance.
Yo Phil!
I was hoping to steal, ahem, borrow part (or much/all) of https://github.com/philsturgeon/build-apis-you-wont-hate/blob/master/behat/FeatureContext.php for the REST tutorial at KnpUniversity because I think your tests are swell.
So, what's the LICENSE on the code? I of course want to give ample credit back to your book from the screencast - let me know if it's a yay or nay.
Thanks!
Not sure if I should post this in this repo or the Fractal one, but whatever.
It appears that when fetching embedded resources, that happens after the parent resource is already fetched from the database. This means that if the parent has hundreds of items, fetching their embedded resources results in hundreds of db calls. Am I right, or am I missing something?
In my current project (not using Fractal at the moment), I have my repository (abstracted from my models) handle the query vars and if embeds are requested, they are immediately sent to the model (firing Actor::with(['movies'])->all()
) for eager loading.
Then, in my own transformer, I do something like this:
<?php namespace Services\Transformation;
class ActorTransformer extends BaseTransformer implements TransformerInterface {
protected $embedables = [
'images' => 'ImageTransformer',
'movies' => 'MovieTransformer'
];
// more code
The BaseTransformer:
<?php namespace Services\Transformation;
abstract class BaseTransformer {
/**
* The repositories that can be embedded in an instance of
* the current resource.
* @var array
*/
protected $embedables;
/**
*
*
* @param $instance object The parent repository (Eloquent)
* @param $data array The transformed data we need to add to
* @return array The data array with the embeds added to it
*/
protected function addEmbeds($instance, $data)
{
foreach ($this->embedables as $embed => $transformer) {
if (isset($instance->$embed)) {
$transformerName = __NAMESPACE__ . '\\' . $transformer;
$subResource = new $transformerName();
if ($subResource instanceof TransformerInterface) {
$data[$embed] = $subResource->transformCollection($instance->$embed);
}
}
}
return $data;
}
The transformer loops through all the $embedables, (which, I see now should be spelled with two d's), checks if the item has a property called 'images' or 'movies' and if so, that property will be parsed by its own transformer (which one it should load is specified in the associative array).
It's still in a very early stage, my own transformer project, but I thought I'd share.
There is a bug in the User.php where you are have $return, it should say "return". This is in all chapters.
Also, you might want to update the code to use Fractal 0.8.*. Great book BTW, I am up and running with the JSON API Serilaizer.
(Wasn't sure where to contact with questions)
This is in regards to how you recommend structuring data.
With responses it seems really obvious; most of the body goes in a data
property.
It wasn't as clear to me in the book what you propose for requests though. Should they mirror responses, and also go in a data
property, or would they be better off sending everything in the root object?
The one example I can find (on page 17) is a payload of {"user_id":2}
. After that it becomes a little hard to follow which examples are talking about responses and which are meant to be generalized.
Behat Config
default:
context:
parameters:
base_url: http://67.205.138.216:80
~ ~
Running the server
root@Box:~/build-apis-you-wont-hate/chapter6# jobs
[1]+ Running sudo php artisan serve --host 67.205.138.216 --port 80 &
Running Behat
root@Box:~/build-apis-you-wont-hate/chapter6# behat -c app/tests/behat/behat-dev.yml
Feature: Places
Scenario: Returning a collection of places # features/places.feature:3
PHP Fatal error: Uncaught TypeError: Argument 2 passed to GuzzleHttp\Adapter\StreamAdapter::createResponseObject() must be of the type array, null given, called in /root/build-apis-you-wont-hate/chapter6/vendor/guzzlehttp/guzzle/src/Adapter/StreamAdapter.php on line 67 and defined in /root/build-apis-you-wont-hate/chapter6/vendor/guzzlehttp/guzzle/src/Adapter/StreamAdapter.php:71
Stack trace:
#0 /root/build-apis-you-wont-hate/chapter6/vendor/guzzlehttp/guzzle/src/Adapter/StreamAdapter.php(67): GuzzleHttp\Adapter\StreamAdapter->createResponseObject(Object(GuzzleHttp\Message\Request), NULL, Object(GuzzleHttp\Adapter\Transaction), Object(GuzzleHttp\Stream\Stream))
#1 /root/build-apis-you-wont-hate/chapter6/vendor/guzzlehttp/guzzle/src/Adapter/StreamAdapter.php(52): GuzzleHttp\Adapter\StreamAdapter->createResponse(Object(GuzzleHttp\Adapter\Transaction))
#2 /root/build-apis-you-wont-hate/chapter6/vendor/guzzlehttp/guzzle/src/Client.php(193): GuzzleHttp\Adapter\StreamAdapter->send(Object(GuzzleHttp\Adapter\Transaction))
#3 /root/bu in /root/build-apis-you-wont-hate/chapter6/vendor/guzzlehttp/guzzle/src/Adapter/StreamAdapter.php on line 71
I bought Phil's book today and must say it's a good read.
I'm currently writing Behat tests for my API and want to try testing a post with a payload. I have a scenario like this:
Given I have the payload:
"""
{"name": "foo_name"}
"""
When I request "POST /api/users"
Then I get a "200" response
When running the test, it seems the payload is not sent. Debugging learns that Input::get('name')
in my store() function returns 422 Unprocessable Entity
.
Other test, without payload, are running fine. Posting the same payload in Chrome's RestClient does work correctly.
Any ideas on what's going on here?
Thanks
I use Homestead and in PHP 7.2 it doesn't even have Mcrypt
When I try to access the "dev.book6.local" url that I've configured in Homestead and host file, I get:
Mcrypt PHP extension required.
Same thing on php artisan.
(From readme in chapter 5)
$ php artisan migrate
PHP Warning: require(/home/noah/build-apis-you-wont-hate/chapter5/bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in /home/noah/build-apis-you-wont-hate/chapter5/bootstrap/autoload.php on line 17
PHP Fatal error: require(): Failed opening required '/home/noah/build-apis-you-wont-hate/chapter5/bootstrap/../vendor/autoload.php' (include_path='.:/usr/share/php') in /home/noah/build-apis-you-wont-hate/chapter5/bootstrap/autoload.php on line 17
The file chapter5/bootstrap/autoload.php has the line
require DIR.'/../vendor/autoload.php';
but it doesn't exist.
Hello,
Today I found out that running the tests on the chapter5 using the current behat version gives the following error:
[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
Unrecognized option "context" under "testwork"
I found out that this is due to some changes on the spec from 2.5.x to 3.x
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.