Home  >  Article  >  Backend Development  >  laravel如何更具后缀名.json,.xml返回对应的格式?

laravel如何更具后缀名.json,.xml返回对应的格式?

ringa_lee
ringa_leeOriginal
2018-05-14 10:58:461274browse

laravel如何更具后缀名.json,.xml返回对应的格式?

准确的说如何优雅的实现?
我用这个方式感觉太丑了(这个是dingoAPI的route)

回复内容:

laravel如何更具后缀名.json,.xml返回对应的格式?

准确的说如何优雅的实现?
我用这个方式感觉太丑了(这个是dingoAPI的route)

没用过dingo,但在restful风格中,返回格式应该是通过Accept头来磋商的

GET /users/1
Accept: application/json

==>

200 OK
Content-Type: application/json

{"name":"foo"}
GET /users/1
Accept: application/xml

==>

200 OK
Content-Type: application/xml

<response><name>foo</name></response>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn