1

The directory structure is

AppBundle/api/CartController

There is a function defined in CartController.php as onlinecoupontrancation($params).

I need to call this function from AppBundle/api/MerchantController.php. I tried

$this->forward('AppBundle:api/Cart:onlinecoupontrancation', $param);

But it is giving error 404. Any clue for this? Thanks in advance.

1 Answer 1

3

If it is a shared function between two controllers, why not create a shared service class that both controllers use?

For example, 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;
    }
}

Create this as a service, instructions here Something like;

app/config/services.yml

services:
    cart.functions:
        class:        AppBundle\CartFunctions
        arguments:    []

So in your controllers;

public function cartAction($params)
{
    // other stuff ...
    $cartFunctions = $this->get('cart.functions');
    $foo = $cartFunctions->onlinecoupontrancation($params);
}
Sign up to request clarification or add additional context in comments.

2 Comments

onlinecoupontrancation function is using other cartcontroller function.
@NituDhaka, I would put all functions relations to cart in the service class, then call functions from this class only. If the other function your are refering too is only for the cart functions then put that in the class, else return the data you need and keep processing as required

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.