Commit 971d4bb1 authored by Mat's avatar Mat

Introduction aux services dans symfony

parent 97d34921
......@@ -33,3 +33,13 @@ services:
# AppBundle\Service\ExampleService:
# arguments:
# $someArgument: 'some_value'
TechnoBundle\:
resource: '../../src/TechnoBundle/*'
exclude: '../../src/TechnoBundle/{Entity,Repository,Tests}'
TechnoBundle\Services\MonService:
arguments:
$userManager: '@fos_user.user_manager'
......@@ -2,7 +2,7 @@
namespace TechnoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use TechnoBundle\Services\MonService;
class DefaultController extends MasterController
{
......@@ -28,12 +28,18 @@ class DefaultController extends MasterController
/**
* @param $service
* @param $name
* @return \Symfony\Component\HttpFoundation\Response
*/
public function helloAction($name)
public function helloAction(MonService $service, $name)
{
return $this->render('@Techno/Default/hello.html.twig', array('name' => $name));
return $this->render('@Techno/Default/hello.html.twig', array(
'name' => $name,
'randomNumber' => $service->randomNumber(),
'users' => $service->getFOSUsers(),
'messages' => $service->getRandomMessage(),
));
}
......
......@@ -51,6 +51,13 @@
{% endfor %}
</table>
{{ dump() }}
<h3>Service</h3>
{{ randomNumber }}<br>
{% for user in users %}
{{ user.username }}
<br>
{% endfor %}
{{ messages }}
{% endblock %}
<?php
/**
* Created by PhpStorm.
* User: mat
* Date: 28/05/18
* Time: 9:37
*/
namespace TechnoBundle\Services;
use FOS\UserBundle\Model\UserManager;
class MonService
{
private $fos_users;
public function getRandomMessage()
{
$messages = array(
'message 1',
'message 2',
'message 3',
'message 4',
'message 5',
'message 6',
);
$index = array_rand($messages);
return $messages[$index];
}
public function __construct(UserManager $userManager)
{
return $this->fos_users = $userManager;
}
public function getFOSUsers()
{
return $this->fos_users->findUsers();
}
public function randomNumber()
{
return rand(0,100);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment