megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

Convert string from snake case to camel case

Thought of sharing a simple regular expression that I use on VIM to convert snake_case letters to camelCase letters (see what I did there) 😜

Here's the pattern for you to use:

:s/ \([a-zA-Z]\)/\u\1/g

Hope you found this tip useful!

Check if trait is being used in your class

Want to know if a trait is being used in your current class? Try this:

<?php
in_array(Foo::class, class_uses($this))
?>

By any chance, if the current class is inherited, please note that the class_uses() method will only return the list of traits used by the current class and won't include any traits of it's parent class.

How to resolve the issue of not receiving emails on the same domain?

Recently, we hosted our company's redesigned website on GoDaddy, which offers cPanel to manage your website. I was dealing with an annoying email bug in which I was able to send/receive emails to any account except the ones that share the company domain.

The company's current email setup makes use of Google Workspace and since we're using a Shared Hosting account, GoDaddy allows you to use their SMTP relay and prohibits the use of third-party SMTP services such as Google Workspace, Outlook, etc.

After configuring it with Google's MX records in the DNS settings, I wasn't receiving any email on my own company email yet I was able to receive on other email accounts that didn't share the company's domain.

I did a little R&D and ran into this documentation about email routing and figured out that there could be an issue with it's configuration.

Here's what I did:

  1. Open cPanel
  2. Search or look for Email Routing
  3. Click on Email Routing
  4. If your MX records are not pointing to the IP address of the hosting server, then select Remote Mail Exchanger
  5. Save changes

After following these steps, I was able to receive mails on the same domain!

So what really caused the issue?

Since, we didn't have a default email address set up in cPanel, the current mode to send all unrouted emails was set to :blackhole:, by default. I guess, it's set up that way to prevent the server from sending/receiving spam mails from the domain.

This makes sense because:

  1. The MX records are not pointing to the current server
  2. There are no email accounts created for the domain on cPanel
  3. By setting the mode to :blackhole, all emails with the same domain are being discarded or rejected

Not really sure if this is what caused the issue but judging from the facts, I was able to reach to this conclusion.

Hope you found this tip useful.

Reference

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 "file_get_contents(): SSL operation failed" error

If you're facing this error while trying to download a file from your server, it's most probably the SSL certificate that has been hosted on your server isn't correctly verified or maybe, you're using OpenSSL on a server running PHP 5.6.

As per the documentation, there are some changes that can be made to resolve it, like the following method:

<?php
public function foo(Request $request) {
    $arrContextOptions = array(
        "ssl" => array(
            "verify_peer" => false,
            "verify_peer_name" => false,
        ),
    );
    return Response::make(file_get_contents(asset('pdf/file.pdf'), false, stream_context_create($arrContextOptions)), 200, [
        'Content-Type' => 'application/pdf',
        'Content-Disposition' => 'inline; filename="file.pdf"',
    ]);
}
?>

Although, I won't recommend this unless you are testing it on a localhost environment as it's not secure and could have significant security implications because disabling verification can permit a Man in the Middle attack to take place.

Use it at your own risk!

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!

Convert to date from timestamp using Carbon

Using Carbon's createFromFormat() method is basically a wrapper for DateTime::createFromFormat(), the main difference between the two methods is that you can add a timezone to Carbon's method.

Here's a sample on how you can convert to date using timestamp using Carbon:

<?php
    function formatDate(Request $request) {
        return \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $request->date)->format('Y-m-d');
    }
?>

Hope you found this useful!