megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

Don't forget what sparked your passion

A short letter to myself and other fellow programmers.

Do you ever remember falling in love with programming when learning to code during school days or while working on a client's project? Probably not.

When I started learning to code, I never wrote code because I wanted to but because I desired to. No one to poke me around with tight deadlines, no need to race against time. Just the feeling of power and creativity in your hands to build something out of nothing but just pure code. It could have been anything as simple as building a small widget for my blog post, writing a game, solving a problem in LeetCode or Project Euler or maybe writing a bash script to automate my tasks.

You could probably relate to some but for me, these are some of the examples that resonate with me.

I'm writing this as a reminder to myself and other fellow programmers: don't ever forget that moment when your passion for programming had sparked. You might be older now and you're probably filled with a lot of responsibilities (especially if you're parenting).

Perhaps, you might never even experience that same amount of adrenaline and rush that you had when you were behind the computer in your room while your siblings and friends were trying to distract you.

There's never an end to learning. You can still find some passion projects that'll help you keep that spark. Have an open mind and be open to newer ideas. Solve a puzzle, read a book, write a game or maybe a blog post.

Don't do this because you need to impress someone or for the sake of it rather because you can and it brings you joy.

Oh and wishing you a Happy New Year! πŸ˜€πŸŽ„

Configure XDebug in Visual Studio Code

For the past 6 months, I've been writing code in C# 11 and .NET 7 Core and of course, using the amazing Visual Studio 2022 IDE was quite an amazing experience especially when it comes to debugging your code.

Recently, I had to switch back to classic ol' PHP and now, I wanted a similar debugging experience in Visual Studio Code.

Using XDebug and if configured correctly, it'll be quite useful in your debugging journey instead of using var_dump() or dd() your variables everytime.

Prerequisites

The only thing you need here is Visual Studio Code and ensure that it's the latest version. I'm writing this from a Windows Machine using the latest version of WAMP Server.

1. Download PHP Debug

The official XDebug team has released an extension that could be installed in your editor. So, here you go, first download it.

2. Install XDebug

In the previous step, we just installed the extension for the editor but that's just an adapter used between the editor and XDebug. Whereas, XDebug is a PHP extension that needs to be installed on your local system.

I'm not going to show you the installation process as this documentation explains that well enough and you can follow it based on your operating system of choice.

3. PHP Configuration

After you're done with installing the extension, open your php.ini file and add the following line in this file:

zend_extension=path/to/xdebug

Now, it's time to enable remote debugging. Depending on your XDebug version:

For XDebug v3.x.x:

xdebug.mode = debug
xdebug.start_with_request = yes

For XDebug v2.x.x:

xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_port = 9000

If you are looking for more specific options, please see read the documentation on remote debugging. Please note that the default port in XDebug v3.x.x has been changed from 9000 to 9003.

Once done, restart your PHP service and check if the configuration has taken effect. You can do this by creating a simple test.php with a phpinfo(); statement in there and look for your XDebug extension under the Loaded Configuration File section.

4. Visual Studio Code Configuration

In your editor, hold CTRL + SHIFT + P and type "Debug: Add Configuration" and it'll prompt you to select your language of preference and this case, of course, you should "PHP".

Upon selecting, it'll automatically generate a launch.json file in your .vscode folder in your project folder.

There'll be three different configurations in your file:

  • Listen for XDebug: This setting will simply start listening on the specified port (by default 9003) for XDebug.
  • Launch currently open script: It will launch the currently opened script as a CLI, show all stdout/stderr output in the debug console and end the debug session once the script exits.
  • Launch Built-in web server: This configuration starts the PHP built-in web server on a random port and opens the browser with the serverReadyAction directive.

Now, to test if it works, hit F5 and add a breakpoint in your PHP code and if it lands on your breakpoint, that means it works fine.

Hope you found this article useful!

References

Generate CSR with multiple subdomains using OpenSSL

I have done this many times but I never got to write about it and I felt that I should write about it after I helped a friend of mine with this.

Prerequisites

It doesn't matter which operating system you're going to do this on as long as you have openssl installed in your system, you should be fine.

All you have to do is just create a simple configuration file and give it a name like multiple-domains.cnf:

[req]
default_bits       = 2048
distinguished_name = req_distinguished_name
req_extensions     = req_ext

[req_distinguished_name]
countryName         = [COUNTRYCODE]
stateOrProvinceName = [STATE]
localityName        = [LOCALITY]
organizationName    = [ORGANIZATION]
commonName          = website.com

[req_ext]
subjectAltName = @alt_names

