composer require 3slab/vdm-version-bundle
config/routes.yaml (or routing.yml in older Laravel versions):
vdm_version:
resource: "@VdmVersionBundle/Resources/config/routing.yml"
prefix: /
config/packages/vdm_version.yaml (or add to config/vdm_version.php in Laravel):
vdm_version:
path: /version
versions:
app: '1.0.0'
api: 'v2.3'
/version to get a JSON response:
{"app":"1.0.0","api":"v2.3"}
Dynamic Version Fetching: Override the default versions by injecting a custom service:
// src/Service/VersionProvider.php
class VersionProvider implements VersionProviderInterface
{
public function getVersions(): array
{
return [
'app' => $this->getAppVersion(),
'database' => $this->getDbSchemaVersion(),
];
}
}
Register it in config/packages/vdm_version.yaml:
vdm_version:
version_provider: App\Service\VersionProvider
Secret-Protected Endpoint: Enable secrets for detailed responses:
vdm_version:
secret: 'my_secure_secret'
Query with:
GET /version?secret=my_secure_secret
Or via header:
GET /version
Headers: VDM-Version-Secret: my_secure_secret
Integration with CI/CD: Use the endpoint in deployment scripts to verify version consistency:
VERSION_RESPONSE=$(curl -s http://localhost/version)
if [[ "$VERSION_RESPONSE" != *"app:1.0.0"* ]]; then
echo "Version mismatch!"
exit 1
fi
Custom Response Formatting: Extend the controller to modify output:
// src/Controller/VersionController.php
class VersionController extends AbstractVersionController
{
public function getVersionAction(): Response
{
$versions = $this->getVersions();
return new Response(json_encode([
'status' => 'success',
'data' => $versions,
'timestamp' => now()->toIso8601String(),
]));
}
}
Routing Conflicts:
Ensure /version doesn’t clash with existing routes. Use a custom path if needed:
vdm_version:
path: /api/version
Secret Handling:
secret is set but not provided, the endpoint returns an empty object {}.vdm_version:
secret: '%env(VDM_VERSION_SECRET)%'
Version Provider Overrides:
versions config and a custom version_provider are set, the provider takes precedence.php artisan cache:clear
Empty Response?
Check if the version_provider service is correctly bound in the container. Run:
php artisan debug:container | grep VersionProvider
404 on /version?
Verify the bundle’s routing file is loaded. Check config/routes.yaml for the vdm_version resource.
Add Metadata: Extend the response with build info or Git hash:
// In VersionProvider
public function getVersions(): array
{
return [
'app' => '1.0.0',
'build' => file_get_contents('/build/version.txt'),
];
}
Rate Limiting:
Protect the /version endpoint with Laravel’s middleware:
# config/packages/vdm_version.yaml
vdm_version:
middleware: ['throttle:60,1']
Caching: Cache the version response for performance (e.g., 1 hour):
// In VersionController
public function getVersionAction(): Response
{
return Response::cache(3600)->json($this->getVersions());
}
Boolean Values:
The secret parameter must be explicitly set to null to disable it:
vdm_version:
secret: null # Disables secret protection
Omitting it entirely defaults to null.
Array Syntax: Use YAML block style for multi-line versions:
vdm_version:
versions:
services:
- name: frontend
version: 1.0
- name: backend
version: 1.1
How can I help you explore Laravel packages today?