megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

Insert mode

I get it, a lot of people are confused on how to start typing on VIM. All you have to do is press the A key, the editor will be switched to insert mode.

Press ESC key to switch back to normal mode.

Quit VIM

One of the most infamous issues that new users of VIM faces when using it for the first time. Press :q to quit VIM

If you've worked on a file and want to save and quit? Just type :wq!.

I hope that solved your quitting issue with VIM.

Save a file

Want to save your work? Type :w

Type :wq! to save your file and quit VIM at the same time.

Set up reCaptcha in Laravel

I mean, come on, you need to have a reCaptcha in your forms, no matter what.

Here are the steps:

1. Install using Composer

composer require anhskohbo/no-captcha

2. Add provider and alias to configuration

Open your config/app.php file and add this to your providers array:

<?php
Anhskohbo\NoCaptcha\NoCaptchaServiceProvider::class,
?>

And this to your aliases array:

<?php
'NoCaptcha' => Anhskohbo\NoCaptcha\Facades\NoCaptcha::class,
?>

3. Publish configuration

php artisan vendor:publish --provider="Anhskohbo\NoCaptcha\NoCaptchaServiceProvider"

4. Add sitekey and secret key to .env file

Open your .env file and add this:

NOCAPTCHA_SITEKEY=yoursitekey
NOCAPTCHA_SECRET=yoursecret

How to use it?

Now, you can use it in your validator using like this:

<?php
$validate = Validator::make(Input::all(),[
    'g-recaptcha-response' => 'required|captcha'
]);
?>

Set up a new repository

Are you new to using Git? Then this is for you.

1. Set up a Git account

Go to GitHub and create your account with your email address.

2. Create a repository

Just create a new project with whatever you wanted. For this, you can just create git-starter-repo or something like that.

3. Initialize git in your project directory

Now, go to your project directory and initialize git by doing the following:

git init

4. Add your files

Add your files to the repository by doing the following:

git add .

Or if you want add selected files:

git add file_1 file_2

5. Commit your changes

Before you push your changes, you need to write a message about what changes are done:

git commit -m "this is my first commit"

6. Link your project to the repo

Do the following to link your project to the repo that you've created in step 1:

git remote add origin https://github.com/youraccount/repositoryname.git

7. Push changes

You can decide which branch you wanted to push but initially, you'll have one branch, which is called master:

git push -u origin master

Or if you have an existing branch, just replace master with yourbranchname.

Hope that helps you out!

Using SSH with a private key

Got a .pem key but don't know how to SSH to your server, just do this:

ssh -i name_of_key user@domain -p 22

BONUS: Convert .ppk to .pem key

Recently, I started working from home and as a programmer, it's pretty common to access the company server for development purposes.

Back in the office, I used to access it using PuTTY but now that I'm using a linux machine, I thought of accessing it via Terminal but there's a catch, I can't use .ppk key to access it.

So, I did a little research and figured that I can easily convert it using puttygen

Open up your terminal and type:

sudo apt install putty-tools

Now, convert your private key to PEM format:

puttygen yourprivate.ppk -O private-openssh -o your_new_key.pem

That's it and you're good to go!

Create a symbolic storage link

When using Laravel, the public directory is used for files that are publicly accessible. By default, it's stored in local and often stored in this storage/app/public directory. You can make it easily accessible by using the following command:

php artisan storage:link

Once, it has been created, you can use access those files using the public_path or asset methods.

<?php
echo public_path('images/sample_1.jpg');
echo asset('images/sample_2.jpg');
?>

Symbolic storage link in shared hosting

If you're hosting a laravel project via cPanel, chances are that it could be a shared hosting server and that means you can't really use php artisan storage:link for this. But don't worry, there's another way to this. Just follow the steps below:

1. Create a symlink

In your public_html/public directory, remove the storage folder. Next, create a symlink.php file in your public_html directory and add the following code:

<?php
$targetFolder = $_SERVER['DOCUMENT_ROOT'].'/storage/app/public';
$linkedFolder = $_SERVER['DOCUMENT_ROOT'].'/public/storage';
symlink($targetFolder, $linkedFolder);
echo "done";
?>

2. Create a custom route to access storage

Alright, this is kind of a hack but it works extremely fine. Just add the following route in your routes/web.php file:

<?php
Route::get('/storage/anyfolder/{filename}', function($filename){
    // Your folder path
    $folder_path = storage_path('app/public/anyfolder/'.$filename);

    // check if the file exists
    if(!File::exists($folder_path)) {
        abort(404);
    }

    $file = File::get($folder_path);
    $type = File::mimeType($folder_path);

    $response = Response::make($file, 200);
    $response->header("Content-Type", $type);

    return $response;
})
?>

Now, you can access your images or any other assets easily using:

<?php
asset('storage/anyfolder/'.$filename)
?>

Hope this helps you out!