[alt_names]
DNS.1 = website.org
DNS.2 = wesbite.ae
DNS.3 = wesbite.co.uk

Save the file and copy-paste this into your command prompt or terminal:

openssl req -out <your-csr-name>.csr -newkey rsa:2048 -nodes -keyout <your-private-key-name>.key -config multiple-domains.cnf

Once executed, it should be generated for you and if not, verify your configuration file for any typos.

Hope this helps you out!

Clean Controllers in Laravel: Best practices for handling form submissions

Learn how to write clean Laravel controllers for efficient contact form handling using validation, Eloquent, Mail, and events.

Unlike most frameworks, Laravel is an unopinionated framework and doesn't force the developer to follow a particular pattern.

In this article, I'm going to show you how your codebase could benefit by following Laravel's best practices like separation of concerns, utilizing Laravel's in-built features like Eloquent models, Form Requests and Mail.

To begin with, here's a dirty method that handles a simple contact form submission:

<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class ContactController extends Controller
{
    public function submit(Request $request)
    {
        // Validate the form inputs
        $request->validate([
            'name' => 'required',
            'email' => 'required|email',
            'message' => 'required',
        ]);

        // Get the form data
        $name = $request->input('name');
        $email = $request->input('email');
        $message = $request->input('message');

        // Save the form data in the database
        DB::table('contact_forms')->insert([
            'name' => $name,
            'email' => $email,
            'message' => $message,
        ]);

        // Send an email to the user
        $mailData = [
            'name' => $name,
            'email' => $email,
            'message' => $message,
        ];

        Mail::send('emails.contact', $mailData, function ($message) use ($email) {
            $message->to($email)->subject('Thank you for contacting us');
        });

        // Return a response to the user
        return redirect()->back()->with('success', 'Thank you for contacting us! We will get back to you soon.');
    }
}

To most developers, this method might seem straight-forward and they might even debate with you that it's completely fine. But allow me to highlight the issues of that this method brings to the table:

  1. Lack of separation of concerns: The controller is responsible for handling form validation, saving data to the database, and sending an email. This violates the Single Responsibility Principle.

  2. Direct usage of the database query builder: Instead of utilizing Eloquent models for database interactions, the example directly uses the query builder, which can make the code less maintainable and harder to read.

  3. Inline email sending: The email sending functionality is implemented inline within the controller method, making the code harder to test and reuse.

  4. Lack of error handling: The example does not handle any potential errors that may occur during form validation, database insertion, or email sending.

Now, it's time to make it cleaner.

Save data using Form Requests and Laravel's Eloquent ORM

Let's start with creating a form request that is dedicated to handling the validation logic:

  1. Quickly, open up your terminal and type the following command:
php artisan make:request ContactFormRequest

This command will generate a new file named ContactFormRequest.php in the app/Http/Requests directory.

  1. Open the ContactFormRequest.php file and modify it as follows:
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ContactFormRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        // Set the authorization logic based on your requirements
        // For example, you may check if the user is authenticated or has certain permissions.
        // Return true if the user is authorized to submit the form; otherwise, return false.
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required',
            'email' => 'required|email',
            'message' => 'required',
        ];
    }

    public function messages()
    {
        return [
            'name.required' => 'Please enter your name',
            'email.required' => 'Please enter your email address',
            'email.email' => 'Please enter a valid email address',
            'message.required' => 'Write a message before submit the form',
        ];
    }
}

In the rules() method, you can define validation rules for each field in the form. You can adjust according to your own preferences and if you want to know more about them, read this documentation.

  1. Save the ContactFormRequest.php file.

Great, now you have a separate class for validating your contact form and this promotes code-reusability, improves code readability and separates the validation logic from your controller.

Now let's update the submit() method in the ContactController.php example:

<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Http\Requests\ContactFormRequest;
use App\Models\ContactFormModel;

class ContactController extends Controller
{
    public function submit(ContactFormRequest $request)
    {
        // Validate the form inputs
        // All of the validated form data is accessible via this property.
        $validated = $request->validated();

        // Save the submission
        ContactFormModel::create($validated);

        // Send an email to the user
        $mailData = [
            'name' => $name,
            'email' => $email,
            'message' => $message,
        ];

        Mail::send('emails.contact', $mailData, function ($message) use ($email) {
            $message->to($email)->subject('Thank you for contacting us');
        });

        // Return a response to the user
        return redirect()->back()->with('success', 'Thank you for contacting us! We will get back to you soon.');
    }
}

