liuggio / symfony2-rest-api-the-best-2013-way Goto Github PK
View Code? Open in Web Editor NEWCode for the article at:
Home Page: http://welcometothebundle.com/symfony2-rest-api-the-best-2013-way/
License: MIT License
Code for the article at:
Home Page: http://welcometothebundle.com/symfony2-rest-api-the-best-2013-way/
License: MIT License
Hello.
I'm trying to implement a new REST API in my project, but I'm getting the same errors I get when I test liuggio/symfony2-rest-api-the-best-2013-way
: the PUT
HTTP calls are always returned as text/html
(instead of application/json
), and fail in the assertJsonResponse
assertion. The other methods always return application/json
.
Did anyone have the same problem?
PHPUnit 3.7.38 by Sebastian Bergmann.
Configuration read from /Users/rodrigorigotti/Sites/liuggio/symfony2-rest-api-the-best-2013-way/app/phpunit.xml.dist
.....F.F......
Time: 4.93 seconds, Memory: 30.75Mb
There were 2 failures:
1) Acme\BlogBundle\Tests\Controller\PageControllerTest::testJsonPutPageActionShouldModify
Allow: GET, PUT, PATCH
Cache-Control: no-cache
Date: Fri, 24 Oct 2014 17:46:27 GMT
Location: http://localhost/api/v1/pages/16.json
Set-Cookie: MOCKSESSID=5bde664cd92ea4a6054d789e41008cc175afd08a; path=/
Failed asserting that false is true.
/Users/rodrigorigotti/Sites/liuggio/symfony2-rest-api-the-best-2013-way/src/Acme/BlogBundle/Tests/Controller/PageControllerTest.php:199
/Users/rodrigorigotti/Sites/liuggio/symfony2-rest-api-the-best-2013-way/src/Acme/BlogBundle/Tests/Controller/PageControllerTest.php:126
2) Acme\BlogBundle\Tests\Controller\PageControllerTest::testJsonPatchPageAction
Allow: GET, PUT, PATCH
Cache-Control: no-cache
Date: Fri, 24 Oct 2014 17:46:27 GMT
Location: http://localhost/api/v1/pages/18.json
Set-Cookie: MOCKSESSID=8c619cf2c11c32a72a303eb8cd68ba72fbd0becb; path=/
Failed asserting that false is true.
/Users/rodrigorigotti/Sites/liuggio/symfony2-rest-api-the-best-2013-way/src/Acme/BlogBundle/Tests/Controller/PageControllerTest.php:199
/Users/rodrigorigotti/Sites/liuggio/symfony2-rest-api-the-best-2013-way/src/Acme/BlogBundle/Tests/Controller/PageControllerTest.php:175
FAILURES!
Tests: 14, Assertions: 49, Failures: 2.
Fresh install out the box following the instructions, first one is in Tests/Controller/PageControllerTest.php on line 68, this line:
$this->assertEquals(
'{"children":{"title":[],"body":[]}}',
$this->client->getResponse()->getContent(),
$this->client->getResponse()->getContent()
);
should be this instead:
$this->assertEquals(
'{"children":{"title":{},"body":{}}}',
$this->client->getResponse()->getContent(),
$this->client->getResponse()->getContent()
);
It looks like the excellent article content at http://welcometothebundle.com/symfony2-rest-api-the-best-2013-way/ is missing. Is that, by chance, duplicated anywhere?
It looks like the excellent article content at http://welcometothebundle.com/symfony2-rest-api-the-best-2013-way/ is missing. Is that, by chance, duplicated anywhere?
PageHandler::createPage, in 'part1' tag, is never used
Tests show these errors
vagrant@precise64:/var/www/symfony$ bin/phpunit -c app
PHPUnit 3.7.37 by Sebastian Bergmann.
Configuration read from /var/www/symfony/app/phpunit.xml.dist
...F.FFF.......EE
Time: 52.64 seconds, Memory: 51.50Mb
There were 2 errors:
1) BoShurik\SurveyBundle\Tests\Controller\SurveyControllerTest::testCompleteScenario
InvalidArgumentException: The current node list is empty.
/var/www/symfony/vendor/symfony/symfony/src/Symfony/Component/DomCrawler/Crawler.php:697
/var/www/symfony/src/BoShurik/SurveyBundle/Tests/Controller/SurveyControllerTest.php:18
2) BoShurik\UserBundle\Tests\Controller\RegistrationControllerTest::testRegistration
InvalidArgumentException: The current node list is empty.
/var/www/symfony/vendor/symfony/symfony/src/Symfony/Component/DomCrawler/Crawler.php:737
/var/www/symfony/src/BoShurik/UserBundle/Tests/Controller/RegistrationControllerTest.php:30
--
There were 4 failures:
1) Acme\BlogBundle\Tests\Controller\PageControllerTest::testJsonPostPageAction
{"code":400,"message":"Validation Failed","errors":{"errors":["The CSRF token is invalid. Please try to resubmit the form."],"children":{"title":[],"body":[]}}}
Failed asserting that 400 matches expected 201.
/var/www/symfony/src/Acme/BlogBundle/Tests/Controller/PageControllerTest.php:194
/var/www/symfony/src/Acme/BlogBundle/Tests/Controller/PageControllerTest.php:81
2) Acme\BlogBundle\Tests\Controller\PageControllerTest::testJsonPutPageActionShouldModify
{"code":400,"message":"Validation Failed","errors":{"errors":["The CSRF token is invalid. Please try to resubmit the form."],"children":{"title":[],"body":[]}}}
Failed asserting that 400 matches expected 204.
/var/www/symfony/src/Acme/BlogBundle/Tests/Controller/PageControllerTest.php:194
/var/www/symfony/src/Acme/BlogBundle/Tests/Controller/PageControllerTest.php:125
3) Acme\BlogBundle\Tests\Controller\PageControllerTest::testJsonPutPageActionShouldCreate
{"code":400,"message":"Validation Failed","errors":{"errors":["The CSRF token is invalid. Please try to resubmit the form."],"children":{"title":[],"body":[]}}}
Failed asserting that 400 matches expected 201.
/var/www/symfony/src/Acme/BlogBundle/Tests/Controller/PageControllerTest.php:194
/var/www/symfony/src/Acme/BlogBundle/Tests/Controller/PageControllerTest.php:155
4) Acme\BlogBundle\Tests\Controller\PageControllerTest::testJsonPatchPageAction
{"code":400,"message":"Validation Failed","errors":{"errors":["The CSRF token is invalid. Please try to resubmit the form."],"children":{"title":[],"body":[]}}}
Failed asserting that 400 matches expected 204.
/var/www/symfony/src/Acme/BlogBundle/Tests/Controller/PageControllerTest.php:194
/var/www/symfony/src/Acme/BlogBundle/Tests/Controller/PageControllerTest.php:174
FAILURES!
Tests: 17, Assertions: 47, Failures: 4, Errors: 2.
//composer file
{
"name": "symfony/framework-standard-edition",
"license": "MIT",
"type": "project",
"description": "The \"Symfony Standard Edition\" distribution",
"autoload": {
"psr-0": { "": "src/" }
},
"require": {
"php": ">=5.3.3",
"symfony/symfony": "~2.4",
"doctrine/orm": "~2.2,>=2.2.3",
"doctrine/doctrine-bundle": "~1.2",
"twig/extensions": "~1.0",
"symfony/assetic-bundle": "~2.3",
"symfony/swiftmailer-bundle": "~2.3",
"symfony/monolog-bundle": "~2.4",
"sensio/distribution-bundle": "~2.3",
"sensio/framework-extra-bundle": "~3.0",
"sensio/generator-bundle": "~2.3",
"incenteev/composer-parameter-handler": "~2.0",
"friendsofsymfony/user-bundle": "~2.0@dev",
"friendsofsymfony/rest-bundle": "@dev",
"jms/serializer-bundle": "@dev",
"nelmio/api-doc-bundle": "@dev"
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "dev-master",
"phpunit/phpunit": "3.7.*",
"liip/functional-test-bundle":"dev-master",
"phpunit/php-invoker":"~1.1"
},
"scripts": {
"post-install-cmd": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
],
"post-update-cmd": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
]
},
"config": {
"bin-dir": "bin"
},
"extra": {
"symfony-app-dir": "app",
"symfony-web-dir": "web",
"incenteev-parameters": {
"file": "app/config/parameters.yml"
},
"branch-alias": {
"dev-master": "2.4-dev"
}
}
}
Hello,
I keep getting 500 error responses, instead of error messages.
{
"error": {
"code": 500,
"message": "Internal Server Error"
}
}
It's because of the InvalidFormException, that is never returning JSON somewhere. What should I change that it'll show a JSON-response when something is wrong?
I get a whole list of the Expeption handler when I'm using app_dev.php, but without ap_dev.php I just get the error 500 message from above.
When you use view annotation then whatever you return in your controller will be considered as data to the view template, so when using this code:
try {
$newPage = $this->container->get('acme_blog.page.handler')->post(
$request->request->all()
);
$routeOptions = array(
'id' => $newPage->getId(),
'_format' => $request->get('_format')
);
return $this->routeRedirectView('api_1_get_page', $routeOptions, Codes::HTTP_CREATED);
} catch (InvalidFormException $exception) {
return $exception->getForm();
}
the view handler will use the view returned by routeRedirectView as the templateData of the annotation view instead of displaying it.
So to prevent this from happening the @view should be removed, but then the function should always return a view like this :
try {
$newPage = $this->container->get('acme_blog.page.handler')->post(
$request->request->all()
);
$routeOptions = array(
'id' => $newPage->getId(),
'_format' => $request->get('_format')
);
return $this->routeRedirectView('api_1_get_page', $routeOptions, Codes::HTTP_CREATED);
} catch (InvalidFormException $exception) {
return $this->view($exception->getForm(), Codes::HTTP_BAD_REQUEST)
->setTemplate('AcmeBlogBundle:Page:newPage.html.twig');
}
and it should be working as expected.
Hello
we have used your rest api tutorial and all those are working fine,we have create new table and add date field on it, now when we have try to insert new record using curl from command line on that time we have problem in inserting date using following command
$curl -X POST -d '{"name":"testname","age":"25", "birthdate":"11-02-1991"}' "Content-Type:application/json"
is it an idea to also make a NoSQL database for for this? Like with MongoDB.
I can see this in two places.
input = "Acme\DemoBundle\Form\PageType",
However when I've tried changing this to BlogBundle, both title and body become required.
When trying curl on patch/put route, it tells me that method isn't allowed. Only post is allowed. Im on apache.
Hi,
let's say I want to have a service in my API that creates multiple entities. For example: I want to have a service like /api/car/new but to create a car you need to create and engine entity wheel entity and so on.
How flexible is the bundle to allow in one API service to create more entities? Reading the documentation it seems that's just meant to do CRUD of any entity. Where do I need to start exploring in the code to achieve this kind of behaviour on the services?
Thank you.
I wonder if you are interested to migrate the project to symfony 4 in case I'm happy to proceed in the migration. ???
Hi,
I am new to symfony. I have installed the symfony blog code and its working fine on web browser (forms& api doc).
But If I tried on curl call I am getting error
curl -X POST -d '{"title":"title","body":"body"}' http://localhost:8000/api/v1/pages.json --header "Content-Type:application/json"
-header "Content-Type:application/json"
{"code":400,"message":"Validation Failed","errors":{"errors":["The CSRF token is invalid. Please try to resubmit the form."],"children":{"title":[],"body":[]}}}
{"code":400,"message":"Validation Failed","errors":{"errors":["The CSRF token is invalid. Please try to resubmit the form.","This form should not contain extra fields."],"children":{"title":{"errors":["This value should not be blank.","This value should not be null."]},"body":[]}}}
Help me to understand and solve the issue....
Thanks,
SVN
It would be great, if you added user is authentication to the article.
One example for fos/userbundle - this is simple
And one for api key authentification
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.