douqiang3768 2016-06-30 13:19
浏览 68
已采纳

调用服务时出错101

I'm totally new on Symfony so I followed a tutorial and I'm now looking for an answer to my problem.

I created a service. But when I call it in my controller, Chrome says : ERR_CONNECTION_RESET. When I remove the line that calls it, it works without any problem.

Here's my code in the controller :

<?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)); 
    }

}

The code of my services.yml file :

services:
    databaseinfos.listannonces:
        class: CoreBundle\DatabaseInfos

And the code of my service :

<?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;
        }
    }   

Here the template called by the controller :

{% 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 %}

Thank you for your time and answers ! Have a good day =)

  • 写回答

1条回答 默认 最新

  • douxunzui1519 2016-07-01 09:58
    关注

    I found why it didn't work:

    I declared in my services.yml :

    services:
        databaseinfos.listannonces:
            class: CoreBundle\DatabaseInfos
    

    But the DatabaseInfos.php file is in CoreBundle/DatabaseInfos/DatabaseInfos.php

    So the services.yml file should be like this :

    services:
        databaseinfos.listannonces:
            class: CoreBundle\DatabaseInfos\DatabaseInfos
    

    And the command to get the list (the method getList() of the class in DatabaseInfos.php) is : $this->container->get('databaseinfos')->getList()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?