Ensure that you have an appropriate model for your contact_forms table like ContactFormModel.php and update the following like so:

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ContactForm extends Model
{
    protected $table = 'contact_forms';

    // Define any additional model configurations, relationships, or methods.
    protected $fillable = [
        'name',
        'email',
        'message'
    ];

    protected $timestamps = true;
}

This allows you to leverage Laravel's ORM in which you could benefit from easy quering, relationship management and automatic timestamps.

Making use of Laravel Events and Mail

  1. Generate a new event class by writing the following artisan command:
php artisan make:event ContactFormSubmitted

This will generate a ContactFormSubmitted class in the app/Events directory.

  1. Open up ContactFormSubmitted.php file and update as follows:
<?php
namespace App\Events;

use App\Models\ContactForm;
use Illuminate\Foundation\Events\Dispatchable;

class ContactFormSubmitted
{
    use Dispatchable;

    public $contactForm;

    /**
     * Create a new event instance.
     *
     * @param  ContactForm  $contactForm
     * @return void
     */
    public function __construct(ContactForm $contactForm)
    {
        $this->contactForm = $contactForm;
    }
}
  1. Next, let's generate an event listener using artisan command:
php artisan make:listener SendContactFormEmail --event=ContactFormSubmitted

This command will generate SendContactFormEmail event listener in the app/Listeners directory.

  1. Open the SendContactFormEmail.php file and update as follows:
<?php
namespace App\Listeners;

use App\Events\ContactFormSubmitted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Mail;
use App\Mail\ContactFormSubmitted as ContactFormSubmittedMail;

class SendContactFormEmail implements ShouldQueue
{
    use InteractsWithQueue;

    /**
     * Handle the event.
     *
     * @param  ContactFormSubmitted  $event
     * @return void
     */
    public function handle(ContactFormSubmitted $event)
    {
        $contactForm = $event->contactForm;

        // Send an email to the user
        Mail::to($contactForm->email)
            ->send(new ContactFormSubmittedMail($contactForm));
    }
}
  1. Create a mailable class using the following artisan command:
php artisan make:mail ContactFormSubmitted

This command will generate a new ContactFormSubmitted mailable class in the app/Mail directory.

Now, update the ContactFormSubmitted.php file as follows:

<?php
namespace App\Mail;

use App\Models\ContactForm;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class ContactFormSubmitted extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    public $contactForm;

    /**
     * Create a new message instance.
     *
     * @param  ContactForm  $contactForm
     * @return void
     */
    public function __construct(ContactForm $contactForm)
    {
        $this->contactForm = $contactForm;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('Thank you for contacting us')
            ->view('emails.contact')
            ->with('contactForm', $this->contactForm);
    }
}
  1. Let's open up the ContactController.php and import the necessary classes on top:
<?php
use App\Events\ContactFormSubmitted;
use Illuminate\Support\Facades\Mail;
  1. Lastly, update the submit() method as follows:
<?php
use App\Http\Requests\ContactFormRequest;
use App\Models\ContactForm;
use App\Events\ContactFormSubmitted;
use Illuminate\Support\Facades\Mail;
use App\Mail\ContactFormSubmitted;

class ContactController extends Controller
{
    public function submit(ContactFormRequest $request)
    {
        // Validated form data is accessible via the $validatedData property of the form request instance
        $validatedData = $request->validated();

        // Save the form data in the database using Eloquent
        $contactForm = new ContactForm();
        $contactForm->name = $validatedData['name'];
        $contactForm->email = $validatedData['email'];
        $contactForm->message = $validatedData['message'];
        $contactForm->save();

        // Dispatch an event to handle email sending asynchronously
        event(new ContactFormSubmitted($contactForm));

        // Return a response to the user
        return redirect()->back()->with('success', 'Thank you for contacting us! We will get back to you soon.');
    }
}

Ah! Finally, this looks much better now, doesn't it?

By making use of Laravel's Mail method, events and listeners, we've separated the logic for sending emails from the controller. The email is now sent asynchronously using an event and listener combination. This approach improves code organization, maintainability, and scalability.

Hope you found this article to be useful.

Thoughts about ChatGPT and the impact on software developers

Will AI-powered tools replace developers in the near future?

This is something I wanted to address for quite sometime but never got the time to talk about my opinions/views about it.

I have been reading many articles and watching a few videos on YouTube in which the authors often contemplate about the future of developers and whether their jobs might be replaced.

If you think that's the case, I'd like to share one of my favorite quotes that might change your mindset:

Software is just a tool to help accomplish something for people – many programmers never understood that. Keep your eyes on the delivered value, and don’t over-focus on the specifics of the tools. — John Carmack

