2016-06-30 64 views
3

我在Symfony上是全新的,因此我遵循教程,現在正在尋找解決問題的答案。致電服務時出現錯誤101

我創建了一個服務。但是當我在我的控制器中調用它時,Chrome會說:ERR_CONNECTION_RESET。 當我刪除調用它的行時,它可以正常工作。

這裏是我的控制器代碼:

<?php 

namespace CoreBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 

class DefaultController extends Controller { 

    public function indexAction() { 
     $listAdverts = $this->$container->get('databaseinfos.listannonces'); /*DOES NOT WORK*/ 
return $this->render('CoreBundle:Default:index.html.twig',array('listAdverts'=>$listAdv‌​erts)); 
    } 

} 

我services.yml文件的代碼:

services: 
    databaseinfos.listannonces: 
     class: CoreBundle\DatabaseInfos 

我的服務代碼:

<?php 

    namespace CoreBundle\DatabaseInfos; 

    class DatabaseInfos 
    { 
     public function getList(){ 
      // Notre liste d'annonce en dur 
      $listAdverts = array(
      array(
      'title' => 'Recherche développpeur Symfony', 
      'id'  => 1, 
      'author' => 'Alexandre', 
      'content' => 'Nous recherchons un développeur Symfony débutant sur Lyon. Blabla…', 
      'date' => new \Datetime()), 
      array(
      'title' => 'Mission de webmaster', 
      'id'  => 2, 
      'author' => 'Hugo', 
      'content' => 'Nous recherchons un webmaster capable de maintenir notre site internet. Blabla…', 
      'date' => new \Datetime()), 
      array(
      'title' => 'Offre de stage webdesigner', 
      'id'  => 3, 
      'author' => 'Mathieu', 
      'content' => 'Nous proposons un poste pour webdesigner. Blabla…', 
      'date' => new \Datetime()) 
      ); 

      return $listAdverts; 
     } 
    } 

這裏控制器稱爲模板:

{% extends "CoreBundle::layout.html.twig" %} 

{% block title %} 
    Accueil principale - {{ parent() }} 
{% endblock %} 

{% block body %} 

<h1>Page d'accueil du super site d'annonces !</h1> 
<ul> 
    {% for advert in listAdverts %} 
     <li> 
     <a href="{{ path('oc_platform_view', {'id': advert.id}) }}"> 
      {{ advert.title }} 
     </a> 
     par {{ advert.author }}, 
     le {{ advert.date|date('d/m/Y') }} 
     </li> 
    {% else %} 
     <li>Pas (encore !) d'annonces</li> 
    {% endfor %} 
    </ul> 

{% endblock %} 

感謝您的時間和答案! 有一個好的一天=)

+1

那麼,如果你已經找到了這行代碼導致的問題,爲什麼不一起分享一些額外的代碼也可以幫助我們理解發生了什麼?我們在這裏沒有精神。 – apokryfos

+0

在問題中添加了代碼,謝謝^^ –

+0

這似乎沒問題,渲染模板中是否有任何重定向?任何可能導致這種情況發生在無限循環中的事情? – apokryfos

回答

1

我發現它爲什麼沒有工作:

我在services.yml聲明:

services: 
    databaseinfos.listannonces: 
     class: CoreBundle\DatabaseInfos 

DatabaseInfos.php文件是CoreBundle/DatabaseInfos/DatabaseInfos.php

所以services.yml文件應該是這樣的:

和命令來獲取列表(DatabaseInfos.php類的方法getList())是:$this->container->get('databaseinfos')->getList()

相關問題