Sending data to frontend library classes in Symfony 1.4

Help with passing info to frontend/lib classes in Symfony 1.4

I’m having trouble figuring out how to send data to a class in the frontend/lib folder of my Symfony 1.4 project. I can’t use $this->getUser inside the class because it’s outside the Symfony framework. So I can’t save stuff in the session.

I’ve tried different ways to pass in the data, but I’m not sure how it’s supposed to work in this case. Here’s a bit of the code I’m working with:

function sendFormData($campaign, $affiliate, $data = [], &$result)
{
    $data['user_ip'] = $_SERVER['REMOTE_ADDR'];
    if (isset($custom_ip)) // need to pass this in somehow
    {
        $data['user_ip'] = $custom_ip;
    }
    $data['timestamp'] = date('Y-m-d H:i:s');

    $result = $this->apiClient->post($this->endpoint . 'submit/' . $campaign . '/' . $affiliate . '/', $data)->getResponse();
    return (strpos($result, '<SUCCESS>1</SUCCESS>') !== false);
}

This class is used by an action in a frontend module too. I know it’s probably a simple fix, but I’m not sure where to look. Any ideas on how to pass in that $custom_ip variable?

Thanks for any help! Sorry if this is a basic question. I’m still learning some parts of PHP and Symfony.

One approach you could consider is dependency injection. Instead of trying to access $this->getUser() directly in your class, you could pass the necessary data as constructor arguments or method parameters.

For example, you might refactor your class to accept the custom IP in the constructor:

class YourClass {
    private $customIp;

    public function __construct($customIp = null) {
        $this->customIp = $customIp;
    }

    public function sendFormData($campaign, $affiliate, $data = [], &$result) {
        // ... existing code ...
        if ($this->customIp) {
            $data['user_ip'] = $this->customIp;
        }
        // ... rest of the method ...
    }
}

Then in your action, you could instantiate the class with the custom IP:

$customIp = $this->getUser()->getIpAddress(); // or however you get it
$instance = new YourClass($customIp);
$instance->sendFormData($campaign, $affiliate, $data, $result);

This approach keeps your library class decoupled from Symfony while still allowing you to pass in the necessary data.

yo, have u tried using a factory pattern? u could create a factory class in ur symfony app that instantiates ur lib class and passes the custom_ip. then in ur action, u just call the factory method. it keeps things clean and separates the concerns. lemme kno if u want more details on how to set that up!

hey there! interesting problem you’ve got. have you tried passing the custom_ip as a parameter to the sendFormData function? something like:

sendFormData($campaign, $affiliate, $data, $result, $custom_ip)

then you could use it directly in the function. just a thought! What other approaches have u tried so far? curious to hear more about your setup!