웹 사이트의 여러 지점에 주석이 있다고 가정 해 봅시다. {{render_widget ('comments', { "object": object})}} 같은 것을 어떻게 만들 수 있습니까? 그러면 해당 객체에 대한 모든 주석이있는 양식과 목록이 렌더링됩니까?symfony PHP와 나뭇 가지로 재사용 가능한 위젯을 만드는 법
# src/Acme/HelloBundle/Resources/config/services.yml
parameters:
# ...
my_widget.class: Acme\HelloBundle\Service\Widget
services:
my_widget:
class: "%my_widget.class%"
arguments: ["@service_container"]
# scope: container can be omitted as it is the default
컨트롤러의 서비스를 사용 :
// src/Acme/HelloBundle/Service/Widget.php
namespace Acme\HelloBundle\Service;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Widget
{
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function getComments()
{
$request = $this->container->get('request'); // use service_container to access request, doctrine, twig, etc...
}
}
이 서비스를 선언 :
[서비스] (http://symfony.com/doc/current/book/service_container.html)를 사용하려고 시도했을 수 있습니다. –
그리고 나서이 서비스를 가지 템플릿에 전달하십시오. –
@Victor 제발, 예제 코드를주세요. :) – CappY