简体   繁体   English

Symfony控制器:重定向,路由还是其他?

[英]Symfony controller: redirect, route or something else?

So I'm building a Symfony web app. 因此,我正在构建一个Symfony Web应用程序。

I have a simple controller ( DefaultController.php ) as follows: 我有一个简单的控制器( DefaultController.php ),如下所示:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class DefaultController extends Controller
{
  /**
  * @Route("/", name="default")
  */
  public function defaultAction(){
    return $this->render('default/hello.html.twig', array(
      'name' => "hello"
    ));
  }
}

Nothing special. 没什么特别的。

Now, I would like to have a separate .php file called APIController.php that gets called when a user navigates to http://eamorr.com/api/ 现在,我想有一个名为APIController.php的单独的.php文件,当用户导航到http://eamorr.com/api/时会被调用

APIController.php would then handle requests such as: 然后, APIController.php将处理以下请求:

Here's what APIController.php should look like: 这是APIController.php样子:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class APIController extends Controller
{
    /**
     * @Route("/getUser", name="getUser")
     */
    public function getUser(){
        //
    }

    /**
     * @Route("/addUser", name="addUser")
     */
    public function addUser(){
        //
    }

    /**
     * @Route("/getAllUsers", name="getAllUsers")
     */
    public function getAllUsers(){
        //
    }

    //etc.


}

From an architectural point of view, am I doing this right? 从体系结构的角度来看,我这样做正确吗? Does anyone have any recommendations as to how to do this in Symfony? 有没有人对在Symfony中执行此操作有任何建议?

You are on the right track. 您走在正确的轨道上。 But if you are looking to support multiple request and response formats (eg. JSON, XML) you are better off using FOSRestBundle . 但是,如果您希望支持多种请求和响应格式(例如JSON,XML),则最好使用FOSRestBundle

It can handle content negotiation, entity serialization (using JMSSerializerBundle which is used by the FOSRest Bundle) and it let's you build RESTful routes. 它可以处理内容协商,(使用实体系列化JMSSerializerBundle这是由FOSRest捆绑使用),它让你构建RESTful路线。

If you define routes like: 如果您定义以下路线:

/**
 * @Route("/getUser", name="getUser")
 */
public function getUser(){
    //
}

Then the URL to this action will be http://eamorr.com/getUser . 然后,此操作的URL将为http://eamorr.com/getUser As you can see there's no /api part, and this is because you didn't mention it anywhere. 如您所见,没有/api部分,这是因为您在任何地方都没有提到它。

You have two solutions for this case. 对于这种情况,您有两种解决方案。

First it to define full routes like 首先定义完整的路线,例如

/**
 * @Route("/api/getUser", name="getUser")
 */
public function getUser(){
    //
}

Second: since you want all APIController actions to have this /api part, you can define a prefix for all routes by defining "base" route for whole class. 第二:由于您希望所有APIController操作都具有/api部分,因此可以通过为整个类定义“基本”路由来为所有路由定义前缀。

/**
 * @Route("/api", name="getUser")
 */
 class APIController extends Controller

Then you can leave your actions' routes like they are. 然后,您可以照原样保留操作的路线。

More info: http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html#route-prefix 更多信息: http : //symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html#route-prefix

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

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