Log Route calls in Laravel

Introduction Sometimes when developing a new site or application in Laravel you need to log exactly the route calls in order to track bugs and errors. This operation is straightforward. First, let’s create a new Middleware php artisan make:middleware LogRouteMiddleware Then edit the handle function like the following: public function handle($request, Closure $next) { Log::info($request->fullUrl()); return $next($request); } The fullUrl method logs the full URL including all url and query parameters. You can decide also to log other information, for example: ip() returns the client IP address isJson() determine if the request is sending JSON Pro-tip: The API documentation for the Request class is available at http://laravel.com/api/5.1/Illuminate/Http/Request.html Finally in app/Http/Kernel.php you can include your Middleware to the list protected $middleware = [ ... 'App\Http\Middleware\LogRouteMiddleware', ... ];

Read More
Work on a Laravel Live Site while in Maintenance Mode

Introduction Since Laravel 4 there’s a handy feature that allows you to put your site in maintenance mode, providing a good way to hide your changes from external eyes. This feature is really helpful on your testing or production server since it doesn’t allow users to browse the site, so you can make you modifications pretty much unnoticed. The artisan commands are the following: php artisan down to put the site into maintenance mode, and php artisan up to disable the maintenance mode. When you don’t have access to the command line, for example in case of shared web servers, you can embed artisan commands in your routes. Route::get('/up', function() { \Artisan::call('up'); return "up"; }); Route::get('/down', function() { \Artisan::call('down'); return "down"; }); However this approach disables the access to the site to everyone, including you. If you want to work on your production server (for your legitimate reasons) while in...

Read More
Keep URL and query parameters using manual pagination in Laravel

Introduction The Laravel documentation is great most of the time, but when you are stuck on a particular problem you have to figure by yourself what is the solution. Recently I had to create manually a paginator instance using the LengthAwarePaginator class. The first attempt was nearly a success, here a snippet of my controller: $page = $request->has('page') ? $request->get('page') : 1; $data = $this->userRepository->paginate($request->all(), $page); $users = new LengthAwarePaginator($data->items, $data->totalItems, 15, $page); Pretty straightforward! The LengthAwarePaginator class constructor accepts the items, the total number of items, the items per page and the current page. In the browser however I immediately noticed that, when switching from the first page to the second, all the query parameters got lost. How do I solve this problem? By looking up into the Laravel API of course. Turns out that the constructor of LengthAwarePaginator accepts an optional array as a fifth parameter formed by...

Read More
Database Design for Bus Timetables

Recently, for a large website commissioned by the local agency for tourism, I had to design a system for storing Skibus timetables. The requirements were straightforward: store multiple bus lines; each line is a round trip, therefore it has two directions (outbound and return). But the route may not be the same; each line is effective between two dates The aim of the website was to provide timetables to regular users using standard HTML tables instead of static PDF files, allowing also mobile devices to browse them without hassle. The users could also choose to browse the timetables by line or by bus stop. Moreover, for each bus stop, the site should have allowed the automatic generation of A3 PDF timetables for later printing. The Process Sample page from the paper version of the timetables. The picture shows the timetable of the line N°4, with the two directions. Each direction...

Read More
Efficient appended property using Laravel Eloquent

This technique is really useful to check if an Eloquent relation is loaded on a Model, in order to efficiently generate an appended property. For example, this approach is particularly useful in a multilingual application, where you can have an entity that has a number of related translations in different languages. Consider the following code: class Entity extends Model { public function translations() { return $this->hasMany('Translation'); } } Our Entity class can have many translations, but the end user is interested only in the one that corresponds to his Locale. Wouldn’t be nice to automatically have a property in the class that stores the current translation or a fallback in case it doesn’t exists? In Laravel we can assign appended properties to each model simply by adding them to the $appends array: protected $appends = ['translation']; We have also to add a function named getTranslationAttribute that returns our value. For...

Read More
Dropzone.js, React.js and Fluxxor integration

Introduction In these days I’m digging deep in the wonderful React.js library, widely used by Facebook and Instagram for their UIs. This library uses an abstraction of the DOM, called Virtual DOM, to render components in the View. Honestly ~~I’m~~ was not a big fan of DOM abstractions, because in my opinon it affects both the work of developers and designers. HTML should not be mixed with JavaScript and viceversa. I’ve worked in the past years with declarative libraries like Knockout.js, so at first I was disoriented by this new approach. However after spending a couple of days on the docs I was really blown away by the power and the intuitiveness of the library. By the way, take a look at this blog post by Pete Hunt which presents a clear approach to start thinking in React. React.js “makes no assumptions about the rest of the technology stack”, therefore...

Read More