CategoryZend Framework

Zend Framework related information

Add URL query parameters to links using view helper in Zend Framework 2

There are 2 ways to add query parameters using the URL view helper in Zend Framework 2, either using Segment route placeholders, or adding them as normal query parameters (e.g. url?id=1&name=barney) Which one you need to use depends on what route the view helper is being used on; since all URL’s in Zend Framework 2 need to be route-aware (even if you’re mimicking ZF1 routing) it...

Mock a JSON HTTP API response in a PHPUnit unit test for Zend Framework 2

In order to mock an API in a unit test, you need to be able to mock the response that comes back from the API request. Therefore you need to specify in the test what JSON (for example) would come back from the HTTP client. To do this in Zend Framework 2, you need to mock the Zend\Http\Client so that it always returns a mock response, and have the mock Zend\Http\Response always return the JSON...

“The class ‘x’ was not found in the chain configured namespaces”

If you are getting a namespace chain error when trying to load entities with Doctrine 2 in Zend Framework 2, it is likely because as far as Doctrine is concerned the namespace your entity is in is missing. If you’ve copied Jason Grime’s Doctrine 2 and Zend Framework 2 tutorial then you’ll notice the configuration he places in module.config.php array( 'doctrine' =>...

Using ZFDebug for Zend Framework with database and cache

ZFDebug is a Zend Framework plugin to debug and analyse your web application as it bootstraps, so that you can see how long everything takes to load, including how many database and cache calls are made. Having used ZFDebug in the past, it’s a handy tool but somewhat light on documentation. Here is a simple way to set it up to debug your database and cache calls, which should be placed...

Using Zend_Pdf and Zend_Pdf_Image with remote URL’s (files)

Zend_Pdf is a handy component of Zend Framework, but expects you to use local files. However if you need to open files that are located on a different server (such as an Amazon S3 bucket) you will need a way of getting them into Zend_Pdf first. $pdf = Zend_Pdf::load($pdfUrl); One solution is to stream them in via a string… $pdf = Zend_Pdf::parse(file_get_contents($pdfUrl)); Similarly, if...

Install Zend Framework 2 and Doctrine 2 using Composer

Since the installation instructions aren’t overly clear (I don’t find) for how to install more than one package to a project, you need a composer.json that looks akin to the following: { "require": { "doctrine/orm": "*", "zendframework/zendframework": "2.0.*" } } What caught me out is that all the packages you want need to be in the...

Persisting Zend controller variables whilst forwarding actions

Quick tip for when you’re trying to persist variables in a controller using Zend Framework, if you’re using a class property for your action controller then it’s because using the _forward action helper will actually re-instantiate the controller class, so your member variables (properties) will be reset. Thus if you declare them static then they will persist between _forward...

Numeric range queries using Zend_Search_Lucene, working around the bugs

Zend_Search_Lucene is a PHP implementation of the Lucene full-text searching engine. Whilst some of it works well, other bits don’t quite, such as performing range queries. The most common error for range queries is along the lines of an Zend_Search_Lucene_Search_QueryParserException that states Range query boundary terms… This, as may have guessed, is that queries that work fine in...

Use Zend_Log_Writer_Firebug to debug AJAX requests using Firebug

Debugging a page request in PHP, simple right? Either output to the page or command-line using something like var_dump, or use a step-through debugger to go through line-by-line. The advantage of the former is that it’s quick, and although the latter is more thorough it can be quite time-consuming. However when you’re debugging an AJAX request, you can’t simply output as...

Using Zend_Db_Profiler_Firebug to show database queries per page

Although ZFDebug is a great tool, it does require you to show the toolbar when you need to check something. Whilst this is fine for developers or administrators, it’ll probably confuse regular users. Therefore if you’re trying to debug something a regular user is doing, you may need another way to see what database queries are being run. Luckily Zend Framework has both a logger and a...