megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

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'
]);
?>

Clear all lines

  1. Switch to normal mode by pressing ESC key
  2. Press gg and it will take you to first line of the file.
  3. Then type dG and it will clear all the lines from start to the end of the file.

Find files containing specific text

This helped me a lot whenever I'm in a remote server trying to find a keyword or specific text amongst a bunch of files.

This command will save you a lot of time:

grep -rwn [path] -e 'pattern'
  • r stands for recursion
  • n displays the line number
  • w matches the whole word

Refer to man find pages for more info.

View the filesize in human-readable format

Wanted to view the size of a file in terminal but don't understand the number of bytes displayed? No worries, just type this command and it'll display the size of the in human-readable format:

du -sh filename.ext

Zip a file

This comes in handy especially whenever you want to download multiples files from a server or take backups.

zip [option] output_file input1 input2

For example: if you want to zip an entire directory with it's file contents, just do this:

zip -r myfiles.zip folder/

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.

Check RAM and disk space

RAM space

Type the following command to view available memory in your system:

free -h

Disk space

Type the following command to view available disk space in your system:

df -h

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!