Symfony make a call to a controller (REST API)

0

Good, I have an environment with symfony3 installed and my intention is to make an api rest, use as web server NGINX and as a proxy reverse apache, that is to say in port 80 I have NGINX and in port 8080 Apache.

I have developed a test project with Symfony3.3 and if I run the command by console to see the routes I see the following

The controller I have created it in this way

<?php
namespace AppBundle\Controller\Api\v1;

use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\BrowserKit\Response;


class UsuariosController extends Controller
{
 /**
  * @Route("/api/v1/nombre")
  * @Method("POST")
  */
 public function nombreAction(Request $request)
 {
    $nombre = $request->get('nombre');
    return new response('its ok',201);
 }
}

The routes file in yml format is this

app:
 resource: '@AppBundle/Controller/'
 type: annotation
oneup_uploader:
resource: .
type: uploader

Now I have the problem when trying to execute this function of the controller with the POST method for it by using POSTMAN I make a call to link

But I get an error code 404

Thanks

    
asked by ilernet 17.11.2017 в 13:27
source

1 answer

0

The request you are making seems to be in the production environment, usually /app_dev.php is for the development environment and /app.php for production. If you omit the front controller the default behavior in the .htaccess is to go to the PROD environment.

The routes in Symfony are cached in production so if you have added the route but you have not cleaned the cache it will not be accessible.

To check if the route is accessible in PROD you can do "php bin / console debug: router --env = prod", however during the development it is much easier to use link

Regarding the API, I recommend you use API Plataform is a Symfony distribution with everything you need for the api already prepared.

    
answered by 18.11.2017 в 21:35