문제

I have a simple and perhaps stupid question.

Using Symfony2 PHP framework i often work extending controllers like below (of course it depends from the kind of work):

    class MainController extends Controller{
        private $locale = array();

        protected function Locale() {
        $em = $this->getDoctrine()
            ->getManager();

                $this->locale = $em->getRepository('CommonLanguageBundle:Language')
            ->findBy(
                array('code' => $this->getRequest()
                    ->getLocale()
                )
            );
//      \Doctrine\Common\Util\Debug::dump($this->locale);
        return $this->locale[0];
    }
     //..
    }
    class StoreController extends MainController{
     function a_method() {
       $data = $this->Locale()->getId();
       //...
      }
    }
    class DefaultController extends StoreController {
     $data = $this->Locale()->getId();
     //...
    }

Is this a good practice?

Surfing on the web i found many articles but it isn't still so clear for me.

In the end, if it worked fine in Symfony2, would it be good in general for MVC pattern?

도움이 되었습니까?

해결책

Symfony is not MVC framework. Symfony is Service-oriented architecture framework. Generally cascading extending controllers doesn't make a sens.

Rather you should create services and use it in whe you need it.

Moreover, good practice is define Controller as service.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top