9

コントローラーでアノテーション(https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Controller/Annotations/)を配置、取得、投稿、削除する人はいますか?

私はそれをこのように使おうとしていますが、それでもgetメソッドが必要です。FOSRestBundleのこれらの注釈の目的は何ですか

/**
 * @Route("/get/{id}", defaults={"_format" = "json"})
 * @Post
 */
public function getObject($id) {    
    $object = $this->getService()->findById($id);
     return $object;
}
4

2 に答える 2

14

すべての注釈に関する情報を共有したい。

@Get、@Post、@Put、@Delete、@Head、@Patchは、@ Route + @Method のショートカットです。両方を使用する代わりに、いずれかを指定できます。例:

    /**
     * @Get("/hello/{id}")
     * 
     */
    public function helloAction($id)
    {
        return array();
    }

@Viewに関する情報はドキュメントにあります: https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/3-listener-support.md

@View //Guess template name
@View("AcmeHelloBundle::layout.html.twig") //Load Resources/views/layout.html.twig
@View("AcmeHelloBundle::layout.html.twig", templateVar="test") // if returned data doesn't 
    // have a key (e.g. return array("string", 5) instead of default variable 'data', 
    // it's placed inside 'test' variable inside template.
@View(statusCode=204) // set HTTP header's status code

名前のプレフィックスは、routing.yml ファイルまたは注釈として追加できます。また、文書化されています - https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/6-automatic-route-generation_multiple-restful-controllers.md :

場合によっては、ルートの自動命名によってルート名の競合が発生することがあるため、RestBundle ルート コレクションは name_prefix (xml/yml の名前プレフィックスおよび注釈の @NamePrefix) パラメーターを提供します。

  #src/Acme/HelloBundle/Resources/config/users_routes.yml comments:
     type:         rest
     resource:     "@AcmeHelloBundle\Controller\CommentsController"
     name_prefix:  api_

この構成では、ルート名は次のようになります: api_vote_user_comment

@Prefixは、親リソースがあり、子リソースの前にプレフィックスを追加する必要がある場合に特に便利です。例:

親:

class UsersController extends Controller
{
    public function getUserAction($slug)
    {} // "get_user"   [GET] /users/{slug}
}

子:

class CommentsController extends Controller
{
    public function getCommentAction($slug, $id)
    {} // "get_user_comment"    [GET] 
}

アクション getCommentAction は/users/{slug}/comments/{id}パスに対応するようになりました。

@Prefix("some_prefix") を使用すると、生成されたパスは /users/{slug}/ some_prefix /comments/{id}になります

また、@NoRouteメソッドレベルのアノテーションを使用することで、ルートは生成されません。

于 2012-03-29T13:51:56.407 に答える
2

ID をルートに入れるべきではありません (これは get と同等であるため)。代わりに、これを実行して id パラメータを強制的に $_POST 経由で送信する必要があります

/**
* @Route("/get", defaults={"_format" = "json"})
 * @Post
 */
public function getObject() {  
    $id = $this->Request::createFromGlobals()->request->get('id');
    $object = $this->getService()->findById($id);
    return $object;
}
于 2011-12-20T22:16:22.903 に答える