justme
justme

Reputation: 43

Creating Routing Paths using FosRestFundle in Symfony

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

Upvotes: 0

Views: 250

Answers (1)

kstupak
kstupak

Reputation: 76

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

Upvotes: 1

Related Questions