Posts tagged with spatie

What Laravel 5.5 means for our packages original

by Freek Van der Herten – 5 minute read

At Spatie we've released a plethora of Laravel packages. Now that Laravel 5.5 has been released most of our packages will get a new (major) version. In this blogpost I'd like to explain how we handle new releases of the framework and what it means for our packages. Preparing for release Laravel has…

Read more

Join 9,500+ smart developers

Every month I share 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.

Easily convert webpages to images using PHP original

by Freek Van der Herten – 4 minute read

Browsershot is a package that can easily convert any webpage into a image. Under the hood the conversion is made possible new headless options in Chrome 59. In this post I'd like to show you how you can use Browsershot v2. Here's a quick example of how it can be used:…

Read more

A package to enable short class names in an Artisan tinker session

Tinker is probably one of my most used Artisan commands. A minor annoyance is that it can be quite bothersome having to type the fully qualified classname to do something simple.

Today we release a new package called laravel-tinker-tools. When fully installed let's you use the short class names in a tinker session:

This magic does not only work with models but with every class in your Laravel app.

Installing the package

There are a few non standard steps you need to take in order to install the package.

First, pull in the package like you normally would:

composer require spatie/laravel-tinker-tools

Next, create a file named .psysh.php in the root of your Laravel app with this content:

<?php

\Spatie\TinkerTools\ShortClassNames::register();

Finally, dump the optimized version of the autoloader so autoload_classmap.php gets created.

composer dump-autoload -o

And with that all out of the way you can use short class names in your tinker session.

A peek behind the curtains

When you use a class that hasn't been loaded in yet, PHP will call the registered autoloader functions. Such autoloader functions are responsible for loading up the requested class. In a typical project Composer will register an autoloader function that can include the file where the class is stored in.

Composer has a few ways to locate the right files. In most cases it will convert the fully qualified class name to a path. For example, when using a class App\Models\NewsItem Composer will load the file in app/Models/NewsItem.php. It's a bit more complicated behind the scenes but that's the gist of it. To make the process of finding a class fast, Composer caches all the fully qualified classnames and their paths in the generated autoload_classmap.php, which can be found in vendor/composer.

Now, to make this package work, \Spatie\TinkerTools\ShortClassNames will read Composer's autoload_classmap.php and convert the fully qualified class names to short class names. The result is a collection that's being kept in the $classes property

Our class will also register an autoloader. When you use NewsItem in your code. PHP will first call Composer's autoloader. But of course that autoloader can't find the class. So the autoloader from this package comes next. Our autoloader will use the aforementioned $classes collection to find to fully qualified class name. It will then use class_alias to alias NewsItem to App\Models\NewsItem.

What happens if there are multiple classes with same name?

Now you might wonder what'll happen it there are more classes with the same name in different namespaces? E.g. App\Models\NewsItem, Vendor\PackageName\NewsItem. Well, autoload_classmap.php is sorted alphabetically on the fully qualified namespace. So App\Models\NewsItem will be used and not Vendor\PackageName\NewsItem.

Because App starts with an "A" there's a high chance that, in case of a collision, a class inside your application will get picked. Currently there are no ways to alter this. I'd accept PRs that make this behaviour customizable.

Need more tinker magic?

There are a lot of other options that can be set in tinker.config.php. Learn all the options by reading the official psysh configuration documentation. Caleb Porzio's excellent blogpost "Supercharge Your Laravel Tinker Workflow" is an excellent read as well. This package was inspired by that blogpost

Maybe you don't need tinker...

If you want to run a single line of code tinker can be a bit of overkill. You must start up tinker, type the code, press enter, and quit tinker. Our laravel-artisan-dd package contains an Artisan command that can dump anything from the commandline. No need to start and quit tinker anymore.

We've updated the package so you can make use of short class names too. Here's how you can dump a model using a minimal amount of keystrokes:

In closing

laravel-tinker-tools and laravel-artisan-dd aren't the only packages we've made that make developing Laravel apps easier.

Be sure to take a look at these ones at well:

Need even more stuff? The open source section on our company website lists everything we've released previously: https://spatie.be/en/opensource

Read more

Our postcard collection original

by Freek Van der Herten – 1 minute read

All our packages are MIT-licensed. But if you use our stuff and want to make us happy, we highly appreciate a postcard from your hometown. This suggestion is mentioned in all readme's of our packages We've been asking for postcards for quite some time now and have built up a nice collection. Today,…

Read more

A package to remember a visitor's original referer

If you want to know how a visitor got on your site you can check the referer request header. Yeah, it's misspelled. That header contains the url of the previously visited page. Unfortunately browsers will fill that header regardless of the previous url was an internal or external one. So after the first click on an internal link you won't know anymore on which site a visitor was on previously.

