2013-05-20 4 views
2

누군가이 주제에 대해 저를 도울 수 있기를 바랍니다.Symfony2 : 부모 렌더링의 변수 값을 변경하려면 어떻게해야합니까?

Symfony 2 구조에 두 개의 컨트롤러가 있는데, 두 번째 컨트롤러는 첫 번째 컨트롤러보다 우선합니다.

다음

부모 컨트롤러의 코드 :

<?php 

namespace WebSender\MainPageBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 

class DefaultController extends Controller 
{ 
    public function indexAction() 
    { 
     $Header_Title = "Titolo 1"; 
     $content_area = "Contenuto di prova 1!"; 
     $Header_Welcome = "Benvenuto utente: "; 
     $username = "nome utente"; 
     $Header_Logout = "Logout"; 

     return $this->render('WebSenderMainPageBundle:Default:index.html.php', array('Header_Title' => $Header_Title, 'content_area' => $content_area, 'Header_Welcome' => $Header_Welcome, 'username' => $username, 'Header_Logout' => $Header_Logout)); 
    } 
} 

그리고 두 번째 컨트롤러 : 당신이 볼 수 있듯이

<?php 

namespace WebSender\MainPageBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Response; 

use WebSender\MainPageBundle\Controller\DefaultController as BaseController; 

class GridController extends BaseController 
{ 
    public function indexAction() 
    { 

     $Header_Title = "Titolo 2"; 
     $content_area = "Contenuto di prova 2!"; 

     $response = parent::indexAction(); 

     // return $this->render('WebSenderMainPageBundle:Default:index.html.php', array('Header_Title' => $Header_Title, 'content_area' => $content_area, 'Header_Welcome' => $Header_Welcome, 'username' => $username, 'Header_Logout' => $Header_Logout)); 
     return $response; 
    } 
} 

, 나는 "특히, $ 응답 값을 변경하는 방법을 모른다 content_area "및"Header_Title ".

감사합니다.

+0

Symfony에서는 컨트롤러를 새로운 것으로 오버라이드 할 수 있습니다. 즉, 컨트롤러의 전체 또는 일부를 다른 컨트롤러로 "확장"할 수 있습니다. –

답변

0

간단한 답변 : 렌더링 메소드가 ready-to-serve html로 응답 객체를 반환하기 때문에 당신은 할 수 없습니다. 다른 말로하면, 렌더링 방법에서 다시 시작하면 나뭇 가지의 작업이 완료됩니다.

내가 게시 한 예가 실제 것이 아니고 단순화 된 사례이기 때문에 어떤 솔루션이 가장 적합한 지 말하기는 어렵습니다.

예를 들어, 두 작업이 제목과 바닥 글의 차이 만있는 동일한 페이지를 생성하는 경우 컨트롤러 상속 대신 나뭇 가지의 상속을 사용하고이 리터럴을 템플릿에 넣으십시오.

다른 한편으로는 - 이러한 것들이 로직에 의해 생성 된 경우 적절한 "로직"서비스를 사용하여 서비스를 제공하면 더 이상 컨트롤러 상속이 필요하지 않습니다.

제 대답으로는 충분하지 않을 경우 더 많은 것을 시도해보십시오. 더 자세한 방법을 알려 드리겠습니다.

+0

설명을 주셔서 감사합니다. 저는 논리를 "다시 생각"할 것입니다. 저는 변수의 논리 생성을 "강제 실행"하려고했습니다. 다시 한번 감사드립니다. –

0

나뭇 가지와 함께 템플릿 상속을 사용해야합니다.

이렇게하면 확장 컨트롤러에 대해 원하는 제목으로 블록을 정의 할 수 있습니다. Cyprian이 말했듯이이 논리를 옮겨야합니다.

+0

고마움, 나는 시도 할 것이다. –