2012-05-16 29 views
7

में एक सेवा से रीडायरेक्ट करें मेरे पास एक ऐसी सेवा है जो किसी पृष्ठ के लिए डेटा देखती है, लेकिन यदि वह डेटा नहीं मिला है, तो उसे होमपेज पर रीडायरेक्ट करना चाहिए। मेरे जीवन के लिए, मैं यह नहीं समझ सकता कि एसएफ 2 में इसे कैसे किया जाए। सेवाओं और राउटर के साथ काम करने के कई अलग-अलग तरीके हैं, लेकिन कोई भी काम नहीं कर रहा है।सिम्फनी 2

namespace Acme\SomeBundle\Services; 

use Acme\SomeBundle\Entity\Node; 
use \Doctrine\ORM\EntityManager; 
use \Symfony\Component\HttpKernel\Event\GetResponseEvent; 
use \Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 
use \Symfony\Bundle\FrameworkBundle\Routing\Router; 
use \Symfony\Component\Routing\Generator\UrlGenerator; 
use Symfony\Component\HttpFoundation\RedirectResponse; 

class NodeFinder 
{ 

    private $em; 
    private $router; 

    public function __construct(EntityManager $em, Router $router) 
    { 

     $this->em = $em; 
     $this->router = $router; 

    } 

    public function getNode($slug) 
    { 

     $node = $this->em->getRepository('SomeBundle:Node')->findOneBy(array('slug' => $slug)); 

     if (!$node) { //if no node found 

       return $this->router->redirect('homepage', array(), true); 
     } 
} 

उत्तर

6

सिम्फनी 2 में, सेवाओं को पुनर्निर्देशन के लिए नहीं बनाया जाता है। आप में नियंत्रक आप अपनी सेवा को कॉल तो

namespace Acme\SomeBundle\Services; 

use Acme\SomeBundle\Entity\Node; 
use \Doctrine\ORM\EntityManager; 

class NodeFinder 
{ 

    private $em; 

    public function __construct(EntityManager $em) 
    { 

     $this->em = $em; 

    } 

    public function getNode($slug) 
    { 

     $node = $this->em->getRepository('SomeBundle:Node')->findOneBy(array('slug'=>$slug)); 

     return ($node)?true:false; 
} 

और पुनर्निर्देशन बनाने:: आप इस तरह आपकी सेवा को बदलने के लिए प्रयास करना चाहिए

//in the controller file 

$nodefinder = $this->container->get('your_node_finder_service_name'); 

if(!$nodefinder->getNode($slug)){ 

    $this->redirect('homepage'); 
} 
+2

धन्यवाद। समस्या यह है कि मैं इस सेवा का उपयोग कई स्थानों पर करता हूं, इसलिए नियंत्रक में रीडायरेक्ट करने के लिए बहुत सारे कोड डुप्लिकेशन हैं। – Acyra

+0

स्लीली सही है, आपको अपने कंट्रोलर में कोई रीडायरेक्ट करना चाहिए, न कि आपकी सेवा में। –

+3

@ChrisMcKinnel क्योंकि? मेरे पास कई बार 'यदि उपयोगकर्ता लॉग इन नहीं है, तो लॉगिन पेज पर रीडायरेक्ट करें' वास्तव में? मुझे यह 100 बार डुप्लिकेट करना है? बुरा बुरा बुरा – Toskan

4

आप अपनी सेवा में ऐसा कर सकता है (मेरे सिर से बाहर लेखन)

class MyException extends \Exception{ 
public $redirectResponse; //is a \Symfony\Component\HttpFoundation\RedirectResponse 
} 
class MyService { 

public function doStuff(){ 
if ($errorSituation){ 
    $me = new MyException() 
    $me->redirectResponse = $this->redirect($this->generateUrl('loginpage')); 
    throw $me; 
} 
} 

} 

class MyController extends Controller{ 

public function doAction(){ 
try{ 
    //call myservice here 
}catch (MyException e){ 
    return $e->redirectResponse; 
} 
} 

इस हालांकि, सही नहीं है यह निश्चित रूप से एक बहुत sllly क्या

1
करने के लिए कोशिश कर रहा था की तुलना में बेहतर है

सिम्फनी बिंदु से आप एक नियंत्रक को सेवा के रूप में बना सकते हैं और इसलिए इस सेवा से पुनर्निर्देशन कर सकते हैं। वाक्य रचना है:

use Symfony\Component\HttpFoundation\RedirectResponse; 

return new RedirectResponse($url, $status); 

अधिक जानकारी यहां पाया जा सकता: http://symfony.com/doc/current/cookbook/controller/service.html#alternatives-to-base-controller-methods

2

आपकी सेवा में रूटर सेवा सम्मिलित करें। आप नया रीडायरेक्ट रीस्पॉन्स वापस कर सकते हैं। here देखें।