Search code examples
phpsymfonyroutesfosrestbundle

Symfony2: My route matches in console, not in browser. Probably due to apache 2.4/php-fpm fcgi config


I am totally new to symfony, doing my first exploratory project.

I want to build a REST API, and I installed FOSRestBundle.

In my console, the output of php app/console router:debug get_usuarios:

[router] Route "get_usuarios"
Name         get_usuarios
Path         /usuarios.{_format}
Host         ANY
Scheme       ANY
Method       GET
Class        Symfony\Component\Routing\Route
Defaults     _controller: MciAPIBundle:Usuarios:getUsuarios
             _format: NULL
Requirements _format: json|xml|html
Options      compiler_class: Symfony\Component\Routing\RouteCompiler
Path-Regex   #^/usuarios(?:\.(?P<_format>json|xml|html))?$#s

and: php app/console router:match /usuarios

Route "get_usuarios" matches
[router] Route "get_usuarios"
Name         get_usuarios
Path         /usuarios.{_format}
Host         ANY
Scheme       ANY
Method       GET
Class        Symfony\Component\Routing\Route
Defaults     _controller: MciAPIBundle:Usuarios:getUsuarios
             _format: NULL
Requirements _format: json|xml|html
Options      compiler_class: Symfony\Component\Routing\RouteCompiler
Path-Regex   #^/usuarios(?:\.(?P<_format>json|xml|html))?$#s

but when I go to my browser and go http://localhost:7080/usuarios I get:

Object not found!

The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

If you think this is a server error, please contact the webmaster.

Error 404

localhost

I don't get it: if it says (in the console) that it matches, why doesn't it match in the browser?

app/config/routing.yml

get_usuarios:
    type: rest
    resource: Mci\APIBundle\Controller\UsuariosController

EDIT: To me it seems that the Controller is not even loaded ever. If I put garbage in that controller nothing happens, still the same 404 error

**EDIT 2:**I believe it has to do with my webserver 2.4 and php-fpm. I am using a fresh install of arch linux, which has apache 2.4. With this version of apache, php needs to be loaded (as an option) with php-fpm via fcgi. So I have a ProxyPassMatch ^/(..php(/.)?)$ fcgi://127.0.0.1:9000/var/www/html/$1 rule. Maybe this is conflicting with the rest api controller? Because how will the /usuarios URL be routed to the symfony framework...


Solution

  • OK, I found the answer.

    The problem was with my webserver configuration. I am using a fresh Arch linux installation - which uses apache 2.4.

    With apache 2.4, some changes need to be done in the default parametrization of php to be able to run it with apache. In fact, I now had to configure it to use php-fpm and load in apache through fcgi.

    I had thus to put the correct ProxyPass directive in the virtualhost conf in order for everything to work:

    DocumentRoot /home/user/project/web    
    ProxyPassMatch ^/(.*)$ fcgi://127.0.0.1:9000/home/user/project/web/app.php/$1
    

    Notably the ProxyPassMatch directive needs to match ALL URLs, not only php files, as I am using URLS with no php ending for the API (e.g.http://localhost:7080/usuarios)