Introduction
April turned out to be a pretty busy month for me in the web development world. I've been busy contributing to projects and releasing new versions of my own packages. There's also been some cool new packages released by other developers, a field guide released, and a new in-person Laracon event.
If you haven't read last month's issue, you can read Round Up: March 2022.
Laracon EU
On April 26th, Laracon EU took place in Amsterdam. Although I didn't attend it myself, I got to see plenty of what was going on through Twitter, and it looks like there were some really cool talks put out. I'm hoping that the talks will have been recorded and will be made available to watch online at some point in the future. I always find conference talks really interesting and useful for learning about new concepts.
BaseCode Released
Jason McCreary (the creator of Laravel Shift) released a free version of BaseCode. In Jason's words, BaseCode is a "field guide containing real-world practices to help you write code that's less complex and more readable".
It comes in 3 different tiers (lite, mid, and full), with the "lite" tier being completely free to download. All you need to do to download it is register with your email and a password.
The guide covers:
- Writing code that prioritizes readability by humans, not a computer
- Avoiding code rot with analysis and automation
- Untangling nested code to improve visual honesty and reduce reader overhead
- Introducing objects to encapsulate complexity
- Refactoring long blocks of code by understanding reading levels
- Making naming things easy with context and time
- Using code as the primary signal, not comments
- Avoiding unnecessary code by using reasonable return values
- Delaying the need to remove duplication with the Rule of Three
- Improving code flow with symmetry
- Deferring design decisions to reduce anxiety when writing code
I don't want to give away any of the content that's in the guide (you'll need to download it yourself), but I'm part way through reading it and have already learned some new things from it. No matter if you're a seasoned developer or brand new to the web development world, I'd definitely recommend grabbing yourself a copy of the guide!
New PHP Crawlers and Scrapers Released
During the month, two awesome (and unrelated) new crawling and scraping packages were released for PHP: crwlr/crawler and Roach. I've not had a project that needs either of these yet, but after looking through the code and documentation for them both, they look like they could be really useful in certain types of projects.
crwlr/crawler
The first package is crwlr/crawler
by Christian Olear. It's part of a collection of open source PHP packages that provivde tools for web crawling and scraping. The documentation is really well presented and the code examples in it make a lot of sense to read and understand.
Here's a quick, basic example from the documentation that shows you how you could extract data from articles on a web page:
First you'd need to create your own "crawler" class that extends a base class provided by the package:
1use Crwlr\Crawler\HttpCrawler; 2use Crwlr\Crawler\UserAgents\BotUserAgent; 3use Crwlr\Crawler\UserAgents\UserAgentInterface; 4 5class MyCrawler extends HttpCrawler 6{ 7 protected function userAgent(): UserAgentInterface 8 { 9 return BotUserAgent::make('MyBot');10 }11}
And then you'd be able to use that crawler like so:
1$crawler->input('https://www.example.com/articles'); 2 3$crawler->addStep(Http::get()) // Load the listing page 4 ->addStep(Html::getLinks('#artList .article a')) // Get the links to the articles 5 ->addStep(Http::get()) // Load the article pages 6 ->addStep( 7 Html::first('article') // Extract the data 8 ->extract([ 9 'title' => 'h1',10 'date' => '.date',11 'author' => '.articleAuthor'12 ])13 ->addKeysToResult()14 );15 16foreach ($crawler->run() as $result) {17 // Do something with the Result18}
I'm sure you'll agree that this code looks simple to understand and could achieve some pretty useful things. If you're interested in reading more about crwlr/crawler
you should check out the documentation or their blog.
Roach
The other package that was released was Roach PHP by Kai Sassnowski. In Kai's words, Roach is a "complete web scraping toolkit for PHP" that is heavily inspired by the Python package Scrapy.
The documentation provides an example that shows you how could extract the title and subtitles from a page in the Roach documentation:
1use RoachPHP\Http\Response; 2use RoachPHP\Spider\BasicSpider; 3 4class RoachDocsSpider extends BasicSpider 5{ 6 /** 7 * @var string[] 8 */ 9 public array $startUrls = [10 'https://roach-php.dev/docs/spiders'11 ];12 13 public function parse(Response $response): \Generator14 {15 $title = $response->filter('h1')->text();16 17 $subtitle = $response18 ->filter('main > div:nth-child(2) p:first-of-type')19 ->text();20 21 yield $this->item([22 'title' => $title,23 'subtitle' => $subtitle,24 ]);25 }26}
Although the approach used in Roach appears a lot different (at least at a first glance) to the approach taken in Crawler, I'm sure you'll agree that this code also looks readable and easy enough to understand.
If you're interested in finding out more about Roach, you can check out the documentation and the repo on GitHub.
My News
Favicon Fetcher Released
During April, I released a new package called Favicon Fetcher. It's a package that you can use in your Laravel projects to get favicon's from other websites. It ships with support for interacting with 4 different drivers for fetching the favicons, and also comes with functionality for caching and storing the favicons.
For example, to grab the favicon of my own website, the code would look something like this:
1use AshAllenDesign\FaviconFetcher\Facades\Favicon;2 3$favicon = Favicon::fetch('https://ashallendesign.co.uk');
As you can see, it's really straightforward and easy to use!
I'm going to be putting together a blog post very shortly to go into further detail about the package, and how you can use it. So, if you're not already subscribed to my newsletter, now would be a great time to subscribe so that you can be notified when I publish the post.
Short URL v7.0.0 Released
During April, I also released v7.0.0 of my Short URL package. It added the ability to create short URLs without prefixes, define middleware to run on the default short URL route, and change the key generator on-the-fly. If you're interested in reading more about the changes that I added to the package, check on my What's New in Short URL v7.0.0 article.
Laravel Config Validator v2.1.0 Released
At the beginning of April, I also released v2.1.0 of my Laravel Config Validator package. It included a really cool contribution from Francisco Madeira that changed all of the Artisan command output to now use Termwind. As a result of this, the command looks a lot cleaner now.
For example, here's how failure output for the command used to look before v2.1.0:
And, here's how the failure and success output for the command now look thanks to the new Termwind additions:
I think you'll agree that the new changes look much better!
If you're interested in reading more into the latest Laravel Config Validator changes, you can check out my What's New in Laravel Config Validator v2.0.0 & v2.1.0 article.
Open Source Contributions
Towards the beginning of the month, I made several pull requests to the Laravel framework and docs. Here are the pull requests that were successfully merged:
- laravel/framework
-
laravel/docs
- Added type to closure (#41966)
Laravel News Podcast Mentions
During April, I was listening to the latest episodes of the Laravel News podcast and heard that I had been mentioned in both episodes (exciting!!!).
One of the episodes mentioned Laravel Config Validator:
The other episode talked about my Using 'query()' in Laravel Eloquent Queries blog post:
Laravel Releases
During April, there were 9 Laravel versions that were tagged and released during the month: 8.83.7, 9.7.0, 8.83.8, 9.8.0, 9.8.1, 8.83.9, 9.9.0, 8.83.10, 9.10.0.
If you're interested in checking out the entire changelog for each of the releases, I recommend checking out the release notes for each one on GitHub. Here are the main additions that were added in the releases:
9.7.0
- Make whereBelongsTo accept Collection (#41733)
- Database queries containing JSON paths support array index braces (#41767)
- Fire event before route matched (#41765)
- Added to Illuminate/Http/Resources/ConditionallyLoadsAttributes::whenNotNull method (#41769)
- Added "whereIn" route parameter constraint method (#41794)
- Added Illuminate/Queue/BeanstalkdQueue::bulk() (#41789)
- Added Illuminate/Queue/SqsQueue::bulk() (#4178)
- Added String::squish() helper (#41791)
- Added query builder method whereJsonContainsKey() (#41802)
- Enable dispatchAfterResponse for batch (#41787)
8.83.8
- Added multibyte support to string padding helper functions (#41899)
9.8.0
- Added inbound option to CastMakeCommand (#41838)
- Added a way to retrieve the first column of the first row from a query (#41858)
- Make DatabaseManager Macroable (#41868)
- Improve Str::squish() (#41877, #41924)
- Added option to disable cached view (#41859)
- Make Connection Class Macroable (#41865)
- Added possibility to discover anonymous Blade components in other folders (#41637)
- Added Illuminate/Database/Eloquent/Factories/Factory::set() (#41890)
- Added multibyte support to string padding helper functions (#41899)
- Allow to use custom log level in exception handler reporting (#41925)
8.83.9
- Backport Fix PHP warnings when rendering long blade string (#41970)
9.9.0
- Add getAllTables support for SQLite and SQLServer schema builders (#41896)
- Added withoutEagerLoads() method to Builder (#41950)
- Added 'throw' method to PendingRequest (#41953)
- Configurable pluralizer language and uncountables (#41941)
9.10.0
- Add the ability to use alias when performing upsert via MySQL (#42053)
- Illuminate/Routing/Router::middlewareGroup() will support array of the middlewares (#42004, e6b84fb)
- Added Missing AsCommand attribute on schedule:list (#42069)
- Add the beforeRefreshingDatabase function to the Testing/RefreshDatabase trait (#42073)
- Added doesntExpectOutputToContain assertion method (#42096)
- Added a findOr method to Eloquent (#42092)
- Allow extension in Illuminate/View/Compilers/Compiler.php (68e41fd)
- Support 'IS' and 'IS NOT' PostgreSQL operators (#42123)
- Added str and string methods to Illuminate/Http/Concerns/InteractsWithInput (c9d34b7)
- Added methods to append and prepend jobs to existing chain (#42138)
Conclusion
Hopefully, this post should have shown you at least one cool new thing from the Laravel world in April 2022.
If you enjoyed reading this post, I'd love to hear about it. Likewise, if you have any feedback to improve the future ones, I'd also love to hear that too.
If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter below.
Keep on building awesome stuff! 🚀