我是Symfony的新手,所以我遵循了一个教程,我现在正在寻找我的问题的答案。
创建了一个服务。但是当我在控制器中调用它时,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'=>$listAdverts));
}
}
我的服务代码。
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 %}
感谢您的时间和回答!祝你今天过得愉快!
我发现为什么它不起作用了:
我在我的services.yml
中声明:
services:
databaseinfos.listannonces:
class: CoreBundle'DatabaseInfos
但是DatabaseInfos.php
文件在CoreBundle/DatabaseInfos/DatabaseInfos.php
服务。Yml文件应该像这样:
services:
databaseinfos.listannonces:
class: CoreBundle'DatabaseInfos'DatabaseInfos
获取列表的命令(DatabaseInfos.php中类的方法getList()
)为:$this->container->get('databaseinfos')->getList()