A Silly way to comment in your Code

During my beginners year of writing code, i used to comment unnecessarily. I realised this after i saw the code written by senior developers. The main purpose of the comment is to make your code understood by other developers who are working together in the same project or for other contributors.

Let us take an example of the silly code:

<?php

// Assigning the value to firstNumber
firstNumber = 9

// Assigning the value to secondNumber
secondNumber = 12

// Creating variable to store the sum of firstNumber and secondNumber
sumOfNumbers = 0;

// Adding both firstNumber and secondNumber and storing it to sumOfNumbers
sumOfNumbers = firstNumber + secondNumber

// Displaying the result
echo sumOfNumbers;
?>

Read the code above, how do you feel about that?

The person who will read your code will be developer or some one who understands the code. If he does not understand that firstNumber = 9 is assigning number 9 to variable firstNumber, then he needs to learn programming. It is not your job to make him understand this.

However, this commenting style might be helpful if you are University Professor teaching how-to-write-code to your student.

So, the better version of the above code would be like this:

<?php
firstNumber = 9
secondNumber = 12
sumOfNumbers = 0;
sumOfNumbers = firstNumber + secondNumber
echo sumOfNumbers;
?>

Much better isn’t it?

So, unless you are teaching programming to someone beginners don’t write unnecessary comment in your code for two reasons. First one is it makes code unreadable to some sense. Next one is other programmers thinks you are silly because you think they are silly.

How to set up basic PHP Project structure with Composer

Most of the time beginners learning PHP are unaware of Composer, if you are among them, Composer is a dependency management tool for PHP.

Let’s take a simple example, rather than using include command in each of the PHP file, composer makes it easy by specifying that file as a dependency. Another example would be if you use some library then it makes easier to use and update it with a single command.

Continue reading How to set up basic PHP Project structure with Composer

How to Install PHP Composer in your Mac

Composer is the easiest way to manage dependencies to your small to large PHP projects.

Use the following commands to install composer in your Mac

$ curl -sS https://getcomposer.org/installer | php

Now the composer is installed in your current directory.

Now, you can access it using the following command

$ php composer.phar [command]

It works but you don’t always want to use this directory path to access the composer, So in the next step we will make it accessible globally.

Let us move it to usr/local/bin to make it globally accessible from your terminal.

$ mv composer.phar usr/local/bin

Next, create alias and add it to your bashrc file to load it everytime you restart your terminal or mac book.

$ echo 'alias composer="php /usr/local/bin/composer.phar"' >> ~/.bash_profile

That’s all, Restart your terminal and now try using composer in your terminal and it works. Every time the terminal opens it autoloads this alias now.

Bonus: If it is for other platform such as Ubuntu, you need to write to bashrc instead of bash_profile.

$ echo 'alias composer="php /usr/local/bin/composer.phar"' >> ~/.bashrc

How to remove /public/ from URL in Laravel

By default in Laravel the URL to access your site is http://example.com/public/. It is not good to keep public in URL as it makes URL ugly and longer.

So, let’s talk about the solution to remove /public/ from the URL.

Method I : Using .htaccess
Create a .htaccess file your Laravel root directory if it does not exists already. (Normally it is under your public_html folder)

And add the following code to it:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Now you can access your site via http://example.com.

Method II : Moving the contents of public folder to root directory
Actually i prefer this method because the above method is not so good to use in production environment.

So now lets make a new folder laravelfiles in your root directory and move all the files and folder except public directory to laravelfiles.

And move everything of public directory to root folder. Now your root directory will look something like this:

Remove Laravel Public url

Now we have to change the paths in laravelfiles/bootstrap/paths.php file and index.php.

Find these lines in laravelfiles/bootstrap/paths.php

    'app' => __DIR__.'/../app',
    'public' => __DIR__.'/../public',

Change these two lines to:

    'app' => __DIR__.'/../app',
    'public' => __DIR__.'/../../',

Find these lines in index.php

    require __DIR__.'/../bootstrap/autoload.php';

    $app = require_once __DIR__.'/../bootstrap/start.php';

And change to:

    require __DIR__.'/laravelfiles/bootstrap/autoload.php';

    $app = require_once __DIR__.'/laravelfiles/bootstrap/start.php';