Lena Reinhard – A Talk about Nothing
This may well be one of the most important talks about the future of our culture.
This may well be one of the most important talks about the future of our culture.
While Form Request Validation in Laravel 5 usually does exactly what it’s supposed to do, I recently had to append a location hash to the redirect URL. Turns out that’s as simple as overwriting the FormRequest
’s getRedirectUrl()
-method:
|
|
Usually, when setting up my controllers in a Laravel 5 application, I end up creating one or several base controllers for different entry points (e.g. APIController, FrontendController, …) While I deem this a good practice, it can lead to problems with Laravel’s otherwise fantastic Dependency Injection. To illustrate, take a look at the following:
|
|
As some IDEs and testing tools would say: There are multiple problems!
BlogController
’s constructor does not call the parent.Not calling the parent constructor which probably does important things? Bad idea. Could be fixed with changing the constructor of BlogController
to:
|
|
But then again that’s not quite right too, is it? It should be more along the lines of this:
|
|
Which is ugly as hell. Let alone remembering that for all of the other descendants of the FrontendController
.
Right around the time I realized that I didn’t want to write that second version even once, I thought “hey, Eloquent models have this boot() method. That could be a way to go.”
Said and done. I refactored my code to this:
|
|
Which, of course, did not work. And - as too often is the case - the error was all too obvious to me once I had committed the crime. So, after a while, I figured that if Laravel can resolve class names to inject from type hints, why shouldn’t I. Thus followed some research on method reflection and I ended up with this:
|
|
And this, finally, works just as expected. One thing to note though: Bad things will happen if you have anything but type-hinted to-be-resolved classes as attributes to the boot()
method.
Sometimes, when designing data models, it turns out that access to a property of a related model might be handy from the current model. For the case of one-to-many relations, Laravel’s Eloquent already comes with the hasManyThrough()
-method. But what if it’s a one-to-one relation? And what if the attribute one wants to have access to is up a few levels in the model hierarchy? Well, turns out that can be pretty easily done, too.
Assuming, three models A
, B
, C
all related, e.g. B
has a_id
, C
has b_id
and C
would benefit from direct access to A
, that can be achieved through Eloquent with the following code:
|
|
Of course, one might also use this to fetch a specific attribute instead of creating a complete relation. Keep in mind though, that this may cause serious performance issues since the ORM has to fetch all referenced entities before it can return anything.
Although Prism.js provides a very good autolinker plugin, it did not quite fit my needs for a recent project. Here’s what I came up with as a stupidly simple replacement that’s not as sophisticated (i.e. does not do links in Markdown and such stuff) but works perfectly well with to-be-highlighted JSON.
|
|
Just add this to your site’s scripts and everything will be better.
Showing the existing tables in a database can be really handy. Unfortunately, MySQL’s SHOW TABLES;
is not a standard sql command. Although it quite honestly should be. Thus, for quick reference, here goes:
SHOW TABLES;
SELECT name FROM pg_catalog.pg_tables;
SELECT name FROM sqlite_master WHERE type = 'table';
There probably is a SELECT
-Syntax for MySQL too, but who really cares? The one above is so much shorter in any case.
Codeception supports several layers of selecting which tests to actually run when you invoke codecept run
. This can - among other times - be extremely useful when you’re trying to fix a bug and you really only need that one single test method to run or when you’re just doing API development and don’t need to check all of your UI integration tests for the time being.
|
|
Among other things, I am currently working on rewriting the text handling code of this website into something cleaner and more usable. In the current version, the text manipulation is a big non-deterministic ugly mess of regular expressions and other not-so-niceties. For the new version I decided on going with a modular approach and - more importantly - doing the actual replacements I want to apply to the Markdown before showing it with DOM manipulations.
One of the wonderful but often forgotten features of PHP’s DOM library is that it offers the DOMDocumentFragment
not-quite-standard part of the DOM Level 1 spec. John Resig once wrote a wonderful piece on the JavaScript version of this. As John points out, working with fragments can lead to significant performance improvements. I have to admit that I did not conduct performance testing to conclusively say that this also applies to PHP but it at least feels faster. Additionally, these fragments don’t come with all the garbage of full blown HTML documents that the default DOMDocument
tends to deliver.
In the case of having HTML stored in a string variable, wrapping things is almost too easy:
$html = "<p>I will be wrapped</p>"; $html = "<div>".$html."</div>";
I bet you wrote code like that at least once. Regardless of the programming language. But this is not good code. To me, the most obvious flaw is the possibility to miss things. What if you forget the slash in the second div. Happened to the best of us. Followed by hours of trying to find out why there’s suddenly this weird gap in the page.
When manipulating HTML inside the DOM classes, malformed output cannot happen. Exceptions can, but that’s the point. You’ll know when something goes wrong before you already shipped it to the client. Unfortunately, working with objects usually means a little more code than string manipulation. In return, the code allows for telling a better readable story.
The below code snippet is extracted from the above mentioned text manipulation library I am working on. Once finished (which hopefully happens in the coming days/weeks), this will be available on GitHub. There will of course be a Laravel integration. The presented method accepts a node (which technically already could contain children) in which the given fragment will be wrapped. Poof, magic. Requires PHP >=5.4, though.
|
|