2

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?

4

1 回答 1

4

Symfony不是MVC 框架。Symfony 是面向服务的架构框架。通常级联扩展控制器没有意义。

相反,您应该创建服务并在需要时使用它。

此外,良好的做法是将 Controller 定义为 service

于 2014-02-11T12:29:56.563 回答