Php Symfony2:调用另一个控制器的函数
目录结构是Php Symfony2:调用另一个控制器的函数,php,symfony,Php,Symfony,目录结构是 AppBundle/api/CartController 在CartController.php中有一个函数定义为onlinecoupontrance($params) 我需要从AppBundle/api/MerchantController.php调用此函数。 我试过了 但它给出了错误404。有线索吗?提前感谢。如果它是两个控制器之间的共享函数,为什么不创建两个控制器都使用的共享服务类呢 例如,CartFunctions.php <?php // src/AppBundle
AppBundle/api/CartController
在CartController.php
中有一个函数定义为onlinecoupontrance($params)
我需要从AppBundle/api/MerchantController.php
调用此函数。
我试过了
但它给出了错误404。有线索吗?提前感谢。如果它是两个控制器之间的共享函数,为什么不创建两个控制器都使用的共享服务类呢 例如,CartFunctions.php
<?php
// src/AppBundle/Classes/CartFunctions.php
namespace AppBundle\Classes\CartFunctions;
class CartFunctions
{
public function __construct()
{
// optional DI of other things if required
}
private function onlinecoupontrancation($params)
{
$foo = true;
// whatever you want to do
return $foo;
}
}
所以在你的控制器中
public function cartAction($params)
{
// other stuff ...
$cartFunctions = $this->get('cart.functions');
$foo = $cartFunctions->onlinecoupontrancation($params);
}
OnlineCoupOntraction函数正在使用其他cartcontroller函数。@NituDhaka,我会将所有与cart相关的函数放在服务类中,然后仅从该类调用函数。如果您引用的另一个函数也仅用于购物车函数,则将其放入类中,否则返回您需要的数据并根据需要进行处理
services:
cart.functions:
class: AppBundle\CartFunctions
arguments: []
public function cartAction($params)
{
// other stuff ...
$cartFunctions = $this->get('cart.functions');
$foo = $cartFunctions->onlinecoupontrancation($params);
}