create a controller in your bundle.
Save it to AppBundle/controllers/Api/UserController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: root | |
* Date: 16.07.16 | |
* Time: 15:34 | |
*/ | |
namespace AppBundle\Controller\Api; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\Response; | |
class UserController extends Controller { | |
/** | |
* @Route("/api/user") | |
* @Method("POST") | |
*/ | |
public function newAction() | |
{ | |
return new Response('this is it'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app_api: | |
resource: "@AppBundle/Controller/Api" | |
type: annotation |
But we wan to write a short testfile, to test our api with gusszle.
add guzzle to your composer composer
cmd->require eightpoints/guzzle-bundle
This loads guzzle.
Create a file named test.php in your project root
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require __DIR__ . '/vendor/autoload.php'; | |
$client = new \GuzzleHttp\Client(); | |
$response = $client->post('http://localhost:8000/api/user'); | |
echo $response->getBody(); |
php test.php
You will get your controller response directly.
Keine Kommentare:
Kommentar veröffentlichen