Showing posts with label symfony. Show all posts
Showing posts with label symfony. Show all posts

Monday, July 18, 2011

How to set batch / cron jobs in symfony.

I cam across an instance, where i needed to set up the batch job for certain API which were getting information from 3rd party vendors on certain time intervals.
So I started to look for doing same with symfony 1.4 and doctrine.
Below are some of my findings and better ways to create the batch / cron jobs.

I will walk you through the steps in order to get it working properly.

Batch Instead of Web

Symfony has already set up to make it easy to create small controllers that will be run from the command line.

The “/batch” directory is where you will be putting your special controller file, instead of the “/web” directory.  This batch file is looks pretty much like the front controllers that go in the “web” directory. The only thing you will be leaving out is the dispatch method which starts running the controller and action. We are going to be replacing it with a line that still fires up the sfContext instance so that we have access to our database, etc.

Batch File:

  1. <?php
  2. // this file is saved as SF_PROJECT/batch/cron_example.php
  3. define('SF_ROOT_DIR', realpath(dirname(__FILE__).'/..'));
  4. define('SF_APP', 'frontend');
  5. define('SF_ENVIRONMENT', 'prod');
  6. define('SF_DEBUG', true);
  7. require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
  8. // get the application instance
  9. sfContext::getInstance();
  10. // put whatever code you need here…
  11. echo "\r\n running cron job….\r\n";
    Or  we can also set configuration from Project configuration file as below:

    require_once(dirname(__FILE__) . '/../config/ProjectConfiguration.class.php');
    $configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'dev', true);
    sfContext::createInstance($configuration);
 it’s similar to a standard front controller file. Now all you require is add code / function your cron job requires do.

Run through command prompt:
  1. go to your PROJECT_DEIRECTORY
  2. php batch/cron_example.php
  3. running cron job…
If you get the expected output, then everything is good to go.

The Crontab File

set up the cron to run every minute and output to null.
  1. * * * * * * php /home/me/my_project/batch/cron_example.php > /dev/null 2>&1
Hope it helps you. comment in case any issue/concern.

Friday, April 29, 2011

How to add authentication in symfony for access rights - credentials

Today, I cam across issue where I needed to set access rights for certain user types in symfony for different modules.
Let me put up the steps and how it can be done:

First of all you need to make changes to security.yml in App directory:
/App/config/securiy.yml
For each module, depict whether it requires any authentication or not.

For example:
default:
  is_secure: false
modulename1:
  is_secure: true
modulename2:
  is_secure: true

Go to module where you require to have users based access
i.e App/[frontend | admin ]
in my case i wanted to set rights in frontend section:
so its apps/frontend/modules/config/security.yml
all:
  is_secure: true
  credentials: [ credentialname1, credentialname2 ]

if certain action requires authentication and certain not than:
for exmaple:
action1 and action2 below do not require any authentication where as remaining other requires authentication based on credentital.

actionname1:
  is_secure: false
actionname2:
  is_secure: true
  credentials: [ credentialname1 ]
all:
  is_secure: true
  credentials: [ credentialname2 ]

Now the final part is to assign credentials when user log-in to the system or application

Once authenticated, write the following code before any redirection:
$this->getUser()->addCredential('credentialname1');
OR
$this->getUser()->addCredential('credentialname2');

and finally, remember to clear the cache :)

Hope it helps
www.edugoog.com

Tuesday, April 26, 2011

Nice symfony plugin for flash based grpahs

I came across to build flash based graph in symfony. Found a nice plugin available , easy to integrate and has good customistion

http://www.symfony-project.org/plugins/stOfcPlugin

In case, anyone needs any help or concern, feel free to post .

Have a good time!!!