package

All my posts about package.

Some Laravel 5 compatible packages

Recently some of the Laravel-packages I built at Spatie were updated for Laravel 5.

spatie/laravel-glide This package enables you to generate image manipulations on the fly and generate URL's to those images. These URL's will be signed so only you will be able to specify which manipulations should be generated. Every manipulation will be cached. Under the hood, Glide is being used.

spatie/searchindex This is an opinionated package to store and retrieve objects from Elasticsearch. It was tailormade for a project I was working on and only provides the functionality that I needed. If you need full control over elasticsearch via PHP, take a look at the official low-level client.

spatie/googlesearch This package can fetch results from a Google Custom Search Engine. It returns an array with searchresults.

eloquent-sortable This package provides a trait that adds sortable behaviour to an Eloquent model. The value of the ordercolumn of a new record of a model is determined by the maximum value of the ordercolumn of all records of that model + 1.

 

A big thank you goes out to Matthias De Winter, who is helping me update these packages.

Read more

The missing tail command for Laravel 5 original

by Freek Van der Herten – 1 minute read

Last week Laravel 5 was released. Unfortunately that handy command for tailing the logs that you know and love from Laravel 4 was not included. The laravel-tail package brings it back. You can install it via composer: composer require spatie/laravel-tail After that you'll have to register the…

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.

An activity logger for Laravel 5 original

by Freek Van der Herten – 1 minute read

The activitylog-package provides a very easy way to log activities of the users of your app. Today I took the time to make it compatible with Laravel 5. Logging some activity couldn't be easier: [code] /* The log-function takes two parameters: - $text: the activity you wish to log. - $user: optional…

Read more

Generate image manipulations in a Laravel project

Earlier this month Glide joined The League of Extraordinary Packages. This package provides an easy way to generate image manipulations on the fly. Within a couple of hours I had integrated this wonderful package into both my company's legacy and current CMS.

The current CMS is based on Laravel. The Glide-integration is now available as a package on github: freekmurze/laravel-glide.

Installation

Nothing too fancy, you’ll have to:
  • use composer to pull in the package
  • install a service provider
  • install a facade
  • publish a config file
Read the specifics on Github.

Usage

Assuming you've got an image named `kayaks.jpg` in `app/storage/images` (this directory can be customized in the config file) you can use this code in a blade view:

<img src=&quot;{{ GlideImage::setImagePath('kayaks.jpg')->setConversionParameters(['w'=> 50, 'filt'=>'greyscale']) }}&quot; />

The function will output an URL to a greyscale version of kayaks.jpg that has a width of 50 pixels. As soon as the URL gets hit by your browser, the image will be generated on the fly. The generated image will be saved in app/storage/glide-cache (= the cache directory specified in the input file).

The generated URL will also be signed to prevent jerks from generating unwanted manipulations.

The setConversionParameters-function accepts an array with conversion options. You can take a look at the image API of Glide to see which options are available.

It's also possible to generate an image directly on the server:


GlideImage::setImagePath('kayaks.jpg')
    ->setConversionParameters(['w'=> 50, 'filt'=>'greyscale'])
    ->save($fileWhereImageManipulationMustBeSaved);

Read more

Using Elasticsearch in Laravel original

by Freek Van der Herten – 1 minute read

Elasticsearch is a lightning quick open source search server. It's java-based (but don't let that scare you) and can index and search documents. If you want to know more about it, watch this excellent talk by Ben Corlett. https://www.youtube.com/watch?v=waTWeJeFp4A For a recent Laravel project I had…

Read more

HTTP based image manipulations

Glide is a wonderfully easy on-demand image manipulation library written in PHP. It’s straightforward API is exposed via HTTP, similar to cloud image processing services like Imgix and Cloudinary. Glide leverages powerful libraries like Intervention Image (for image handling and manipulation) and Flysystem (for file system abstraction).
http://glide.thephpleague.com/

It comes with the ability to secure image URL's using a private signing key. I'm a bit worried about flooding the server but the owner of the package states that

... the cache filesystem + caching in league/flystem (with Redis/Memcache) should (at least this is the case in my testing) result in a speedy response...
There's also a risk that over time your cache will become gigantic. The maintainers of the package are already discussing possible solutions.

Read more

Automatically reject packages with known security vulnerabilities

This package ensures that your application doesn't have installed dependencies with known security vulnerabilities.

...

The checks are only executed when adding a new dependency via composer require or when running composer update: deploying an application with a valid composer.lock and via composer install won't trigger any security versions checking.

https://github.com/Roave/SecurityAdvisories

Awesome idea! It works by leveraging the "conflict"-property in the composer.json-file of the package.

Read more

Scan a https-site for mixed content

Last week Bramus presented a cool mixed content scanner on his blog. Just for fun/as an excercise I made my own version.

The core scanner is mostly a copy of Bramus' code but there are a few noticeable differences. My version:

  • can be installed via composer
  • uses the Symfony console component
  • uses Guzzle instead of naked curl
  • can write the scanresults as json to a file
You can install the scanner with this command: ``` composer global require spatie/mixed-content-scanner ```

You'll find the instructions how to use it on github.

Read more

Excel exports

Evan Miller makes the case for offering xls-exports instead of csv-exports.

Most people of your website’s users are also Excel users. When they export their data as CSV, they’ll probably just bring it into Excel first to have a look around. You should probably offer an explicit XLS export, and take it seriously.
http://www.evanmiller.org/please-offer-an-excel-export-option.html

An xls-file is capable of things that 'll never be possible in csv:

  • freezing the header row
  • formatting important cells
  • multiple sheets in one file
  • specify numer formatting
  • formulas
In the projects I've been working on I've been doing this for a couple of months. A nice Laravel package you can use is Laravel Excel.

This is how you export all values from a given repository that returns Eloquent models:


Excel::create($pathToFile, function($excel) {

    $excel->sheet('The sheet name', function($sheet) {
        $sheet->freezeFirstRow();

        $sheet->cells('A1:Z1', function($cells) {
            $cells->setFontWeight('bold');
            $cells->setBorder('node', 'none', 'solid', 'none');
        });

        $sheet->fromModel($this->yourOwnRepository->getAll());
    });

})->export('xls');

The result is an excel-file with the header row frozen and underlined and it's values in bold.

Read more

Inside Composer's speed boost

If you've been following the news, you'll have noticed that yesterday Composer got a bit of a speed boost. And by "bit of a speed boost", we're talking between 50% and 90% reduction in runtime depending on the complexity of the dependencies. But how did the fix work? And should you make the same sort of change to your projects? For those of you who want the TL/DR answer: the answer is no you shouldn't.
http://blog.ircmaxell.com/2014/12/what-about-garbage.html

Read more