Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2:调用另一个控制器的函数_Php_Symfony - Fatal编程技术网

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);
}