2016-01-13 9 views
5

मुझे symfony2 से twig में एक इकाई तक पहुंचने की आवश्यकता है। controler के अंदर, मैं के रूप में कुछ कर सकते हैं:विधि रीडायरेक्ट ToRoute() में रेंडर() जैसे तर्क हो सकते हैं?

return $this->render('frontendBundle::carrodecompras.html.twig', array(
     'entity' => $entity 
)); 

और फिर टहनी में मैं entity.name और इस तरह के साथ इकाई गुण पहुँच सकते हैं।

मैं इसी कार्य को पूरा करने की जरूरत है, लेकिन समारोह के साथ redirectToRoute()

return $this->redirectToRoute('frontend_carrodecompras', array(
     'entity' => $entity, 
)); 

लेकिन यह काम करने के लिए प्रतीत नहीं होता।

मैं निम्न त्रुटि हो रही है:

चर "इकाई" लाइन पर carrodecompras.html.twig frontendBundle में मौजूद नहीं है :: 32

संपादित करें: मैं Symfony 2.7

उपयोग कर रहा हूँ

चर $ इकाई मौजूद है (यह वास्तव में एप्लिकेशन मैं सरलीकरण के लिए $ इकाई उपयोग कर रहा था में $ Cortina कहा जाता है), बस redirectToRoute समारोह से पहले मैं इस परीक्षण करने के लिए था कि यह

echo "<pre>"; 
var_dump($cortina); 
echo "</pre>"; 

return $this->redirectToRoute('frontend_carrodecompras', array(
       'cortina' => $cortina, 
       )); 

और परिणाम यह है:

object(dexter\backendBundle\Entity\cortina)#373 (16) { 
    ["id":"dexter\backendBundle\Entity\cortina":private]=> 
    int(3) 
    ... 

इस टहनी कोड है:

<tr> 
    {% set imagentela = "img/telas/" ~ cortina.codInterno ~ ".jpg" %} 
    <td><img src="{{ asset(imagentela | lower) }}" alt="" width="25" height="25"> 
    </td> 
    <td>{{ cortina.nombre }}</td> 
    <td>{{ "$" ~ cortina.precio|number_format('0',',','.') }}</td> 
</tr> 
+0

आपको पुष्टि करनी चाहिए और सुनिश्चित करना चाहिए कि '$ इकाई' वास्तव में मौजूद है। – jbafford

+0

रीडायरेक्ट ToRoute विधि कहने से पहले डेटाबेस को इकाई पर रखा जा रहा है, इसलिए $ इकाई मौजूद है! – enlego

+0

मुझे संदेह है कि '$ entity' वास्तव में मौजूद है जहां आप' redirectToRoute 'को कॉल कर रहे हैं। क्या आप अतिरिक्त संदर्भ प्रदान कर सकते हैं? शायद यह फ़ंक्शन रीडायरेक्ट को कॉल कर रहा है अगर यह बहुत बड़ा नहीं है? या कम से कम कोड पथ दिखा रहा है, जहां से '$ entity' को परिभाषित किया जा रहा है जहां इसका उपयोग किया जा रहा है? – jbafford

उत्तर

11

जब आप एक नियंत्रक से redirectToRoute($route, array $parameters) फोन, $parameters यूआरएल टोकन ध्यान में रखते हुए प्रस्तुत करना चर उत्पन्न करने के लिए, नहीं प्रयोग किया जाता है , यह उस मार्ग पर असाइन किए गए नियंत्रक द्वारा किया जाता है जिस पर आप रीडायरेक्ट कर रहे हैं।

उदाहरण:

class FirstController extends Controller 
{ 
    /** 
    * @Route('/some_path') 
    */ 
    public function someAction() 
    { 
     // ... some logic 
     $entity = 'some_value'; 

     return $this->redirectToRoute('some_other_route', array('entity' => $entity)); // cast $entity to string 
    } 
} 

class SecondController extends Controller 
{ 
    /** 
    * @Route('/some_other_path/{entity}', name="some_other_route") 
    */ 
    public function otherAction($entity) 
    { 
     // some other logic 
     // in this case $entity equals 'some_value' 

     $real_entity = $this->get('some_service')->get($entity); 

     return $this->render('view', array('twig_entity' => $real_entity)); 
    } 
} 
+0

इस समाधान ने समस्या हल की। मैं दूसरे एक्शन में $ इकाई वैरिएबल पास नहीं कर रहा था (जिसे मैं मूल रूप से रीडायरेक्ट कर रहा था)। – enlego

1

$this->redirectToRoute('something', array('id' => 1)$this->redirect($this->generateUrl('something', array('id' => 1))) करने के लिए एक सुविधा आवरण है। यह आपके पैराम के साथ एक यूआरएल बनाता है और पैराम के मान को स्ट्रिंग या संख्या होने की उम्मीद कर रहा है।

http://symfony.com/blog/new-in-symfony-2-6-new-shortcut-methods-for-controllers

आप या तो संस्था की आईडी पारित करने के लिए फिर नए कार्रवाई में डेटा लाने या यह टूट डेटा के अलग-अलग टुकड़ों में से पहले यह redirectToRoute() कॉल हिट की जरूरत है।

class MyController extends Controller { 
    public function myAction(){ 
     $cortina = new Cortina(); 
     $cortina->something = "Some text"; 

     $em = $this->getDoctrine()->getManager(); 
     $em->persist($cortina); 
     $em->flush(); 

     return $this->redirectToRoute('frontend_carrodecompras', array(
      'id' => $cortina->getId() 
     ); 
    } 
} 
संबंधित मुद्दे