How to inject Zend Service Manager in ZF3 Controllers

Zend Service Manager component is Zend Framework’s implementation of service locator pattern. This object is very usefull component for an application and is largely used in ZF applications.

Unfortunately in ZF3 default application, Service Manager component is no more available in controllers.

An official solution exsists for this, but in this little tutorial, I want to share an easy solution to inject Service Manager in all controllers.

Continue Reading

Update Twitter Bootstrap to release 3 in Zend Framework 2

Recently I started a new project based on Zend Framework 2, using Twitter Bootstrap as CSS framework. Some days ago, new Bootstrap v3 was released and introduced a lot of changes and improvements. Because of ZF2 Skeleton Application comes out-of-box with Twitter Bootstap 2, I decided to setup the new project skeleton and update CSS framework to latest available release.

In this post, I’ll describe the process to update Bootstrap to v3 into a ZF2 project.

Please note: I set up the project with Zend Framework 2.2.2 and updated to Twitter Bootstrap 3.0.0-rc1 because, actually, are latest available versions. If time is passed, you should check official documentations to ensure that this procedure is still valid.

I’ll describe update process assuming that we have a working Skeleton Application deployed following Zend Framework Getting starded guide.

First of all, we have to update libraries in public/ folder by accomplish following steps:

  1. Download Twitter Bootstrap library package and unzip it.
  2. Move content of dist/ folder into <project>/public/ folder.
  3. Download Glyphicons package and unzip it.
  4. Copy fonts/ folder into <project>/public/ folder.
  5. Copy css/bootstrap-glyphicons.css into <project>/public/css/ folder.
Continue Reading

Setup Doctrine 2 filters in Zend Framework 2

Scenario

Doctrine filter is a very powerfull tool that can be used to add conditional clauses at SQL level into our Doctrine 2 engine. This means that filters constraints will affect DQL queries, collections, lazy loading, etc.

How to setup and use filters in generic conditions is well explained in this article of official Doctrine documentation, but in a Zend Framework 2 project, the same operation is a bit different.

So, in this post, we will explain how to configure one or more filters into our Zend Framework 2 projects.

Continue Reading

Configurare filtri Doctrine 2 in Zend Framework 2

Scenario

Un filtro di Doctrine è uno strumento molto potente che può essere utilizzato per aggiungere condizioni a livello SQL all’interno del nostro gestore di oggetti Doctrine 2. Ciò significa che i filtri influenzeranno il comportamento di query DQL, collezioni, recupero dati, ecc.

Come configurare ed utilizzare filtri in condizioni generali è ben spiegato in questo questo articolo della documentazione officiale di Doctrine, ma in un progetto basato su Zend Framework 2, la stessa operazione è un po’ diversa.

Così, in questo articolo, vedremo come configurare uno o più filtri nei nostri progetti sviluppati su Zend Framework 2.

Esempio esplicativo

Il seguente esempio è basato su quello riguardante un filtro “locale” mostrato nel manuale di Doctrine. Assumeremo di avere un progetto ZF2 funzionante sviluppato a partire da ZendSkeletonApplication con il modulo DoctrineORMModule configurato (fare riferimento ai manuali ufficiali per raggiungere questo obiettivo).

Innanzitutto, abbiamo bisogno di una classe interfaccia da utilizzare come segno distintivo per decidere quali entità saranno influenzate dal filtro. Quindi, se vorremo che un filtro sia applicato ad un’entità dovremo solo fargli implementare tale interfaccia. Ovviamente, possiamo anche utilizzare l’interfaccia per specificare quali metodi implementare nell’entità, se vogliamo. Questo può essere utile e vedremo un esempio in un prossimo articolo.

Continue Reading

ReCaptcha in Zend Framework 2 without Zend/Form

The problem

Captcha is a very useful mechanism to avoid automated abuse of your sites and applications. In particular, reCAPTCHA is a Google powered service that offers a free and simple way to implement a captcha protection field in your forms or pages. Zend Framework 2 implements his own components to handle captcha and a specific one to handle reCAPTCHA service. At the moment, the ZF2 documentation is very usefull if you want to integrate a reCAPTCHA in your Zend/Form component, but lacks in describing how to use reCAPTCHA service component alone. Actually Zend/Form is very powerful, but sometimes a developer need to use standard html to implement his own forms and this prevents to use ReCaptcha as Zend/Form element.

The solution

To reach our achievement, we will implement a reCAPTCHA service that can be use all over the project to generate captchas and test them against values inserted by user. This service will be used principally in views and controllers. The following code implementation assumes that you have a working ZF2 project, configured as described in official documentation.

Continue Reading