Faiawuks
Faiawuks

Reputation: 81

A good way to link front-end to Symfony2 API?

I'm having a Symfony2 API, which I want to call from a single page front-end application. My current url is pointing to the /web directory of Symfony. So I can call the API trough app.php or app_dev.php. That works.

Now I want to couple my front-end app to the Symfony backend. What is a good way to do that? Sure I searched for .htaccess rewrites.

For example, what tought of was redirecting all requests in the url that point to */api to the app.php of Symfony and all the url requests that do not contain /api to the front-end app (symbolic link in /web directory that points to the vendor map with the front-end app in it).

Upvotes: 1

Views: 531

Answers (1)

Faiawuks
Faiawuks

Reputation: 81

I currently solved it changing the (default Symfony 2.6) .htaccess:

# (remove this line!) DirectoryIndex app.php 

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    # Instead of:
    # (removed) RewriteRule .? %{ENV:BASE}/app.php [L]

    # I did:

    # Redirect everything with %url%/api to the Symfony2 font controller.
    RewriteCond %{REQUEST_URI} api/
    RewriteRule .? %{ENV:BASE}/app.php/$1 [L]

    # Redirect everything else to another dir, possibly front-end app.
    RewriteRule .? %{ENV:BASE}/myfrontend/$1 [R,L,QSA,NE]
</IfModule>

Important is the [R,...]

Since this is possibly wrong, I hope someone still can give tips about how to properly do this.

Upvotes: 1

Related Questions