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

StackOverflow https://stackoverflow.com/questions/23576267

문제

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...

도움이 되었습니까?

해결책 2

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)

다른 팁

If you look at the out put of your command php app/console router:debug get_usuarios: you can see the route allowed json|xml|html as format but default value of your format is NULL.

Defaults             _controller: MciAPIBundle:Usuarios:getUsuarios
                          _format: NULL
Requirements    _format: json|xml|html

That means your format parameter is not an optional field. So you need to access http://localhost:7080/usuarios.json or http://localhost:7080/usuarios.xml or http://localhost:7080/usuarios.html to access this resource.

Now to the answer of your question:

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

The php app/console router:match /usuarios is simulation of path info match. i.e. what it does is just find out the matched router pattern.

If you like to access this resource with http://localhost:7080/usuarios, you have to provide a default format option for your router. LIke:

get_usuarios:
    type: rest
    resource: Mci\APIBundle\Controller\UsuariosController
    defaults: { _format: json }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top