Mahmoud Zalt
Mahmoud Zalt

Reputation: 31190

How to change the Public directory path in Lumen?

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?

Upvotes: 2

Views: 9723

Answers (5)

A Hashemi
A Hashemi

Reputation: 339

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.

Upvotes: 0

iben
iben

Reputation: 51

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

Upvotes: 3

Ɛɔıs3
Ɛɔıs3

Reputation: 7853

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.

Upvotes: 2

FilipNikolovski
FilipNikolovski

Reputation: 71

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

Upvotes: 2

MaGnetas
MaGnetas

Reputation: 5108

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.

Upvotes: 0

Related Questions