Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3925

Symfony 4, how get the root path directory (or /src path) properly from a Service Class ? ( error : 'Call to a member function get() on null')

$
0
0

In Symfony 4, from a controller I can get the root path of my project via :

// From a Controller Class, in the src/Controller dir

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
// ...
Class myController extends Controller{
// ...
// in a public method
$rootDir = $this->get('kernel')->getRootDir();

But how can I get the root path from a Service Class ? I tried this (ugly) way

// From a Service Class, in the src/Service dir

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
// ...
Class myService extends Controller{
// ...
// in a public method
$rootDir = $this->get('kernel')->getRootDir();

But when I did that, I get the error :

"Call to a member function get() on null"

Why this solution does not work?


Viewing all articles
Browse latest Browse all 3925

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>