Ever since, ChatGPT went viral, I (and some of my team members) thought of incorporating it to help educate myself during development.

Will it ever replace humans?

Just like the quote above, software is just a mere tool that helps accomplish your end goals. It definitely isn't going to replace a developer with years of experience, let alone, a human being but rather, as a tool or learning aid to help accelerate our ways of being productive.

There are tools like GitHub Copilot (powered by ChatGPT) that helps developers in troubleshooting, refactoring and solving well-defined problems but however, I feel it still has a long way to be able to solve really complex problems in which otherwise could be solved by a software developer.

I'd say that it's crucially important for you to know and be aware of the capabilities and potential of AI-based tools but not to worry too much about it's impact in the industry.

In my opinion, I'd say that a developer who knows how to be effective using AI-powered tools as a learning aid are more likely to replace an developer who aren't aware of them at all.

Here's another quote from John Carmack:

If you build full "product skills" and use the best tools for the job, which today might be hand coding, but later maybe AI guiding, you will be probably be fine. — John Carmack

It's important to view yourself as a problem solver who uses technology rather than someone who simply "codes" for a living and would get replaced in the near future.

What many people don't realize is that a software developer isn't someone who only writes code but also someone who solves complex problems and makes critical decisions. From my experience, I can tell you that it takes a lot of decisions to go from design to deployment and that involves a team that's made up of various skillsets that includes project management, design, development, QA and deployment.

Currently, AI might be good at solving well-defined or common problems but it can never replace the decision-making needed for a successful project.

Can we trust it?

Not yet. ChatGPT ain't AGI thus it has limitations such as that it "hallucinates" i.e. comes up with random facts or solutions that aren't even logical to the problem defined. Whereas, if you look for the problem on StackOverflow, you're more likely to copy/paste a solution that's already recommended by the community.

Exploring ChatGPT

As I had mentioned above, it's quite good at solving well-defined problems and here are a few examples that has helped saved me time:

  • Refactoring methods and repetitive code
  • Writing a method that validates email using regular expressions
  • Writing a simple image gallery component using ReactJS
  • Helped me learn new languages and concepts faster

Oh yes, it did make plenty of mistakes but with all that said, I think it's a great time to learn something new.

Conclusion

My take is that to not overestimate the progression of AI in the near future and underestimate it's impact in the distant future.

Hope you liked reading this article.

List tables that don't have primary keys in a MySQL database

Whenever I restore a database dump on a MySQL InnoDB Cluster, most of the time, the error I face is related to missing PRIMARY KEY attribute because as per MySQL's official documentation:

Every table that is to be replicated by the group must have a defined primary key, or primary key equivalent where the equivalent is non-null unique key.

What I would usually do is, run a simple query to determine the list of the tables that don't the PRIMARY KEY attribute:

select tab.table_schema as database_name,
       tab.table_name
from information_schema.tables tab
left join information_schema.table_constraints tco
          on tab.table_schema = tco.table_schema
          and tab.table_name = tco.table_name
          and tco.constraint_type = 'PRIMARY KEY'
where tco.constraint_type is null
      and tab.table_schema not in('mysql', 'information_schema', 'performance_schema', 'sys')
      and tab.table_type = 'BASE TABLE'
      and tab.table_schema = 'your_database_name_here'
order by tab.table_schema,
         tab.table_name;

Hope you found this tip useful!

5th February 2023 — Blogmarks

Playing around with ImageMagick

ImageMagick is not a new tool for me but I haven't really talked about how it comes in handy whenever I wanted to resize and/or compress images efficiently using the terminal instead of using some image conversion service found for free online.

To get started, make sure you install ImageMagick on your machine. This is the command I use for Fedora Linux:

sudo dnf install ImageMagick

For example, if you wanted to compress an image's quality to 80%, here's you can do it:

convert -quality 80% src.png dest.png

Great! What if you want to do it for hundreds of images in a directory, then you can easily do something like this:

find . -type f -name '*.png' -exec bash -c 'convert -quality 80% "$1" "${1%}_compressed.png" ' bash  {} \;

You can do the same to resize the images while keeping original aspect ratio intact:

convert -resize 450x450 src.png dest.png

And similar to the one above, you can do the same for multiple images like so:

find . -type f -name '*.png' -exec bash -c 'convert -resize 450x450 "$1" "${1%}_resized.png" ' bash  {} \;

I have just scraped the surface of this image manipulation tool and if you are interested to know more, read the official manpage on how to use it.

Hope you found this tip useful!