是扩展控制器的良好实践吗? [英] Symfony2 and MVC - Is extend controller a good practice?

查看:211
本文介绍了是扩展控制器的良好实践吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的,也许是愚蠢的问题。



使用Symfony2 PHP框架我经常工作扩展控制器如下(工作):

  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];
}
// ..
}
类StoreController扩展MainController {
函数a_method(){
$ data = $ this-> Locale ) - > getId();
// ...
}
}
class DefaultController extends StoreController {
$ data = $ this-> Locale() - > getId
// ...
}

/ p>

在网上冲浪我发现很多文章,但对我来说还不太清楚。



解决方案

Symfony 不是 MVC框架。 Symfony是面向服务的架构框架。一般来说,级联扩展控制器不会产生感觉。



而是应该创建服务,并在需要时使用。



将Controller定义为服务


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.

这篇关于是扩展控制器的良好实践吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