cnerta/behind-a-proxy-bundle
Installation:
composer require cnerta/behind-a-proxy-bundle:1.0.*
Add to AppKernel.php:
new Cnerta\BehindAProxyBundle\CnertaBehindAProxyBundle(),
Basic Configuration (config/packages/cnerta_behind_a_proxy.yaml):
cnerta_behind_a_proxy:
enabled: true
proxy_host: "your-proxy-host"
proxy_port: 8080
proxy_username: "optional-user"
proxy_password: "optional-pass"
First Use Case:
Enable the bundle and configure proxy settings. Test with a curl request in a controller:
use Symfony\Component\HttpFoundation\Request;
public function testProxy(Request $request) {
$response = $request->getClient()->request('GET', 'http://example.com');
return new Response($response->getContent());
}
Global Proxy Configuration:
config/packages/cnerta_behind_a_proxy.yaml.enabled to activate/deactivate globally.Context-Specific Overrides:
Use the bundle’s ProxyContext service to override settings per request:
$context = $this->get('cnerta_behind_a_proxy.context');
$context->setProxyHost('custom-proxy.example.com');
Integration with HTTP Clients:
ProxyClient or inject the ProxyContext:
$client = new Client([
'handler' => HandlerStack::create($this->get('cnerta_behind_a_proxy.handler')),
]);
SoapClient Integration:
Pass the bundle’s ProxyContext to SoapClient:
$context = $this->get('cnerta_behind_a_proxy.context');
$soapClient = new SoapClient($wsdl, [
'stream_context' => $context->getStreamContext(),
]);
Stream Context for Raw PHP:
Use the ProxyContext to generate stream contexts for file_get_contents or stream_context_create:
$context = $this->get('cnerta_behind_a_proxy.context');
file_get_contents('http://example.com', false, $context->getStreamContext());
Dynamic Proxy Routing: Use dependency injection to conditionally enable proxies:
services:
App\Service\DynamicProxyService:
arguments:
$proxyContext: '@cnerta_behind_a_proxy.context'
Proxy-Aware Middleware: Create middleware to log or modify proxy behavior:
public function handle(Request $request, Closure $next) {
$this->get('cnerta_behind_a_proxy.context')->logProxyUsage();
return $next($request);
}
Environment-Specific Configs:
Override proxy settings per environment (e.g., config/packages/dev/cnerta_behind_a_proxy.yaml).
Deprecation Warnings:
host_ssl is removed in v2.0.0 (use proxy_host + proxy_port with https://).1.0.* for older versions.Configuration Overrides:
proxy_host, not http_proxy).enabled: true (not Enabled: true).Stream Context Conflicts:
file_get_contents with custom stream options, merge contexts manually:
$proxyContext = $this->get('cnerta_behind_a_proxy.context')->getStreamContext();
$customOptions = ['http' => ['header' => "User-Agent: MyApp\r\n"]];
$mergedContext = stream_context_create($proxyContext, $customOptions);
SoapClient Quirks:
SoapClient::setUseCurl():
$soapClient = new SoapClient($wsdl, [
'stream_context' => $context->getStreamContext(),
'use_curl' => true,
]);
Debugging:
config/packages/dev/monolog.yaml to log proxy requests:
handlers:
proxy_log:
type: stream
path: "%kernel.logs_dir%/proxy.log"
level: debug
Testing:
ProxyContext in PHPUnit:
$this->get('cnerta_behind_a_proxy.context')->setProxyHost('test-proxy');
Performance:
enabled: false) to avoid overhead.Extending:
ProxyContext service to add custom logic:
services:
cnerta_behind_a_proxy.context:
class: App\Service\CustomProxyContext
parent: cnerta_behind_a_proxy.context
Fallbacks:
$context = $this->get('cnerta_behind_a_proxy.context');
if (!$context->isProxyConfigured()) {
$context->setProxyHost(config('app.fallback_proxy'));
}
Security:
parameter_bag:
parameters:
proxy_password: '%env(APP_PROXY_PASSWORD)%'
How can I help you explore Laravel packages today?