在我的应用中,我有两个不同的BundlesBaseBundle
和UserBundle
。当我处于的其中一个控制器中时UserBundle
,如何访问其中的可用功能BaseBundle
?
我正在UserBundle
尝试做类似的事情:
$property['x'] = $this->calculateNumber(array($propertyX->indexX, $propertyY->indexY));
这是我得到的错误:
Attempted to call method "calculateNumber" on class "Example\UserBundle\Controller\DefaultController".
500 Internal Server Error - UndefinedMethodException
这就是Symfonynamespaces
派上用场的地方。因此,当您进入时UserBundle
,只需导入包含您要调用的方法的类:
# UserBundle/Controller/UserController.php
use BaseBundle\Controller\DefaultController;
//...
class UserController extends Controller
{
/**
* @Route("/whatever", name="whatever")
*/
public function whatever()
{
$base = new DefaultController(); //instantiate the class containing the desired method
$property['x'] = $base->calculateNumber(array($propertyX->indexX, $propertyY->indexY)); //call the calculateNumber method
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句