0

I am developing an Api for one of my projects in Symfony and I want create several routing paths like the following:

/api/messages/{id}
/api/messages/creator/{username}
/api/messages/receiver/{username}

My routing files are configured in this way (I will post just the info relating to the api config):

routing.yml:

NelmioApiDocBundle:
    resource: "@NelmioApiDocBundle/Resources/config/routing.yml"
    prefix: "/doc"

api:
    type: rest
    resource: "routing_api.yml"
    prefix: /api

routing_api.yml:

api_message:
    type: rest
    resource: AppBundle\Controller\ApiMessagesController

api_chats:
    type: rest
    resource: AppBundle\Controller\ApiChatController

And my ApiMessagesController:

<?php
namespace AppBundle\Controller;

use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\View\RouteRedirectView;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;

/**
* Class ApiMessagesController
* @package AppBundle\Controller
*
* @RouteResource("messages")
*/
class ApiMessagesController extends FOSRestController{

    /**
    * Gets an individual Message
    *
    * @param int $id
    * @return mixed
    * @throws \Doctrine\ORM\NoResultException
    * @throws \Doctrine\ORM\NonUniqueResultException
    *
    * @ApiDoc(
    *     output="AppBundle\Entity\Message",
    *     statusCodes={
    *         200 = "Returned when successful",
    *         404 = "Return when not found"
    *     }
    * )
    */
    public function getAction($id){;
        return $this->get('app.message.manager')->findById($id);
    }

    /**
     * Gets all messages sent by each users
     *
     * @param int $id
     * @return mixed
     * @throws \Doctrine\ORM\NoResultException
     * @throws \Doctrine\ORM\NonUniqueResultException
     *
     * @ApiDoc(
     *     output="AppBundle\Entity\Message",
     *     statusCodes={
     *         200 = "Returned when successful",
     *         404 = "Return when not found"
     *     }
     * )
     */
    public function getCreatorAction($creator){
        return $this->get('app.message.manager')->findByCreator($creator);
    }


}

The route /api/messages/{id} works as I expected but I am really stuck right now about how to define the route /api/messages/creator/{creator} because Nelmio is telling me that I created this route /api/messages/{creator}/creator instead of /api/messages/creator/{username}

I think that I put all info regarding to my issue, but if something is missing tell me and I will update my question. Thanks

1 Answer 1

1

If you're not happy with URLs FOSRest does, you can always use @Route annotation and redefine everything you need.

Sign up to request clarification or add additional context in comments.

2 Comments

Adding just @Route Annotation didn't solve my problem (it duplicated my routes). But you gave me the tip to fix it, removing "@RouteResource("messages")" is enough. Thanks!
Just adding the @Route annotation is not enough, you should do @Route("/api/messages/creator/{username}")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.