PHP
No, Utility Classes Aren't the Same As Inline Styles
An excellent post on the subject by Sarah Dayan. Read More
An excellent post on the subject by Sarah Dayan. Read More
Tighten employees share encouragement for anyone struggling through the pandemic. Read More
[AdSense-A]
Sometimes it is not clear what the responsibility of a certain class is. This can be solved by adding a suffix to the class name.
(more…)Thanks to Doeke Norg personal access tokens became more flexible in Sanctum. In this blog post, he explains how you might use the new functionality Read More
[AdSense-A]
Our team has released a new package called file-system-watcher. As the name implies, this package can watch changes in the file system and let you act on those changes.
Here's a simple example that allows you to run some code when a new file gets added.
use SpatieWatcherWatch;
Watch::path($directory)
->onFileCreated(function (string $newFilePath) {
// do something...
})
->start();
After calling start
, the watcher will start a never-ending loop. When a file is created in the given $directory
, the closure passed in onFileCreated
will be executed.
There are methods for each kind of file change onFileCreated()
, onFileUpdated()
, onFileDeleted()
, onDirectoryCreated()
, onDirectoryDeleted()
.
Pretty simple, right?
Under the hood, this package leverages (https://github.com/paulmillr/chokidar), a popular JS library that can efficiently handle file system changes without requiring much memory or CPU.
To see a demo of the package and learn how we integrated our PHP code with a JS package, watch this fragment of a stream in which I introduced our file-system-watcher.
There are a few more options available in file-system-watcher. Head over to the readme on GitHub to learn more.
Be sure also to check out the packages our team has created previously. I'm sure there is something that you could use in your next project.
(more…)An in-depth guide on offset and cursor pagination in Laravel, and the pros and cons of each. Read More
Jordi is back with another post containing interesting statistics around PHP usage. Read More
[AdSense-A]
You can make your code more readable by moving all your exception messages to dedicated classes. We using this technique in all our projects and packages.
(more…)[AdSense-A]
Out of the box, Laravel comes with the ability to generate "signed" URLs. These URLs have a hash in their query string that verifies that the URL was not modified.
At Flare, we use these signed URLs to add action links in mail notifications. The action links allow users to snooze and resolve errors right from the mail without having to be logged in. Pretty convenient!
My buddy Dries Vints noticed a slight drawback. He got a mail from Flare that contains these action links. A few hours after the mail arrived, he clicked one of the action links. This is what he saw.
This error screen is confusing: you might think that the links in the mail are invalid. To keep things secure, we use a short lifetime for our signed URLs. Dries got this screen because the link had expired.
We can improve on this by creating a dedicated error message when clicking expired or invalid links. Luckily, this is not that difficult.
When you try to validate a signed URL and the validation fails, Laravel will throw a dedicated exception IlluminateRoutingExceptionsInvalidSignatureException
In your exception handler, you can listen for that exception and render a dedicated view.
// in app/Exceptions/Handler.php
use Illuminate<span class="hljs-title">Routing<span class="hljs-title">Exceptions<span class="hljs-title">InvalidSignatureException;
public function render($request, Throwable $exception)
{
if ($exception instanceof InvalidSignatureException) {
return response()->view('errors.link-expired')->setStatusCode(Response::HTTP_FORBIDDEN);
}
<span class="hljs-comment">// ...</span>
}
With that code in place, this is what Dries will see when clicking another expired link in the future.
And that is all there is to it. To avoid confusions for your users, I highly recommend setting up a dedicated error message when using signed URLs.
Thanks for bringing this to my attention, Dries.
(more…)