If you are using the “FOSUserBundle” in Symfony, here is how you can check if a user is logged in:

if ( $this->get(‘security.context’)->isGranted(‘ROLE_USER’)) {
//User is logged in
}

if( $this->container->get(‘security.context’)->isGranted(‘IS_AUTHENTICATED_FULLY’) ){
// authenticated (NON anonymous)
}

For more information about user authentication in Symfony, visit the Friends of Symfony website at:  https://github.com/FriendsOfSymfony/FOSUserBundle.

If you are an organization in the Jacksonville Northeast Florida area looking for open source PHP web developers with experience in Symfony, Drupal, WordPress, Magento, or API integration experts, please contact me.  I have a list of competent web developers who I can recommend here in Jacksonville, Southwest Florida, and throughout the US, Canada, UK, Australia, and Germany as temporary web development consultants or remote web developers!


I usually end up implementing my own very small class that couples with a database table to create my own caching system.  It’s very easy to create one that way and it’s organized and has the extra benefits of not being files and so can be easier to maintain and access, be removed upon expiration with a simple query to the database.

That said, Symfony has a very thorough caching system, and other developers may have noticed an option called “cache:warmup”.  Of course, to warm up a cache usually refers to prepping the system and pre-populating the cache so the first visitors don’t get hit with a performance penalty because, well, they were the first poor shlubs to access a certain page that hadn’t been cached yet.  Kind of like trailblazers for the rest.

Anyway, I wanted to find out more, so I visited this site and found a very thorough explanation.

http://blog.whiteoctober.co.uk/2014/02/25/symfony2-cache-warmup-explained/

Sensio Labs, who is the company behind Symfony, has made an incredible tool, but I find their online resources aren’t nearly as thorough as Zend.  More real world examples ranging in complexity could easily quadruple their cookbook, but it’s needed.  With such a powerful and diverse PHP framework and so many tools, there is plenty of room to help build the community and create more common practices along with concrete examples.  To dig into Symfony, I suggest visiting their website at http://symfony.com/doc/current/cookbook/index.html.

Cache warming in Symfony.  Another mystery revealed!


Every field in Symfony now is an instance of a form object.  To add custom errors in the controller upon form submission, you have to access the form object of the field then add the custom error to it.

$error = new formerror("Your field custom error message here.");
$form->get('fieldName')->addError($error);

//You will need to include the formerror class at the top of the file:

use Symfony\Component\Form\FormError


In Symfony, you can set the parameter “invalid_message” in your form types.

For example:

class FooShortlistChoiceType extends AbstractType {
    protected $em;

    public function __construct(EntityManager $entityManager)
    {
        $this->em  = $entityManager;
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $fooTransformer = new FooToStringTransformer($this->em);
        
        $builder
            ->add('yourField', 'text', array('invalid_message' => 'Invalid message pertaining to this form field here.'))
            ->get('yourField')->addModelTransformer($fooTransformer);
        
    }

    public function getParent() {
        return 'choice';
    }

    public function getName() {
        return 'fooShortlist';
    }
}


For more about Data Transformers and what they’re used for, visit the Symfony Cookbook:  http://symfony.com/doc/current/cookbook/form/data_transformers.html


In Symfony, making a service means different classes and their functions can be called on as needed from the controller without cluttering up your controller and allowing you to separate useful code that can be called on from other places later.  That’s modularity, efficiency and the DRY principle (Don’t Repeat Yourself).

Here is a simple pattern to get you started in adding a service entry and then an example is provided how to call on the custom class.  This calls on a special class of methods meant to deal with processing payments, including cleaning credit card input of any characters except digits.

In this example, the file “ProcessPayment.php” is saved in the bundle’s /Form directory where I suggest making a special folder called “Handlers”.  Handlers, as in “form handlers,” actually signifies the opposite of what a repository does in Symfony.  A repository is meant to only accept parameters and return values, like a list of the 5 most current blog posts, etc.  Also, you don’t have to save your form handlers in a “/Handlers” folder like I have, but I find that helps organize code and segregate these more sensitive form functions being saved within the “/Forms” folder of the bundle.

Just a tip I find useful in remembering where things go.  When you first start Symfony, the file organization can be a little confusing.

In bundle services.yml:

parameters:
  process_payment.class:  Main\MainBundle\Form\Handlers\ProcessPayment
    process_payment.transport:  cleanCcInput  # This line is not necessary to work

services:
  process_payment:
    class:  "%process_payment.class%"
      calls:
        - [cleanCcInput,['']]

Code to call on service from controller:

$cc_full_num_cleaned = $this->get('process_payment')->cleanCcInput($cc_full_num);

It would be helpful if there were more solid examples of Symfony projects for those learning this powerful framework. If any developers have any recommendations, I would appreciate it, not just for me, but some friends and colleagues who are interested!

– Aaron Belchamber