简体   繁体   English

Symfony,为移动应用程序,桌面应用程序和网站实现Web服务器

[英]Symfony, implement webserver for mobile app, desktop app, and website

I already have a mobile app that retrieve some data from database, show it to the user by php scripts that execute querys on a database, and return a json with results to my app. 我已经有一个移动应用程序,它可以从数据库中检索一些数据,通过对数据库执行查询的php脚本将其显示给用户,然后将包含结果的json返回给我的应用程序。

I also have a desktop app that use other php scripts to retrieve data on same database, and next step is to implement a website too. 我也有一个桌面应用程序,该应用程序使用其他php脚本来检索同一数据库上的数据,下一步就是实现一个网站。

Now I think that having a script for each operation that my app support is not the best practice, so I've decided to use Symfony framework for organize my server scripts better. 现在,我认为应用程序支持的每个操作都没有一个脚本是最佳实践,因此,我决定使用Symfony框架更好地组织服务器脚本。

For now I've created a new PHP Symfony project, replicated my database on localhost, generated a bundle for db operations, mapped sql tables, generated entities and generated a crud controller for each entity. 现在,我已经创建了一个新的PHP Symfony项目,在本地主机上复制了数据库,生成了用于数据库操作的捆绑包,映射了sql表,生成了实体,并为每个实体生成了一个crud控制器。

Now I don't know how to go ahead. 现在我不知道该怎么做。

How can I make my project return only json messages for each operation (and not an html page with result as happen by default)? 如何使我的项目为每个操作仅返回json消息(而不是返回默认情况下出现结果的html页面)?

You must return the json string at the end of an action! 您必须在操作结束时返回json字符串!

eg 例如

public function ajaxAction(Request $request){

    //get some data from doctrine, do other stuff

    //generate a json string from it and return it

    $json = json_encode($data);
    $response = new Response($json, 200);
    $response->headers->set('Content-Type', 'application/json');
    return $response;
}

Now your Route for that action only return the json string! 现在,该操作的Route仅返回json字符串!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM