A curated collection of useful PHP snippets that you can understand in 30 seconds or less.
A GitHub repo containing easy to understand PHP snippets.
Read more [github.com]
Posts tagged with github
A GitHub repo containing easy to understand PHP snippets.
Read more [github.com]
? When linking to a file on @github, press 'y' to transform the URL to a SHA-based reference. That way, the link will remain stable even if the underlying file changes in future commits. pic.twitter.com/cAsupEk7tV
— Derrick Reimer (@derrickreimer) 30 mei 2018
Read more [twitter.com]
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.
"Freek publishes a super resourceful and practical newsletter. A must for anyone in the Laravel space"
At Spatie we have over 180 public repositories. Some of our packages have become quite popular. We're very grateful that many of our users open up issues and PRs to ask questions, notify us of problems and try to solve those problems, ... Most of these issues and PRs are handled by our team. But…
Marcel Pociot, author of BotMan, used GitHub and Google BigQuery to look up some interesting numbers about the PHP repos in 2017.
It's this time of the year again - the end of the year is coming up fast, so why not step back and take a look at what we, as a PHP community, have achieved this year?
For these statistics, I used the free GitHub Archive data in combination with Google BigQuery, which lets you process 1TB of data per month free of charge.
So let's take a look at some numbers.
http://marcelpociot.de/blog/2017-12-21-a-php-year-in-review
My team is mentioned in the article too. Pretty proud of this!
As you can see, Spatie - a company doing a ton of open source projects - is on this list 16 times. Well done ???? !
? Tip of the Day
— Jacob Bennett (@JacobBennett) September 26, 2017
Press T while in a Github Pull Request to get a fuzzy search for all changed files. ? pic.twitter.com/LOQsJ9z0vZ
Read more [twitter.com]
A few months ago I installed a command line utility called hub. I'm really fond of it. It's aim is to make it easier to interact with GitHub from the commandline. It's a wrapper around the standard git command. Once it's installed you can do stuff like this (take from the manual page) # clone your…
Ondřej Mirtes, author of PHPStan lists some great tips to make a side project succesful.
Most developers have side projects. That's how we try out new things or make something that we miss on the market or in our dev stack. But most side projects end up unfinished and never actually see the light of day. And even if a developer builds up the courage to show his work to the public, he quickly finds out that just publishing a repository doesn't actually bring the masses to his doorstep. ... In this article, I'd like to share with you what I did to make sure that the project doesn't end up in the dustbin of history. I will concentrate on open source software, but the following advice may as well apply to any creative endeavour.
Serial blogger Matt Stauffer wrote a good tutorial on how use forked repos.
I just recently joined a new open source project, and there were a few folks on the team who weren't familiar with how to contribute to an open source project by forking your own copy, so I wrote this up for the docs of that project. I figured I'd also share it here.If you join a new open source project, it's very likely that you won't get direct access to push commits or branches up to the repository itself. So, instead, you'll fork the repo, make the changes on your version of the repo, and then "pull request" your changes back to the original.
Here are the steps to take.
https://mattstauffer.co/blog/how-to-contribute-to-an-open-source-github-project-using-your-own-fork
At Spatie we use GitHub for both our client projects as our open source code. So in our day to day work we often have to open the browser to view issues of a repo or review pull requests.
Paul Irish, a well known developer and part of the Google Chrome team at Google, made a nice bash script to quickly open up a GitHub page from your terminal. If you're on a path inside a git repo and type "git open" that'll open up the corresponding page on GitHub.
The command also supports, amongst others, repos hosted on GitLab.com and Bitbucket.
Laravel hero Matt Stauffer has a new article on his blog where he talks about using a social network site login as the primary login for your application.
https://mattstauffer.co/blog/using-github-authentication-for-login-with-laravel-socialiteLaravel's Socialite package makes it simple to authenticate your users to Facebook, Twitter, Google, LinkedIn, GitHub and Bitbucket. You can authenticate them for the purpose of connecting their pre-existing user account to a third-party service, but you can also use it as your primary login mechanism, which we'll be talking about here.
I'm working on a new little micro-SaaS that is purely dependent on GitHub in order to operate, so there's no reason to set up any user flow other than just GitHub. Let's do it.
It's quite surprising that Laravel didn't have a changelog until today. Community member Till Krüss added one and will probably maintain it in the foreseeable future. The changelog can be viewed on GitHub.
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…
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:Check it out: https://github.com/myclabs/php-enumThis Enum class is not intended to replace class constants, but only to be used when it makes sense.
- 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)
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:
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…
Google Tag Manager allows you to add tags to your site without editing the site code. A tag, in Google parlance, is a snippet of JavaScript that sends information to a third party, such as... Google. There are tags to install things like Analytics, Adwords or others on your site . To use GTM you…
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…
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…
Nearly all greenfield projects at Spatie are built with Laravel. A few months ago I made laravel-analytics, a package to fetch data from Google Analytics.
Spatie exists for more than ten years so there are quite a few legacy projects as well. In those projects there is also a need to display some analytics data in the admin-section. Recently Google retired some of their older API's. Unfortunately our old solution to fetch GA data stopped working.
To resolve this problem my colleague Sebastian created a framework agnostic version of laravel-analytics that works with PHP 5.3. If you also have a legacy project that needs some data from Analytics, you can install the package via composer.
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…