package

All my posts about package.

Using Algolia in Laravel original

by Freek Van der Herten – 2 minute read

Algolia is a hosted service that makes advanced searching very easy. It's well documented and lightning quick. You can see some impressive examples on their site. Artisans probably know that Jeffrey Way recently published a series on Algolia. Earlier this year I made a package to easily work with a…

Read more

Laravel-medialibrary hits version 3 original

by Freek Van der Herten – 3 minute read

Not a month has gone by since v2 of the laravel-medialibrary package got released. If you're not familiar with it: the package provides an easy way to associate files with Eloquent models. Though I was quite happy with the improvements made over v1 there were some things that bothered me. Take a…

Read more

Join 9,500+ smart developers

Get my monthly newsletter with what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.

No spam. Unsubscribe anytime. You can also follow me on X.

Using enums instead of class constants

I've been enums lately instead of relying on class constants. The myclabs/php-enum package provides a nice implementation. The readme lists the benefits of doing so:

Using an enum instead of class constants provides the following advantages:
  • You can type-hint: `function setAction(Action $action) {`
  • You can enrich the enum with methods (e.g. `format`, `parse`, …)
  • You can extend the enum to add new values (make your enum `final` to prevent it)
  • You can get a list of all the possible values (see below)
This Enum class is not intended to replace class constants, but only to be used when it makes sense.
Check it out: https://github.com/myclabs/php-enum

 

Read more

Speed up a Laravel app by caching the entire response

A typical request on an dynamic PHP site can do a lot of things. It's highly likely that a bunch database queries are performed. On complex pages executing those queries and hydrating them can slow a site down.

The response time can be improved by caching the entire response. The idea is that when a user visits a certain page the app stores the rendered page. When a second request to the page is made, the app shouldn't bother with rendering the page from scratch but just serve the saved response.

I've made a Laravel package named "laravel-responsecache" that does just that. Installing it is very easy: just add the service provider and facade to the app's configuration. And step two is... there is no step two. In most cases you're done. All successful responses (that is a response with a statuscode in the 200 or 300 range) to a GET-requests will now be cached for a week. If the response of a specific route or controller should never be cached middleware can be added that prevents caching. Furthermore each logged in user will have have it's own separate cache. Cached responses can be stored in any configured repository in Laravel. You could easily share a cache between servers by using memcached.

I think that behaviour will suit a lot of use cases. If you need some other caching behaviour (eg. cache error responses, exempting redirects, using a common cache for users with the same role, changing the expiration time of the cache) you can easily write a custom caching profile.

The package isn't supposed to sweep performance troubles under the rug. All apps should be optimized so that they'll respond in an acceptable timeframe without using response caching. My rule of thumb is that typical pages in a cms should be able to render within a second (and preferably much less). Anything above that is unacceptable. That number is by no means scientific. Make up your own mind what an acceptable responsetime should be. Of course all of this depends on the type of site and the amount of visitors it has to handle. Also keep in mind that that there are a lot of other aspects that need to be considered when trying to deliver a speedy experience.

There are some great alternatives to cache responses. Two well known solutions are Varnish and Nginx caching. They take response caching one step further by not even invoking php when serving a cached request. Both options are very robust and can work on any scale. The benefits the Laravel package has over Varnish-like solutions is that it is easier to set up and that application logic can be used to determine what needs to be cached.

If you're interested in speeding up your Laravel app using the package, go take a look at it on GitHub:

https://github.com/spatie/laravel-responsecache

Read more

Convert a pdf to an image using PHP original

by Freek Van der Herten – 1 minute read

Converting a pdf to an image is easy using PHP, but the API kinda sucks. $imagick = new Imagick('file.pdf[0]'); $imagick->setImageFormat('jpg'); file_put_contents($pathToImage, $imagick); The pdf-to-image-package aims to fix that. Here is the equivalent code: $pdf = new…

Read more

A new version of the medialibrary package original

by Freek Van der Herten – 1 minute read

When starting out with Laravel I made a medialibrary to associate uploaded files with models. In our custom made cms we use the medialibrary to for example associate articles with images. In short the medialibrary could: associate files with models generate thumbnails and other derived images from…

Read more

A trait to optionally abort a Laravel app original

by Freek Van der Herten – 1 minute read

Inspired by Edd Man's post on optional value control-flows I made a small Laravel package to optionally abort your application. The package provides a Spatie\OrAbort\OrAbort-trait that can be used on any class you want. All the methods of the class will gain orAbort-variant. When the original…

