2013-04-17 29 views
6

Tôi đang cố gắng theo dõi Will Durand's tutorial về cách thiết lập một API REST tốt với Symfony2. Tuy nhiên tôi đã thất bại ngay từ đầu khi tôi nhận được lỗi này:Bộ điều khiển phải trả về một phản hồi, mảng được cung cấp

The controller must return a response (Array(welcome => Welcome to my API) given). 

Điều gì đó cơ bản phải sai với cấu hình cơ bản của tôi. Tôi đã thử các thiết lập khác nhau cho cấu hình fos_rest, nhưng configuration reference không cung cấp để rất hữu ích vì tôi không thực sự hiểu những gì các thiết lập duy nhất làm.

thiết lập của tôi:

//config.yml 
sensio_framework_extra: 
    view: 
     annotations: true 

fos_rest: ~ 

//Controller 
<?php 

namespace Acme\Bundle\ApiBundle\Controller; 

use FOS\RestBundle\Controller\Annotations as Rest; 

class DefaultController 
{ 
    /** 
    * @Rest\View 
    */ 
    public function indexAction() 
    { 
     return array(
      'welcome' => 'Welcome to my API' 
     ); 
    } 
} 

API của tôi nên quay lại oder XML JSON dựa trên chấp nhận tiêu đề. Sẽ không bao giờ có đầu ra html.

Trả lời

28

Tôi đã sửa nó! Các cấu hình cần nhìn như thế này:

sensio_framework_extra: 
    view: 
     annotations: false 

fos_rest: 
    view: 
     view_response_listener: true 
+1

Tôi nghĩ rằng, nó nên được bổ sung trong các tài liệu chính thức (xin vui lòng liên hệ với họ và trình) . Bởi vì nó không tồn tại ngày hôm nay. Chúc mừng! –

+1

Chỉ cần gửi thông báo lỗi liên quan từ nhật ký, để mọi người có thể tìm thấy thông báo này qua google: 'PHP message: PHP Fatal error: Uncaught exception 'RuntimeException' với thông điệp 'Bạn cần vô hiệu hóa chú thích xem trong SensioFrameworkExtraBundle khi sử dụng FOSRestBundle View Response listener . ' trong /home/jupiter/symfony/dimsym/vendor/friendsofsymfony/rest-bundle/FOS/RestBundle/DependencyInjection/Compiler/ConfigurationCheckPass.php: 27' –

4

tôi dành một ngày để tìm kiếm cấu hình làm việc:

sensio_framework_extra: 
    view: { annotations: false } 
    router: { annotations: true } 

fos_rest: 
    param_fetcher_listener: true 
    body_listener: true 
    format_listener: true 
    view: 
     view_response_listener: 'force' 
     formats: 
      xml: true 
      json : true 
     templating_formats: 
      html: true 
     force_redirects: 
      html: true 
     failed_validation: HTTP_BAD_REQUEST 
     default_engine: twig 
    routing_loader: 
     default_format: json 
Các vấn đề liên quan