是扩展控制器的良好实践吗? [英] Symfony2 and MVC - Is extend controller a good practice?
问题描述
我有一个简单的,也许是愚蠢的问题。
使用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是面向服务的架构框架。一般来说,级联扩展控制器不会产生感觉。
而是应该创建服务,并在需要时使用。
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屋!