我是symfony的新手,这是我的第一个探索性项目。
我想构建一个REST API,我安装了FOSRestBundle。
在控制台中,php app/console router:debug get_usuarios:
[router] Route "get_usuarios"
Name get_usuarios
Path /usuarios.{_format}
Host ANY
Scheme ANY
Method GET
Class SymfonyComponentRoutingRoute
Defaults _controller: MciAPIBundle:Usuarios:getUsuarios
_format: NULL
Requirements _format: json|xml|html
Options compiler_class: SymfonyComponentRoutingRouteCompiler
Path-Regex #^/usuarios(?:.(?P<_format>json|xml|html))?$#s
和: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 SymfonyComponentRoutingRoute
Defaults _controller: MciAPIBundle:Usuarios:getUsuarios
_format: NULL
Requirements _format: json|xml|html
Options compiler_class: SymfonyComponentRoutingRouteCompiler
Path-Regex #^/usuarios(?:.(?P<_format>json|xml|html))?$#s
,但当我去我的浏览器,去http://localhost:7080/usuarios
我:
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
我不明白:如果它(在控制台中)说它匹配,为什么它在浏览器中不匹配?
app/config/routing.yml
get_usuarios:
type: rest
resource: MciAPIBundleControllerUsuariosController
编辑:对我来说,似乎控制器甚至没有加载过。如果我在控制器中放入垃圾什么都不会发生,还是相同的404错误
**编辑2:**我相信这与我的webserver 2.4和php-fpm有关。我正在使用新安装的arch linux,它有apache 2.4。对于这个版本的apache, php需要通过fcgi使用php-fpm加载(作为一个选项)。所以我有一个ProxyPassMatch ^/(。。php(/。) ?) $ fcgi://127.0.0.1:9000/var/www/html/1美元的统治。也许这与其他api控制器相冲突?因为/usuarios URL如何被路由到symfony框架…
如果您查看命令php app/console router:debug get_usuarios:
的输出,您可以看到路由允许json|xml|html作为格式,但您的格式的默认值是NULL
。
默认值 _controller: MciAPIBundle:Usuarios:getUsuarios
,,,,,,,,,,,,,,,,,,,,,,,, _format:空
需求,,xml _format: json | | html
这意味着你的格式参数不是一个可选字段。因此,您需要访问http://localhost:7080/usuarios.json
或http://localhost:7080/usuarios.xml
或http://localhost:7080/usuarios.html
来访问该资源。
现在回答你的问题:
我不明白:如果它(在控制台中)说它匹配,为什么它在浏览器中不匹配吗?
php app/console router:match /usuarios
是模拟路径信息匹配。也就是说,它所做的只是找出匹配的路由器模式。
如果你想用http://localhost:7080/usuarios
访问这个资源,你必须为你的路由器提供一个默认格式选项。如:
get_usuarios:
type: rest
resource: MciAPIBundleControllerUsuariosController
defaults: { _format: json }
好了,我找到答案了。
问题是我的web服务器配置。我正在使用一个新的Arch linux安装-它使用apache 2.4。
对于apache 2.4,需要对php的默认参数进行一些更改,以便能够与apache一起运行它。实际上,我现在必须将它配置为使用php-fpm并通过fcgi加载apache。
因此,我必须在虚拟主机配置文件中放入正确的ProxyPass指令,以便一切正常工作:
DocumentRoot /home/user/project/web
ProxyPassMatch ^/(.*)$ fcgi://127.0.0.1:9000/home/user/project/web/app.php/$1
值得注意的是ProxyPassMatch
指令需要匹配所有的url,而不仅仅是php文件,因为我使用的url没有php结束的API(例如http://localhost:7080/usuarios
)