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'=>$listAdverts));
}
}
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 =)