Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public disk - How to handle properly? #11

Closed
Scaenicus opened this issue Nov 29, 2019 · 5 comments
Closed

Public disk - How to handle properly? #11

Scaenicus opened this issue Nov 29, 2019 · 5 comments

Comments

@Scaenicus
Copy link

Scaenicus commented Nov 29, 2019

Hello!

Thank you for you great tool!
How would
php artisan storage:link --domain domain.of.this.site
work properly?

https://laravel.com/docs/master/filesystem#the-public-disk

Currently I would manipulate the config/filesystem.php to point every request into the default storage/app/public/domain.of.this.site directory


That said, even cooler would be a file resolution trick to replace the public/storage directory with the correct one.

Which should be possible with .htaccess

What have you planned in that regard?

@Scaenicus
Copy link
Author

Scaenicus commented Nov 29, 2019

My current solution to keep Laravel's default notation for storing "public customer data":

Make a similar link as php artisan storage:link would make, but to a different subdirectory. public/domains and run for each domain (from public/domains):

ln -s ../../storage/<full_domain_name>/app/public <full.domain.name>

(mind the change from _ to . in the current version)

Modify .htaccess in public:

    # Rewrite to specific public storage
    RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
    RewriteRule ^storage/(.*)$ domains/%1/$1

(( Also now I know that RewriteCond stores its regex-atoms into %<number> and RewriteRule into $<number> that makes it way easier to design rules ))


If you like that idea: Would it be possible that you automate the linking part, or have a command like php artisan domain:storages to do that?

@gecche
Copy link
Owner

gecche commented Nov 29, 2019

Hi, thank you for appreciating the package :)

You have a point because storage:link command does not work properly with my package.
But I think there is not a big issue:

  • The problem is that the link which is created by the command is hard coded in the source code: there is no way of using another link name apart from "storage", so, obviously you can use it once and not for all the domains
  • For the above, I believe that in future versions of Laravel they will make it configurable as any other thing in the framework and I think that it is not a good idea to re-do the command in my package

What I would do to solve your issue probably would be the following:

  1. instead of use the command simply create the link manually (the storage:link command ONLY creates a link) for each domain, e.g. for the domain site1.test in the public folder do:
    ln -s <STORAGE_DOMAIN_PATH> storage-site1_test
    or whatever other link name you want. <STORAGE_DOMAIN_PATH> is the storage folder created by the domain:add command.

  2. In the .env of each domain put an entry, e.g. APP_PUBLIC_STORAGE=-site1_test

  3. In the filesystems.php change as follows:

       'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage'.env('APP_PUBLIC_STORAGE'),
            'visibility' => 'public',
        ],

Let me know if it helps, maybe it is worth considering to add it in the docs.

Cheers

Giacomo

@Scaenicus
Copy link
Author

Scaenicus commented Nov 29, 2019

Hi Giacomo!

I just finished the .htaccess solution by banging my head against mod_rewrite until it worked :-D

My problem is, that I use Laravel only for the backend. The frontend is Vue.js so it is preferable that "customer data" remains, at least seemingly, in the public/storage directory, but in truth the domain name selects what "customer data" means.

Of course I could also just rewrite the Vue.js frontend to be domain-name aware, which has additional problems, as we have a huge repository mostly used for none-multi-domain applications.

If you find any benefit in my solution you can also document it for SPA-developers.

With kind regards,
Philipp

@Scaenicus
Copy link
Author

  • For the above, I believe that in future versions of Laravel they will make it configurable as any other thing in the framework and I think that it is not a good idea to re-do the command in my package

I don't think there will be any customization. Putting files into the public directory bypasses Laravel, which is a huge performance boost, as the HTTP daemon just serves the file without consulting Laravel.

All other requests are relayed to the public/index.php which in turn boots up Laravel for the request.

As the default .htaccess reads: When the URL doesn't lead to any directory (first line) or file (second line) ask laravel for help (third line).

# Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

@gecche
Copy link
Owner

gecche commented Dec 2, 2019

Hi!

I have had now a bit of time to review your comments.

  • For the above, I believe that in future versions of Laravel they will make it configurable as any other thing in the framework and I think that it is not a good idea to re-do the command in my package

I don't think there will be any customization. Putting files into the public directory bypasses Laravel, which is a huge performance boost, as the HTTP daemon just serves the file without consulting Laravel.

Mmmh I don't know, but probably I will do a pull request when I have a bit of time. The only customization part would be the name of the link, in a way that we will not be forced to choose storage but whatever we want.
If the link name would be customizable the command php artisan domain:storages you suggest would be for free simply using an .env value as the link name.
If it will not be the case and the link name remains fixed and hard coded, I will consider do a command of that kind.

If you find any benefit in my solution you can also document it for SPA-developers.

Yes, I'm not very expert of .htaccess and server configuration stuff :) but it seems a good solution in SPA projects and I will document it asap.

If you don't have any other remarks, I close the issue, thank you very much

Giacomo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants