2

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?

Martin Bean
  • 38,379
  • 25
  • 128
  • 201
Mahmoud Zalt
  • 30,478
  • 7
  • 87
  • 83
  • Why do you want to change the location of the public directory out of interest? – Martin Bean Jun 13 '15 at 23:09
  • yes I want to change the location of the public directory. Because I will have multiple public directories. And I want my app to load from different public directories dynamically, (example if the request subdomain is app1.localhost I want to load the public directory of /src/app1/public). – Mahmoud Zalt Jun 14 '15 at 10:14

5 Answers5

3

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

iben
  • 51
  • 5
  • I forgot to mention that I am on shared hosting, so cannot control vhost public directory. – iben Sep 02 '15 at 08:44
2

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
Bogdan
  • 43,166
  • 12
  • 128
  • 129
  • Your answer is logical, I've been trying this everywhere and still wont work in Lumen. As fo the second workaround it is still an option but I prefer not to do it this way, instead I prefer to let the application itself handle this and not the sys admin. – Mahmoud Zalt Jun 11 '15 at 20:31
2

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.

Community
  • 1
  • 1
w3spi
  • 4,380
  • 9
  • 47
  • 80
  • This seems to be the solution. However I tried it and didn't worked!! can you please edit the answer and include a full copy of your `.htaccess` file. It might help. Thanks anyway. – Mahmoud Zalt Jun 13 '15 at 22:07
  • Ok. I just have made a complete overhaul of my answer :) – w3spi Jun 14 '15 at 11:09
  • 1
    Fantastic this actually work. Just quick note for others, this answer doesn't work with the `php artisan serve` command. you need to configure which ever server you use and point it to the root directory of your application. – Mahmoud Zalt Jun 14 '15 at 20:02
0

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.

MaGnetas
  • 4,918
  • 4
  • 32
  • 52
  • I want to dynamically set the public directory. Because I will have multiple public directories `app-1/src/custom/public` and `app-2/src/custom/public` and when I call the subdomain `app1.something.com` I want to to look for the public of `app-1/src/custom/public` the same applies to `app2`. I was able to do this with the routes and the views. But yet not with the public directory. – Mahmoud Zalt Jun 11 '15 at 20:37
  • I'd still go this way, but the server.php should be rewritten to match your subdomain logic. It needs to check if the requested file exists in correct dir and require_once index.php file from correct dir. – MaGnetas Jun 11 '15 at 20:44
0

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.

A Hashemi
  • 289
  • 2
  • 6