0

From a Symfony 4 project, Is there a properly/symfony way for get the root path directory from a Fixture class ?

The final goal :

I use fixtures class for load some "default" datas in my database when I deploy my website. (May be its not a good practice)

Among theses defaults datas, I need to store the root path directory of the project because I need this information from custom utils and services class in the project.

I know there are multiple ways for that. Currently, I hard coded the absolute path ( <= best ugliest way ever ;) )

5
  • Typically your fixtures would reside in a single "/tests" directory in your project root directory, allowing you to use __DIR__. However most fixtures are expected to be isolated from your application configuration. Please update your question to show what you have tried so far and we can help you with any issues you are encountering. If you need some direction, I suggest looking at the Symfony tests, which use a KernelMock to determine how they are using it. Commented Nov 5, 2018 at 14:24
  • thanks for your comment, and done ;) Commented Nov 5, 2018 at 14:57
  • 1
    I see, there was some confusion of a fixture, which are typically used as isolation tests. You could just use your AppKernel in your data fixture, for the desired environment. e.g. $kernel = new AppKernel('dev'); Then retrieve the root dir by using $kernel->getRootDir(); or $kernel->getProjectDir(); depending on the context, since they use __DIR__ by default. Commented Nov 5, 2018 at 15:06
  • 1
    I think he should not instance another kernel !!! Instead he should get the existant instance this->get('kernel')->getProjectDir() Commented Nov 6, 2018 at 4:45
  • 1
    The fyrye's solution works but I agree with AhmedEBENHASSINE . However, the context of "this" is not good from a Service Class, so when I use "$this->get('kernel')->getProjectDir()" , I get the php error "Attempted to call an undefined method named "get". Instead (fyrye solution) I inject the "KernelInterface" service in the constructor of my custom service and use a "$kernel->getProjectDir()". I'm not sure if its the better solution. Commented Nov 6, 2018 at 7:57

1 Answer 1

6

Symfonfy 4.1.7

Service.yml

services: _defaults: bind: $webDirectory: '%kernel.project_dir%/public_html/' 

Fixtures File

use Symfony\Component\HttpFoundation\File\UploadedFile; // -- private $webDirectory, $imageLocation; public function __construct($webDirectory) { $this->webDirectory = $webDirectory; $this->imageLocation = $webDirectory . 'img/config/'; } public function load(ObjectManager $manager){ for ($i = 0; $i < 20; $i++) { copy($this->imageLocation . 'blog_default_50x50.jpg', $this->imageLocation . 'blog_default_50x50-copy.jpg'); $file = new UploadedFile($this->imageLocation . 'blog_default_50x50-copy.jpg', 'Image1', null, null, null, true); --- $blogPost->setFile($file); $manager->persist($blogPost); } $manager->flush(); } 

UploadedFile() info from https://stackoverflow.com/a/18883334/624533 (Thankyou!).

Hope this helps someone.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.