logo
Ask your Symfony questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
Best strategy for FOSRest + UI Web together

Best strategy for FOSRest + UI Web together.
by aggressivex » Sat Jun 08, 2013 8:56 pm

Recently I'm working on a project that follows a common pattern

panel.domain.com (UI web)
api.domain.com (REST api)

( basically same actions ) They are in the same Symfony project and same bundles, but I use different environments. So in that way I only have to maintain one project.
My UI web uses backbone so I'd like to use REST apis for the UI web also but from the same domain to avoid authentication between subdomains etc.

After this point everything is clear so far.

But right now I've got the controllers that generates the CRUD generator ( for UI Web ) with Route annotations etc. and other controller like: EntityRestController for REST with FOSRest annotations and using a route with a rest/ prefix panel.domain.com/rest/… The things is I've got a lot of controllers that do the same logic and I'm not sure if these is a nice way to achive this.

What is the best strategy to maintain a REST api and Web actions ?
It's possible to combine FOSRest with Common Routes within the same controller?

aggressivex | 06/09/13 at 8:24am Edit
Tutorial: How to assign prize money


(5) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    06/09/13
    8:31am
    Luis Cordova says:

    is possible to use more than one @Route if that is what you mean
    and on Rest bundle configuration sort out the format and priorities to match your needs
    on symfonylive i saw two talks that talked about how to make rest automation easier for these cases

    you are tackling a major problem we all have so share your conclusions with me cordoval at gmail

  • avatar
    Last edited:
    06/10/13
    7:31am
    Florian Klein says:

    First of all, ask yourself if it is a good idea to make controllers able to response everything (html AND rest api).
    If yes, just decide which route loader you want to use:

    - FOSRest (naming conventions of actions / annotations)
    - yaml (you decide of the name)
    - sensioFrameworkExtra (annotations)

    Once these routes are generated and correct, use the same routes for html (web UI) AND rest api.

    Concerning host name separation, if you run under symfony 2.2, you can have the same route on many subdomains: http://symfony.com/doc/2.2/components/routing/hostname_pattern.html
    If you run under symfony < 2.2, try to hack some rewrite rules and keep a url prefix internally.

    If you do not like this idea, and prefer having 2 types of controllers, you can still avoid controller code duplication by moving common code to a separate service that can be called from both controllers.

    PS: separate controllers for api and html can be a good idea, since most of the time html is far from following REST pattern.

    And I forgot to answer your 3 points:

    1) https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/3-listener-support.md#view-response-listener
    2) In case of security, maybe annotation are not the best option. What about using access map with common url pattern under /api and /panel ?
    3) It wont change any problem above if you make your own viw listener.

    Previous versions of this answer: 06/10/13 at 7:30am | 06/10/13 at 7:31am

  • avatar
    Last edited:
    06/09/13
    10:52am
    aggressivex says:

    Yep I knew that's possible.

    The issue is beyond that.

    Actually my routes fits a REST pattern without FOSRestBundle. The problem is that FOSRestBundle and FrameworkExtraBundle views management conflict each other with the content negotiation.

    So …

    1: To know if FOSRestBundle could work with FrameworkExtraBundle together and fallback to HTML depending on negotiation
    2: Duplicate all controllers , with security annotations etc. etc.. to setup for REST in other url pattern
    3: Implement my own REST manipulating KernelRespone …


    There are more approaches ? What is the best ? Why ? Issues on each approach ?

  • avatar
    Last edited:
    06/10/13
    8:17am
    aggressivex says:

    Hi, I Really appreciate your answer


    First of all, ask yourself if it is a good idea to make controllers able to response everything (html AND rest api).
    If yes, just decide which route loader you want to use:


    Well that basically one of my questions. I already ask my self the best approach


    Once these routes are generated and correct, use the same routes for html (web UI) AND rest api.



    As I commented before, looks that you can't have same routes for UI and Rest. As I said to Luis Cordova FOSRest doesn't have the possibility to determine the response based on content negotiation.


    Concerning host name separation


    Never was a concern, that's easy to achieve with different kernels / environments


    PS: separate controllers for api and html can be a good idea, since most of the time html is far from following REST pattern.


    As I comment before my routes fits a REST pattern without FOSRestBundle. I think controllers should be clean enough to fit the two scenarios, each action controller should be doing the action that name itself, nothing more, no loading or prepare other HTML things. Thats should be delegated to views or listeners …



    3) It wont change any problem above if you make your own viw listener.


    Actually finally I think that's the best options, I've commented in github and the creator of FOSRest just comment that Time ago It wasn't look right… But know I think the best option is to drop FOSRest and use my own listeners.

    https://github.com/FriendsOfSymfony/FOSRestBundle/issues/335#issuecomment-19173529


    Not the best answer but at least is regarding the topic and comment some Ideas. If I don't get something better I'll check this answer as ok.

    Thanks!!

  • avatar
    Last edited:
    06/11/13
    7:17pm
    aggressivex says:

    ^^ How the hell do you close a question here ?

This question has expired.





Current status of this question: Community pot



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.