Symfony 1.4: How to inject variables into frontend/lib classes

I’m working with Symfony 1.4 and need help with variable injection into classes located in the frontend/lib directory. I can’t access $this->getUser from within these classes since they exist outside the standard Symfony context, so session storage isn’t available.

Here’s my current implementation in apps/frontend/lib:

public function submitFormData( $project_id, $partner_id, $data = array(), &$result )
{
  $data[ 'client_ip' ] = $_SERVER[ 'REMOTE_ADDR' ];
  if ( isset( $custom_ip )) //need to pass this variable somehow
  {
    $data[ 'client_ip' ] = $custom_ip;
  }
  $data[ '__created_at' ] = date( 'Y-m-d H:i:s' );

  $result = $this->httpClient->post( $this->service_url . 'submit/' . $project_id . '/' . $partner_id . '/', $data )->getResponseText();
    return ( preg_match( '/(success)/', $result ) ) ? true : false;
}

This method gets called from an action in a frontend module. I’ve tried various approaches to pass the variable but I’m missing something fundamental about how data flows between these components. What’s the proper way to inject variables into these lib classes from the action layer?

just pass it as a parameter in your method call. Something like submitFormData($project_id, $partner_id, $data, &$result, $custom_ip = null) then check if it’s not null instead of using isset. That’s how i handle this stuff in my SF 1.4 projects.

interesting problem! have you tried using Symfony’s service container? you could register your lib class as a service and inject the user context directly. another option is creating a simple factory pattern in your action that builds the class with all the necessary data. what triggers the custom_ip scenario in your workflow?

Dependency injection works great here. Just add a constructor or setter method to your lib class that accepts the custom IP from your action. When you instantiate the class in your action, pass the data before calling submitFormData. I usually store these variables as class properties during initialization. Add something like private $customIp and set it through the constructor when creating the instance. This keeps your action layer and business logic separate while making sure data flows correctly.