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:

Continue reading “A Silly way to comment in your Code”

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 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';