Our new laravel-referer package aims to fix that problem. Once the package is installed it will remember the original referer in session. So even after a users clicks around on your site, you are still able to detect which site he or she visited previously.

Because users are also often tracked using UTM Codes the package will also remember the utm_source query parameter.

The easiest way to retrieve the referer is by just resolving it out of the container:

use App\Spatie\Referer\Referer;

$referer = app(Referer::class)->get(); // 'google.com'

Or you could opt to use Laravel's 5.4 fancy new automatic facades:

use Facades\Spatie\Referer\Referer;

$referer = Referer::get(); // 'google.com'

To know more take a look at the readme of the package on GitHub.

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

Read more

A package to easily manipulate images in PHP original

by Freek Van der Herten – 4 minute read

Today we released a new package called image that makes manipulation images in PHP extremely easy. In this post I'd like to explain why we built it and how it can be used. Manipulating images in PHP To manipulate images in PHP there are already a lot of options. You can go hardcore and use the Gd or…

Read more

Introducing Private Packagist

Jordi Boggiano and Nils Adermann, creators of Composer, have recently released a paid version of Packagist. The service aims to make managing private packages a breeze.

Private Packagist aims to remove all these hurdles for businesses to finally make working with Composer as convenient as it should be. Being a hosted service, setting up your own Composer package repository on Private Packagist is done with a few clicks. No matter if your private source code is hosted on GitHub, GitLab, Bitbucket, any of their on-premise solutions, or in any other Git, Mercurial, or Subversion repository, Private Packagist can immediately access your code after setting up your credentials to make it available for installation through Composer.

https://medium.com/packagist/introducing-private-packagist-492553d10660

If you're not afraid to get your hands dirty you could, instead of using Private Packagist, choose to use Satis. This tool is also written by Jordi & Nils. Laravelista has posted this great tutorial to get you started with the tool.

At Spatie we have set up a satis server to register packages that are intended to only be used in our own projects.

Read more

An easy to install uptime monitor original

by Freek Van der Herten – 4 minute read

A few weeks ago we released our uptime and ssl certificate monitor. It's written in PHP and distributed as a Laravel package. If you're familiar with Laravel that's all fine, but if you have no experience with that (kick ass) framework, it's a bit difficult to get started with using our uptime…

Read more

A package to fluently generate schema.org markup original

by Freek Van der Herten – 2 minute read

Schema.org is a vocabulary of microdata markup that aims to make it easer for search crawlers to understand what's on a webpage. The vocabulary is very extensive: here's the official list of things that can be described with Schema.org. This article on Tutsplus explains schema.org and structured…

Read more

Improving the performance of our PHP based crawler original

by Freek Van der Herten – 2 minute read

Today a new major version of our homegrown crawler was released. The crawler is used to power our http-status-check, laravel-sitemap and laravel-link-checker packages. A new major feature is the greatly improved crawling speed. This was accomplished by leveraging multiple concurrent requests. Let's…

Read more

A magic memoization function original

by Freek Van der Herten – 4 minute read

Last friday Taylor Otwell tweeted an easy to use memoization function called once: Wanted a slick way to generalize class method memoization. Y'all don't even want to know how it works. ? ? pic.twitter.com/xRJAY1C14y— Taylor Otwell (@taylorotwell) November 4, 2016 Taylor was kind…

Read more

An opinionated tagging package for Laravel apps original

by Freek Van der Herten – 4 minute read

There are a lot of quality tagging packages out there. Most of them offer the same thing: creating tags, associating them with models and some functions to easily retrieve models with certain tags. But in our projects at Spatie we need more functionality. Last week we released our own - very…

Read more

A class to parse, build and manipulate URLs original

by Freek Van der Herten – 2 minute read

For several projects and other packages we need to manipulate URL's. Instead of coding the same URL class over and over again, we extracted URL manipulation to it's own package. Here are some code examples on how you can use it. $url = Url::fromString('https://spatie.be/opensource'); echo…

Read more

A Laravel package to store language lines in the database original

by Freek Van der Herten – 3 minute read

In a vanilla Laravel installation you can use language files to localize your app. The Laravel documentation refers to any string in a language file as a language line. You can fetch the value of any language line easily with Laravel's handy trans-function. trans('messages.welcome'); //…

Read more

A little library to deal with color conversions original

by Freek Van der Herten – 1 minute read

My colleague Seb needed to convert some color values in PHP. He looked around for some good packages, but there weren't any that fit the bill. So guess what he did? He just created a new package called spatie/color. Here's what it can do: $rgb = Rgb::fromString('rgb(55,155,255)'); echo…

Read more