Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get version from composer.json [Symfony]

I wonder if there is a way to get the composer.json version from a controller with Symfony. My composer.json looks like:

{
    "name": "myProject",
    "version": "0.0.0",
    "description": "myProject description",
    "license": "AGPL-3.0",
    "type": "project",
    "require": {
        "php": "^7.1.3",
        ...
    }
}

I can't find any reference to this.

PS: I'm using Symfony 4.

like image 889
Manolo Avatar asked Aug 30 '26 00:08

Manolo


1 Answers

You can do something like this:

$filename = $this->getParameter('%app.kernel_dir%') . '/../composer.json';
$composerData = json_decode(file_get_contents($filename), true);

$version = $composerData['version'];

The variable should then containv the value 0.0.0 from your example.

This assumes that your controller extends the base Controller to access the %app.kernel_dir% parameter. If not, you could just as well use the relative path from your controller or something else to determine the location of the composer.json

like image 54
dbrumann Avatar answered Sep 01 '26 15:09

dbrumann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!