Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

giwrgos's avatar

How i can change laravel directory structure

Hello, I'm trying to change the structure of the laravel. Basically on the dedicated virtual server I have created a folder called laravel and I added all the laravel files inside there and all the contents of the public inside the public_html. Then in the index.php (the file in the public_html) I did the following changes.

require __DIR__.'/../laravel/bootstrap/autoload.php';
$app = require_once __DIR__.'/../laravel/bootstrap/app.php';



$app->bind('path.public', function() {
    return __DIR__;
});

The project is working, the issue now is that because I have changed the structure I have affected some libraries that I'm using. For example the module media ( https://github.com/AsgardCms/Media )

When I'm trying to upload an image instead of uploading inside the public_html/assets/media, where public_html is www, it's trying to create a public or public_html inside the laravel folder. I have added the following code to change the public to public_html, but it seems it is not working. Can anyone help to keep this structure and fixing the paths?

Here is the code of the Media how it saves the images

    public function store(UploadedFile $file)
{
    $savedFile = $this->file->createFromFile($file);
    $path = $this->getDestinationPath($savedFile->getOriginal('path'));
    $stream = fopen($file->getRealPath(), 'r+');
    $this->filesystem->disk($this->getConfiguredFilesystem())->writeStream($path, $stream, [
        'visibility' => 'public',
        'mimetype' => $savedFile->mimetype,
    ]);
    $this->createThumbnails($savedFile);
    return $savedFile;
}

Here is an image of my directory structure on my server. http://postimg.org/image/xd5ucom9d/

P.s: the project is based on the asgardcms

0 likes
11 replies
giwrgos's avatar

@arcadev You can do the same if for example in the same level of public_html I can create a laravel folder and paste all the code in there?

postitief's avatar

Just asking, why would you not run the whole site in the public_html folder?

giwrgos's avatar

@postitief this is my last solution to change the document root directory. @ARCANEDEV because I don't want to mix the laravel folders and files with the folders and files of the cpanel which creates outside the public_html. The image that i have posted shows exactly that there is already a mess of files outside without having the laravel's folders

postitief's avatar

@giwrgos, what if you change the document root of the domain and just install Laravel in de public_html folder?

You should change the document root to something like this /public_html/public. I believe nothing in the public_html folder is public, only the stuf in the /public_html/public folder. And you site directly runs without changing anything to Laravel and you don't get a mess on folders.

This link might help you how to change the document root on CPanel: https://www.servint.net/university/article/the-tech-bench-changing-a-document-root-in-cpanel/

ARCANEDEV's avatar
Level 23

OK, i see what you want to achieve @giwrgos.

In your local machine, try to structure your laravel project like this:

 /
 |--- /laravel
 |    |--- /app
 |    |--- /bootstrap
 |    |--- /config
 |    |--- /database
 |    |--- /resources
 |    |--- /storage
 |    |--- /tests
 |    |
 |   ...
...
 |--- /public_html
 |    |--- .htaccess
 |    |--- index.php
 |   ...

As you can see, the laravel folder contains all the Laravel folders & files except the public folder. Copy the the public folder content to your public_html.

Now you need to change these lines https://github.com/laravel/laravel/blob/master/public/index.php#L22 & https://github.com/laravel/laravel/blob/master/public/index.php#L36 in your public_html/index.php.

And make the changes that i've said about extending the Laravel Application class.

<?php namespace App;

use Illuminate\Foundation\Application as IlluminateApplication;

class Application extends IlluminateApplication
{
    /**
     * Get the path to the public / web directory.
     *
     * @return string
     */
    public function publicPath()
    {
        $separator = DIRECTORY_SEPARATOR;

        return $this->basePath.$separator.'..'.$separator.'public_html';
    }
}

After these changes, try to use a vhost in your local machine to test if everything is working and upload it in your remote server.

1 like
giwrgos's avatar

@bashy i know the symlinks. @ARCANEDEV i tried what you post except to extend the IlluminateApplication. by chaning the index.php it was loading fine the laravel but when i was trying to upload some images on my web app, the module was trying to create a public/assets/media folder inside the laravel instead of saving into public_html/assets/media. Basically it was trying to save the images into the laravel folder instead of going out of the laravel and going into the public_html. Anws I will give a try. thanks a lot. If it will not work I will change the document root

ARCANEDEV's avatar

Just try it @giwrgos, extend the Application class because you're using a custom folder structure => different path instances => path.* including the path.public => changing the path helpers like public_path().

And this is way better than:

$app->bind('path.public', function() {
    return __DIR__;
});

Laravel also gives you the possibility to create your own extended classes (https://github.com/laravel/laravel/blob/master/bootstrap/app.php#L29-L42).

1 like

Please or to participate in this conversation.