Sevalla is the all-in-one PaaS for your web projects. Host and deploy your applications, databases, object storage, and static sites. Enjoy advanced deployment pipelines, a complete database studio, instant preview apps, and one-click templates. The pricing is simple: no hidden fees, no seat-based pricing, and you pay only for what you use. Get real human support from developers.

Get started now with a $50 credit at Sevalla.com.

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

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

An exceptional change in PHP 7.0

Link –

Davey Shafik cleary explains the new exception hierarchy in PHP 7.

With PHP 7 errors and exceptions are undergoing major changes. For the first time, the PHP engine will start to emit exceptions instead of standard PHP errors for (previously) fatal, and catchable fatal errors. This means that we can now handle them much more gracefully with `try... catch`.
http://daveyshafik.com/archives/69237-an-exceptional-change-in-php-7-0.html

Read more

What would make Laravel Forge even better

Original – by Freek Van der Herten – 2 minute read

A little over a year ago Laravel Forge was launched. At Spatie we currently have 60 servers that are provisioned by and administered using it. I'm assuming we still hold the biggest Forge-account. By this time next year the number of servers will probably be higher. So yeah, I'm a very happy…

Read more

Upload large files to S3 using Laravel 5

Original – by Freek Van der Herten – 1 minute read

Chris Blackwell yesterday published a tutorial on how to upload files to S3 using Laravel. This is the code he used (slightly redacted): $disk= Storage::disk('s3'); $disk->put($targetFile, file_get_contents($sourceFile)); This is a good way to go about it for small files. You should note…

Read more

Abusing neural networks to generate images

Link –

Over at the Google Research blog there's an interesting article on how neural networks can be abused to generate very psychedelic images.

The results are intriguing—even a relatively simple neural network can be used to over-interpret an image, just like as children we enjoyed watching clouds and interpreting the random shapes. This network was trained mostly on images of animals, so naturally it tends to interpret shapes as animals. But because the data is stored at such a high abstraction, the results are an interesting remix of these learned features.
Realmac has developed an OSX app to tinker with the technology. You can download a trail version on their website. Here's my current Twitter avatar processed by the "animals" preset:

Deep Dreamer

Read more

Displaying stream progress in PHP

Link –

Hannes Van de Vreken has written a tutorial on how to display stream progress in PHP.

Opposite to HTTP requests, tasks run from the command line aren’t supposed to return instantly. They can take a very long time. Imagine a task that loops an entire database table or a task that references an external source repetitively, or maybe a taks that performs a large file transfer. It’s very important to show the issuer what is actually going on, or he/she will be left in the dark for minutes/hours. "Is this task still running?", "How long has this thing been running yet?", "Is it almost done?", "Is it running out of memory?"
https://hannesvdvreken.com/2015/05/12/stream-progress/

Read more

Using enums instead of class constants

Link –

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

Our open source software

Original – by Freek Van der Herten – 1 minute read

The past few months my colleagues and I invested quite some time on creating open source software. Because there now are a lot of packages under the Spatie-vendor name, we decided to put a nice overview on our website. Obviously these packages benefit the community, but there are a lot of advantages…

Read more

How is Doctrine 2 different to Eloquent?

Link –

One of the really great things about ORM’s that implement the Active Recordpattern like Eloquent is, they are really easy and really intuitive to use.

With Active Record, you basically just have an object that you can manipulate and then save. Calling save() on the object updates the database, and all of the magic persistence happens behind the scenes.

Having the business logic of the object and the persistence logic of the database tied together definitely makes working with an Active Record ORM easier to pick up.

In this tutorial I want to explore exactly how Doctrine 2, an ORM that implements the Data Mapper pattern, is different to Eloquent.

http://culttt.com/2014/07/07/doctrine-2-different-eloquent/

Read more

Speed up a Laravel app by caching the entire response

Link –

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

10 Reasons To Use HTTPS

Link –

Today, however, there are more reasons than ever to switch to HTTPS — even for a news site, corporate site, or any site that doesn’t consider itself at the top of the security food chain. HTTPS adoption grew 80% last year alone, much faster than previous years, but we’re still very far from encryption being the norm.

If you’re not convinced HTTPS is right for you, or need ammo to convince your peers and bosses, here are 10 good reasons to go HTTPS.

https://medium.com/@guypod/10-reasons-to-go-https-a2cba5734bb6

Read more

How to contribute to PHP documentation

Link –

Sammy Kaye Powers wrote a post on how to contribute to the PHP documentation.

If you've been wanting to contribute to PHP internals, starting with the documentation can be a great entry point; especially because it doesn't require dusting off those old C books from college.

But knowing where to start can be tricky since information on how to contribute to the docs is scattered across the internet.

Phew! After reading this process my intentions of contributing melted away like snow in the sun. Editing the documentation should be much easier. I believe there would be more contributions if the docs would reside in a git-repo as markdown-files.

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

In favor of the singleton

Link –

A short and very enjoyable read by Uncle Bob on singletons:

Do you recognize this: ``` public class X { private static X instance = null;

private X() {}

public static X instance() { if (instance == null) instance = new X(); return instance; }

// more methods... }


<em>Of course. That's the Singleton pattern from the GOF book. I've always heard we shouldn't use it.</em>

Why shouldn't we use it?

<em>Because it makes our systems hard to test.</em>

It does? Why is that?

....</blockquote>
You can read the whole conversation on <a href="http://blog.cleancoder.com/uncle-bob/2015/07/01/TheLittleSingleton.html">http://blog.cleancoder.com/uncle-bob/2015/07/01/TheLittleSingleton.html</a>

Read more