megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

Clear a file directory using Filesystem in Laravel

I was working on a project that dealt with generating large .zip exports and as a result, the storage/exports directory, which was used to store all the .zip exports, ended up going all the way up to a whopping size of 10 gigabytes! 😮

I resolved it by calling the Filesystem package in my controller:

<?php
use Illuminate\Filesystem\Filesystem;
?>

Then simply, create a new instance and define the directory you wanted to clear:

<?php
$folder = new Filesystem;
$folder->cleanDirectory('storage/exports');
?>

Hope this helps you out!

How to move a running process to the background?

Pause the current process using Ctrl + Z, which will send it to the background and then type bg to allow the process to run in the background.

Alternatively, you can add a & after the desired process you wanted to execute and it'll automatically execute it as a background process.

Hope this helps you out!

Get pagination data by page number

By default, Laravel's paginator checks the value of the page based on the query string and uses that to display the results and also, it generates links to previous and next pages as well.

The paginate() method takes the following parameters by default:

<?php
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null);
?>

So, if you want to fetch the pagination data of a specific page, then just write this:

<?php
$pageNumber = 5;
$data = ExampleModel::paginate(5, ['*'], 'page', $pageNumber);
?>

Hope you found this article useful!

Resolve cURL error 60: SSL certificate problem on localhost

You'll get this error when you're hosting a Laravel project with using HTTPS/SSL protocol on localhost or 127.0.0.1:

cURL error 60: SSL certificate problem: unable to get local issuer certificate (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)

I read some article on trying to install cacert.pem authorization certificate on your WAMP/XAMPP setup but it didn't really work out for me as I was running out of time.

So, I did a little digging and learnt that I can just modify the verify flag to false in the vendor/guzzlehttp/guzzle/src/Client.php file:

<?php
$defaults = [
    'allow_redirects' => RedirectMiddleware::$defaultSettings,
    'http_errors'     => true,
    'decode_content'  => true,
    'verify'          => false, // changed it to false
    'cookies'         => false
];
?>

By changing to it false, you'll not face that error again but please keep in mind, you should do this only if you're developing on localhost.

Happy coding!

How to resolve the "No application encryption key has been specified" error

Are you facing the same error as this article's title? Then this article might help you out.

If you're developing applications using HTTP/SSL Protocols (regardless, you should!) on Laravel, by default, you'll be using Laravel's encrypter but in it's official documentation, it says:

Before using Laravel's encrypter, you must set a key option in your config/app.php configuration file. You should use the php artisan key:generate command to generate this key since this Artisan command will use PHP's secure random bytes generator to build your key. If this value is not properly set, all values encrypted by Laravel will be insecure.

Just execute the following command in your root directory:

php artisan key:generate

If the error still persists, try clearing the cache by doing the following:

php artisan config:cache
php artisan cache:clear
php artisan config:clear

Hope you found this article useful!

How to filter requests with errors on Chrome's DevTools

Tired of trying to look for errors in DevTools by scrolling a long list of HTTP responses?

Open up your Chrome DevTools and hit the Network tab and then try filtering the responses by typing the status-code property in the filter box.

It comes in handy when you want to narrow down to a specific list of HTTP responses. For instance, if you're looking for responses with error 404, you can filter it by typing like this: status-code:404.

Another trick is, you can filter out the responses that you don't want to see by just adding a hyphen like this: -status-code:200.

Hope this tip helps you out!

Enable HTTPS/SSL on WAMP Server

Building a website with HTTPS/SSL in mind can help resolve a lot of problems when you're going to deploy it on production server.

So, here's a small tutorial on how to enable HTTPS mode and install SSL certificate on your local WAMP Server.

Note: This tutorial assumes that you have a Windows PC and have installed WAMP Server 3.2.0 in your local system, if not, download it from here.

Download OpenSSL

Based on your system's architecture, you can download either a 32 or 64-bit installer. You can find the latest version of OpenSSL from here. While installing, please make sure all the options selected are default.

Generate SSL Key and Certificate

Open your terminal or command-line prompt and navigate to the following folder:

cd "C:\Program Files\OpenSSL-Win64\bin"

Next, you need to create a private key. While generating a private key, you'll have to enter a passphrase, it can be anything but make sure that you can remember it for the next step 😂.

Execute the following command:

openssl genrsa -aes256 -out private.key 2048

Good, now let's generate our certificate and in this step, you'll be prompted with several questions. You can fill as per your wish or just hit "Enter" to leave it as default. The only thing that matters is Common Name and this should named as localhost

Execute the following command:

openssl req -new -x509 -nodes -sha1 -key private.key -out certificate.crt -days 36500

Move the certificate and key to Apache's directory

Create a folder named keys and move both private.key and certificate.crt to this directory: C:\wamp64\bin\apache\apache2.4.41\conf.

Modify your httpd.conf file

You have to uncomment 3 lines from C:/wamp64/bin/apache/apache2.4.41/conf/httpd.conf:

LoadModule ssl_module modules/mod_ssl.so
Include conf/extra/httpd-ssl.conf
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so

Modify your httpd-ssl.conf file

Go to C:/wamp64/bin/apache/apache2.4.41/conf/extra/httpd-ssl.conf and modify the following parameters:

DocumentRoot "c:/wamp64/www"
ServerName localhost:443
ServerAdmin admin@youremail.com
ErrorLog "${SRVROOT}/logs/error.log"
TransferLog "${SRVROOT}/logs/access.log"
SSLSessionCache "shmcb:${SRVROOT}/logs/ssl_scache(512000)"
SSLCertificateFile "${SRVROOT}/conf/keys/certificate.crt"
SSLCertificateKeyFile "${SRVROOT}/conf/keys/private.key"
CustomLog "${SRVROOT}/logs/ssl_request.log"

DocumentRoot is the location of where your website files are located. ServerName can be anything but preferable to use localhost.

Restart your WAMP Server

Just restart your WAMP Server for the changes to take effect. If the WAMP icon turns green, you're good else, an typo or syntax error must have occurred.

Hope you found this tutorial useful! 😀

Accessing WAMP Server from other computers on LAN

Whenever you develop a website using the LAMP or WAMP stack, you'll want to access the website, locally, via different systems and devices solely for testing purposes.

Modify your VirtualHosts configuration

Setting up your WAMP Server for LAN access only requires you to tweak your VirtualHosts configuration file found in C:\wamp64\bin\apache\apache2.4.41\conf\extra\httpd-vhost.conf

<VirtualHost *:80>
  ServerName example.test
  ServerAlias example.test
  DocumentRoot "c:/wamp64/www/yourprojectname"
  <Directory "c:/wamp64/www/yourprojectname/">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

Oh, if you're using Linux, you'll find the file 000-default.conf in /etc/apache2/sites-available directory.

Update your hosts file

If you type the URL of the website that you want to visit, the computer will first refer to the hosts file and then it'll go out to fetch DNS information. So, if you want http:\\example.test url to point to your local system, you just have add it in your hosts file:

Open the hosts file(C:\Windows\System32\drivers\etc\hosts) using a text editor and add this line to your hosts file:

127.0.0.1   example.test

Once done, save the file and restart your WAMP Server and you're all set to go!

Hope you found this tip useful!