Read more

Common string functions

When working on projects I found myself, over time, needing the same string functions over and over again. Things like how to shorten a string but still let it end on entire word, replace the last occurrence of string in a string, ...

Instead of keeping these functions in a helper file locally in the project, I made a package out of it. It was a good opportunity to spice it up with bit of OO to make them chainable. An example:

[code] // outputs "MIDDLE" echo string('StartMiddleEnd')->between('Start', 'End')->toUpper();



In addition to it's own methods, the package provides <a href="https://github.com/spatie/string#integration-with-underscorephp">an integration</a> with <a href="https://github.com/Anahkiasen/underscore-php">Maxime Fabre's underscore package</a>.

You are very welcome to submit pull requests to add functions that you feel are missing. Be sure to include some unit tests to ensure everything working as intended.

Granted,  it isn't the most sexy package in the world, but it sure is handy.

<a href="https://github.com/spatie/string">https://github.com/spatie/string</a>

Read more

A Laravel package to easily add paginated routes original

by Freek Van der Herten – 1 minute read

Laravel offers a nice way to add pagination. As far as your routes are concerned you don't have to do a thing. It just works out of the box. Unfortunately the generated url's are pretty ugly: http://example.com/news?page=2 What we want are url's that look like this: http://example.com/news/page/2…

Read more

Handling private composer packages with Satis

Satis allows you to require private php packages in your projects. One of it's authors is Jordi Boggiano of composer/packagist fame. Over at the excellent Laravelista blog Mario Bašić explains how to install and use it.

I was working on three projects at the same time and I've found myself copying code from one project to another and then making small changes. And then I recognized a pattern. ...

I created a package and as soon as it was ready for GitHub I remembered: "I can't open source this...". The package contained few paid html templates, company logo's, slogans, specific company text etc. ...

This post will explain what Satis is, when to use it and how to set it up on your server.

Read more

Creating packages original

by Freek Van der Herten – 2 minute read

Prosper Otemuyiwa recently wrote an article on how to create Laravel 5 packages on his blog. Although his approach is entirely valid and may suit you well, I work a little differently when creating a new package. First, I create a new GitHub repository where the package will live. In that repo I…

Read more

Manage newsletters in Laravel 5 original

by Freek Van der Herten – 1 minute read

A few hours ago I tagged version 1.0.0 of a my new package: laravel-newsletter. It provides a very easy way to interact with email marketing services. Or maybe I should simply say MailChimp, as it is currently the only supported supported service. After you install the package (un)subscribing an…

Read more

A medialibrary package for Laravel 5 original

by Freek Van der Herten – 3 minute read

At Spatie all our greenfield projects are powered by custom built CMS based on Laravel 5. An important part of the CMS is the medialibrary-component. It handles how uploaded files are associated with models. My intern and I recently took the time to release our medialibrary as a package on GitHub.…

Read more

A Laravel package to retrieve Google Analytics data original

by Freek Van der Herten – 1 minute read

If you need to retrieve some data from your Google Analytics account in Laravel 5, then laravel-analytics is the package for you. Assuming the analytics tracking code is installed on your site, the package allows you to determine which pages are visited the most, which browsers are used most to…

Read more

Clone your package inside the vendor directory

Dimitrios Savvopoulos, the creator of the laravel-translatable package (which I use in almost every project), shared a very nice tip on how to develop a package while it is installed as a requirement.

If you have write access to a composer package repository, you have the possibility to continue its development while it is installed as requirement in another project. Let's see how we can accomplish this.
http://dimsav.com/blog/9/git-repository-inside-composer-vendors

If you have another, possibly better, way to go about this, let me know in the comments.

Read more

A package to backup your Laravel 5 app original

by Freek Van der Herten – 1 minute read

Last week I made a Laravel 5 package that could dump your db. Last saturday night I took the time to expand the functionality. laravel-backup can now backup your entire application. The backup is a zipfile that contains all files in the directories you specify along with a dump of your database. The…

Read more

Add a command to dump your database in Laravel 5 original

by Freek Van der Herten – 1 minute read

This Laravel 5 package provides an artisan command to dump your database to a file. When installed you can dump your database with this command [code]php artisan db:backup``` A file containing the dump of your database will be created in the directory you specified in the config-file. I plan on…

Read more