How to change the Public directory path in Lumen?

9.5k views Asked by At

By default Lumen "same as Laravel" has myApp/public directory to put all the public files (assets).

I want to change that directory path from myApp/public to myApp/src/custom/public. How can I do achieve this?

5

There are 5 answers

3
w3sic3 On BEST ANSWER

Got the same problem, and solved it. Look here, maybe it will help you. I have done this solution for my Lumen application, which works for me.

UPDATE

Ok, let's go proceeding some changes to make the system to work with your tree.

  1. Add a .htaccess file in the root of your application so in the directory myApp\. Write it in :

    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
    RewriteRule ^ %1 [L,NE,R=302]
    RewriteRule ^((?!public/).*)$ src/custom/public/$1 [L,NC]
    

Assuming that you have configured your vhost pointing to \Path\myApp, we now accede to the index file of myApp\src\custom\public\. If we didn't make any mistakes, then we should get to a page that indicates an error, telling us that the bootstrap/app.php file is not found. Logic.

  1. We must therefore change the index.php file in the directory myApp\src\custom\public :

    Change from this :

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

    To this :

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

You can now get your home page directly from the path wanted.

2
MaGnetas On

I believe you should "point" your vhost to the new location (assuming you moved/renamed the public dir to your desired path). This is only needed is your server is not yet configured this way.

Then open the index.php from your new location and "fix" the path to bootsrap/app.php

Then open server.php from the Lumen base dir and edit your paths in both locations you find "public" mentioned.

Didn't really test this but looks like it should work. Give it a try.

1
FilipNikolovski On

You can override your public directory using the IoC container like this:

App::bind('path.public', function()
{
    return base_path().'/public_html';
});

But I prefer to use a symlink to the public folder like this:

ln -s public public_html
1
iben On

I also struggled with this and found a soltuion elsewhere.

I wanted the following directory structure:

.hosting root dir
 ├── lumen_app_dir <---
 ├── other_app_dir
 ├── etc...
 └── public_html
     └──lumen_app_public_dir <---

So I did the following:

  1. Copied the index.php and .htaccess from lumen_app_dir/public to the lumen_app_public_dir.
  2. Changed the index.php there like this:

    $app = require __DIR__.'/../../lumen_app_dir/bootstrap/app.php';
    
    $app->run($app['request']);
    

    The important part here is, that I had to include $app['reqest'] as the parameter for run function.

And it just works without any change in the default .htaccess file. I can access the Lumen installation at server.dev/lumen_app_public_dir

0
A Hashemi On

One solution is to bind the path fro public in the appServiceProvider.php register method:

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

A reliable way is to go into your public_html/index.php! I'm using Lumen 6.x and it's work properly.