Skip to content
Home Sinan Eldem
Software

Renaming the Public Directory in Laravel

Sinan Eldem

• 3 min read

Renaming the Public Directory in Laravel

Hi everyone,

One of the first things I do when starting any project with Laravel is rename Laravel’s public directory to public_html, both to avoid virtual host issues and because the default public directory on my servers is public_html.

Note: Here I'll assume the default public directory is named public_html; on your server it might be www, htdocs, httpdocs, etc. Check your server and configure things according to that name.

Below is a sample screenshot from FileZilla:

We'll set up all the configuration needed so that this change doesn't cause problems when you use the php artisan serve command, and so that your assets are sent to this directory when you use elixir / gulp and during any vendor publishing.

Follow the steps below one by one:

Rename the public directory to public_html.

In the index.php file in this directory, below the line

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

add the following lines:

// set the public path to this directory
$app->bind('path.public', function () {
    return __DIR__;
});

Save and close.

In the server.php file in the project root, update the ‘/public’ parts to ‘/public_html’.

In the gulpfile.js file in the project root, below the line

var elixir = require('laravel-elixir');

add these:

elixir.config.sourcemaps = false;
elixir.config.publicDir = 'public_html';
elixir.config.publicPath = 'public_html';
elixir.config.cssOutput = 'public_html/css'; // tercihe bağlı
elixir.config.jsOutput = 'public_html/js'; // tercihe bağlı

Save and close.

Create a file named Serve.php in the app/Console/Commands directory and save the following in it:

<?php

namespace App\Console\Commands;

use Exception;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\ProcessUtils;

class Serve extends Command {
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'serve';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Serve the application on the PHP development server';

    /**
     * Execute the console command.
     *
     * @return void
     *
     * @throws \Exception
     */
    public function fire() {
        chdir('/');

        $host = $this->input->getOption('host');

        $port = $this->input->getOption('port');

        $base = ProcessUtils::escapeArgument($this->laravel->basePath());

        $binary = ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false));

        $this->info("Laravel development server started on http://{$host}:{$port}/");

        if (defined('HHVM_VERSION')) {
            if (version_compare(HHVM_VERSION, '3.8.0') >= 0) {
                passthru("{$binary} -m server -v Server.Type=proxygen -v Server.SourceRoot={$base}/ -v Server.IP={$host} -v Server.Port={$port} -v Server.DefaultDocument=server.php -v Server.ErrorDocument404=server.php");
            } else {
                throw new Exception("HHVM's built-in server requires HHVM >= 3.8.0.");
            }
        } else {
            passthru("{$binary} -S {$host}:{$port} {$base}/server.php");
        }
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions() {
        return [
            ['host', null, InputOption::VALUE_OPTIONAL, 'The host address to serve the application on.', 'localhost'],

            ['port', null, InputOption::VALUE_OPTIONAL, 'The port to serve the application on.', 8000],
        ];
    }
}

Save and close.

In the Kernel.php file in app/Console, change the section

protected $commands = [
    //
];

as follows:

protected $commands = [
    Commands\Serve::class,
];

Save and close.

That's it!

If you get stuck on anything, let me know in the comments section below this article.

Happy Laraveling!

Sinan Eldem

Custom software and AI solutions with Laravel

Related posts

Software
6 min read

Laravel Eloquent Relationships Quick Reference Guide

This quick reference is a practical guide that explains the most common relationship types with simple examples. Covering hasOne, hasMany, belongsTo, belongsToMany and polymorphic relationships along with eager loading and performance tips, it gives developers a fast and accurate reference.

Read more