megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

Fetch selected files from your remote repository

Wanted to fetch a specific file from your Git repository except that the repository doesn't exist in your local machine?

Try this out:

git init
git remote add origin <your_repo_link>.git
git fetch
git checkout <your_branch_name> -- </path/to/file>

After executing these commands, you should be able to see the selected directory/file in your project directory.

Hope you found this useful!

How to recover from an errorneous forced git commit?

If you're the type of person who types git push -f origin master, please don't do that as it might overwrite your entire branch. I'm saying this because I did this once and I thought I lost all the files.

Luckily, I was a bit relieved as git is a VCS (Version Control Software), which means the files are most likely not deleted. This is when I came across git reflog command.

According to the Git manual, this is what it does:

Reference logs, or "reflogs", record when the tips of branches and other references were updated in the local repository.

This is a life-saver especially if you wanted to return back to the previous point in time. Here's how I recovered my files back again:

  1. Type git reflog show remotes/origin/master
  2. Find and make note of the previous commit hash.
  3. Create a new branch with using the previous commit hash like this: git branch <new_branch_name> <previous_commit_hash>
  4. Then finally, push the files to the new branch: git add . && git commit -m "pushing recovered files" && git push origin <new_branch_name>
  5. Checkout to the newly created branch: git checkout <new_branch_name>
  6. Delete the corrupted branch and replace it with the newly created branch that contains your restored files.

If I didn't discover this, I don't really know what I would have done to recover those files.

References

Hope this helps you out!

Use MySQL 8.0 with Native Password Authentication

Last month, I was configuring an Ubuntu Server to deploy a client's project that uses MySQL 8.0 and PHP 7.2. So, I installed the necessary dependencies and finally installed MySQL 8.0, created a new schema and imported the database tables required for the project.

Next, I typed the project URL and ran into this error:

Unable to load plugin 'caching_sha2_password'

If you're running PHP 7.2 and facing this error, you should know that PHP 7.2 doesn't support native password authentication by default. But it's a simple fix, all you have to do is either one of the following:

  1. Alter the current user's authentication to native password
  2. Create a new user with native password authentication

Alter the current user's authentication to native password

ALTER USER 'your_user'@'your_server_host' IDENTIFIED WITH mysql_native_password BY 'your_password';

Create a new user with native password authentication

CREATE USER 'your_user'@'your_server_host ' IDENTIFIED WITH mysql_native_password BY 'your_password'

For the changes to take effect, you need to reload the privileges by typing the following:

FLUSH PRIVILEGES;

Hope this helps you out!

Clearing cache on a Shared Hosting Server

Hosted your website on a Shared Hosting Server and got limited access to clear the cache on your project?

Open up routes/web.php and create this route:

<?php
Route::get('/clearcache', function(){
    \Artisan::run('config:clear');
    \Artisan::run('cache:clear');
    \Artisan::run('view:clear');
    \Artisan::run('route:clear');
    \Artisan::run('config:cache');
});
?>

Just type the URL and it will clear all existing cache from the project.

Hope this helps you out!

Convert numbers from English to Arabic in PHP

If you're a developer working in the Middle East, it's quite common that you'll work on a project that bilingual, in our case, it's english and arabic.

In my opinion, it's not aesthetically pleasing and logical to have english numbers in arabic text, so, write a simple helper function to convert the numerals from english to arabic:

<?php
function convertEnglishToArabicNumerals($str) {
    if (\App::getLocale() == 'ar') {
        $westernArabic = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
        $easternArabic = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩');
        $str = str_replace($westernArabic, $easternArabic, $str);
    }
    return $str;
}
?>

And since most browsers can handle RTL, you don't have to worry about how the arabic numerals are being displayed in your application.

Convert string to variable in PHP

I read about variable variables in PHP's official documentation.

Here's a sample:

<?php
    $a = "hello";
    // Remove special characters and tags to prevent it from crashing.
    $foo = preg_replace('/[^a-zA-Z0-9\s]/', '', $$a);
    echo $foo;
?>

Not sure if this is a good practice but it sure gets the job done!

How to resolve the "Failed to clear cache. Make sure you have the appropriate permissions." error

This error is annoying and mostly happens if the data directory is missing under the storage/framework/cache/data directory. For some reason, this folder doesn't exist by default.

To resolve it, just manually create the data directory under the storage/framework/cache/data directory and it should fix the issue.

How to exclude certain slugs in Laravel

Using plain Regular Expressions, you can exclude certain slug from your routes, try adding the following to your routes/web.php file:

<?php
    Route::match(array('GET', 'POST'), '/{slug}', 'YourController@index')->name('page')->where('slug', '^(?!pattern).*$');
?>

Hope you found this useful!