How to access Symfony Components in a custom main file

147 views Asked by At

I am working to make a batch file which will get some data from DB and send email. A folder Batch is created in the same level of app and web. Now my question is - how to access DB and other controls out of controller.

I tried to import use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; but it gives error saying no such files found.

I don;t want it to be command/console app.

My aim is to create a file which uses symfony components and Doctrine by staying outside bundle/controller. How do I import files in the file?.

1

There are 1 answers

1
Mario On

First I really recommend to create a Symfony Console Command for this.

But if you really want to create a file (lets say test.php) in your document root, you can load a symfony service like this:

<?php
require __DIR__.'/app/autoload.php'; // load the autoloader

$kernel = new AppKernel('prod', false); // create a kernel instance
$kernel->loadClassCache(); // load the class cache
$kernel->boot(); // boot the kernel which will initialise the container.

$myInstance = $kernel->getContainer()->get('myservice');

$myInstance->doWhatever();

PS: In there you should also be able to make use of all other symfony components.

PPS: The Notice: Use of undefined constant OCI_COMMIT_ON_SUCCESS - assumed 'OCI_COMM IT_ON_SUCCESS' error tells you that something is wrong with your oci8 extension try to reinstall/install or enable it again. http://php.net/manual/en/oci8.installation.php