How to change the Public directory path in Lumen?

对着背影说爱祢 提交于 2020-01-02 11:29:52

问题


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?


回答1:


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:


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




回答3:


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



回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/30789790/how-to-change-the-public-directory-path-in-lumen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!