Step 1: Setting up the bundle

A) Download FOSRestBundle(下载FOSRestBundle)

Note:

注意:

This bundle recommends using JMSSerializer which is integrated into Symfony2 via JMSSerializerBundle.Please follow the instructions of the bundle to add it to your composer.json and how to set it up.If you do not add a dependency to JMSSerializerBundle, you will need to manually setup an alternativeservice and configure the Bundle to use it via the service section in the app config

本功能包建议使用 通过 JMSSerializerBundle 集成到Symfony2中的 JMSSerializer,请遵循功能包中的指示将其添加到您的 composer.json 中并设置它。如果您没有添加JMSSerializerBundle的依赖,您将需要手工安装并配置该功能包,以便在应用配置中通过service段来使用它。

Ultimately, the FOSRestBundle files should be downloaded to thevendor/bundles/FOS/RestBundle directory.

最终FOSRestBundle文件将被下载到 vendor/bundles/FOS/RestBundle 目录中。

This can be done in several ways, depending on your preference. The firstmethod is the standard Symfony2 method.

根据您的喜好,这可以通过多种方式来实现。第一种方式是标准的Symfony2方式:

Using the vendors script(使用vendors脚本)

Add the following lines in your deps file:

在您的deps文件中添加下列语句:

[FOSRest]
    git=git://github.com/FriendsOfSymfony/FOSRest.git
    target=fos/FOS/Rest
[FOSRestBundle]
    git=git://github.com/FriendsOfSymfony/FOSRestBundle.git
    target=bundles/FOS/RestBundle

Now, run the vendors script to download the bundle:

现在,运行vendors脚本来下载该功能包:

$ php bin/vendors install

Using submodules(使用子模块)

If you prefer instead to use git submodules, then run the following:

如果您喜欢使用git子模块来代替,您可以使用下列语句:

$ git submodule add git://github.com/FriendsOfSymfony/FOSRestBundle.git vendor/bundles/FOS/RestBundle
$ git submodule add git://github.com/FriendsOfSymfony/FOSRest.git vendor/fos/FOS/Rest
$ git submodule update --init

Using composer(使用composer)

Simply run assuming you have installed composer.phar or composer binary:

假设您已经安装了composer.phar 或 composer 命令,您只需要简单运行以下指令:

$ composer require friendsofsymfony/rest-bundle

B) Configure the Autoloader (not needed for composer)(配置Autoloader,composer不需要)

Add the FOS namespace to your autoloader:

在您的autoloader中添加FOS全称空间:

<?php
// app/autoload.php
$loader->registerNamespaces(array(
    // ...
    'FOS\\Rest' => __DIR__.'/../vendor/fos',
    'FOS'       => __DIR__.'/../vendor/bundles',
));

C) Enable the bundle(启动功能包)

Finally, enable the bundle in the kernel:

最后,在kernel中启动功能包:

<?php
// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        // ...
        new FOS\RestBundle\FOSRestBundle(),
        // if you installed FOSRestBundle using composer you shoudn't forget
        // also registering JMSSerializerBundle.
        // new JMS\SerializerBundle\JMSSerializerBundle(),
    );
}

That was it!(就是这样)

Check out the docs for information on how to use the bundle! Return to the index.

查看文档以获取如何使用该功能包的信息!返回使用指南