Tag: PHP

Don’t include external JS for custom content example Goodreads

On many websites, you can have content embedded from other sites and one way that most external sites will offer will be to include a script that’s hosted on their website.

But if all that script does is including some remote content there are better ways to get that, that will provide customizations and performance.

For example, the website Goodreads offers such an embed JS with a list of the latest books you read, and you must include the script where you want the content to be shown. But that has a long list of drawbacks: the script uses the old API document.write, is a script and can’t be loaded in AMP, the scripts write very old style HTML with tags like center, attributes like border which again is not valid AMP, then also that script will be loaded for every request, so you’ll have one additional request. View full article

Build a Simple WordPress commenting system on top of the native one with AMP support

There are a lot of commenting system plugins out there but most of them come with a cost, and sometimes just building a simple WordPress commenting system on top of the native commenting system can be enough.

The main features of this system will be:

  • Comments will be only loaded if the user clicks a button
  • Comments will be submitted and fetched by JS
  • Comments will have paginations fetched by JS
  • Comments will have some JS interactivity
  • Comments will have the reply feature enabled
  • The comment system will have an AMP implementation
  • The comment system will have a no JS implementation
  • Comments will work the same on AMP with our own script

The first thing will have to do for our system is to make some custom REST API for the JS to fetch. View full article

Async Task without Queue or E-loop PHP

Most react-PHP code I’ve seen in the open that is not extension-based (doesn’t extend the PHP core language) uses some kind Event-Loop, something similar to how many technologies or languages(I.e. JavaScript) implement concurrency that doesn’t support threads.

And historically on single-core CPU’s threads practically transformed into loops underneath. But the main idea is that in many languages, an async behavior is very easily achieved, maybe you need a keyword, maybe you need a special class, but in general, it is simple.

Now if you want the easiest way to do something asynchronous in PHP it will be a little costly, you can do that by spawning a new PHP process directly from PHP. View full article

Simplest way to reduce the page number links in Laravel

Recently and currently(Laravel 5.6) Laravel is using Bootstrap-4 and they’ve updated all their views accordingly, and when you want to use Laravel’s pagination feature you may have a bit of a problem in the mobile world as there are too many page links to be shown in an esthetic way on a mobile screen.

So you can do a lot of things to resolve this problem but the simplest way available is to change the view which defines the links listening.
First, we need to publish that view, this will force Laravel to use the published view, not the one residing in the app-source/vendor directory.
We do that with the following command:

php artisan vendor:publish --tag=laravel-pagination

You should immediately see after executing the command the following:

Copied Directory [\vendor\laravel\framework\src\Illuminate\Pagination\resources\views] To [\resources\views\vendor\pagination]
Publishing complete.
View full article