CakePHP & AngularJS Pagination

It is actually pretty easy to make your RESTful calls in CakePHP paginateable by using what the Paginator Component already offers us and using Angular UI Bootstrap Paginator on the client side code. On our server side we just use the built in serialization of CakePHP to generate the response. This is an example of a CakePHP controller index() method that is paginateable:

Now let’s fetch the data via a custom Angular JS service in our Angular controller. The service “someApiService” is doing nothing more than a $http.get() call internally and resolving the result and then returning the promise again.

We also need a method to do the page change when one of the pagination links is clicked, so that a new request is done with the “page” param is issued. This is actually the reason we’ve wrapped the API call in a function, we don’t have to duplicate the code. You’ll call getData() one time to load the data when you enter the page and later each time a page button is clicked.

In your Angular view:

That’s all, as you can see it is pretty easy to get AJAX pagination working with these two awesome frameworks!

CakePHP 3.0 i18n – language inside the URL

In CakePHP 2.x it was pretty cumbersome to get the language across all pages into the URL. With CakePHP 3 it is just awesome and easy. You can now create persistent URL parameters.

This is basically all you need for the most basic implementation inside your config/routes.php. The code is mostly the same as in the previous link to the persistent URL parameters page but I’ve added the default language so  that it is always present. If you don’t want a default language to be always present just remove the elseif part. Notice that you’ll have to add routes into the /:language/* scope to inject it.

If you now want to change the URL to something else by a simple click on a flag for example you can use a simple link like this to change it:

If you don’t want the language to be part of the URL but still detect the language somehow you can use the LocaleSelector dispatcher filter that will get it from the client. This is straight taken from the CakePHP book:

By using the LocaleSelectorFilter in your application, CakePHP will automatically set the locale based on the current user:


Using Robo to automatically trigger unit tests while developing

Robo is an excellent php task runner, similar to NodeJS Gulp. I was looking for a pure php alternative to Gulp for my php projects and I simply prefer to have a tool chain written in the same language if possible. Also I found the code to be a lot better manageable than using Gulp.

So here is one of my main use cases I have for it. I wanted to run the unit tests as soon as I modified a test case. The following code is for a CakePHP3 project.

You can adept the above script for other projects as well pretty easy. Also that should be required is to make sure that you change line 13 so that it fits the path and convention of your project.