Laravel 5多个公用文件夹资产功能

I need some help.

I want to create a multisite from only one Laravel 5.1 installation. I read multiple .env based on subdomain.

But I want to every site has its own public folder.

So for example with this folders:

-> public
-> custom_publics
   -> user1
   -> user2
   -> etc

I can set public_path() but when I try to get it with asset() function doesn’t work.

For example: I have a image.png on http://user2.test.on/custom_publics/user2/image.png. I change public_path() to custom_publics/user2. But when call asset(image.png)gives me http://user2.test.on/image.png and not exists.

There is some way to point to http://user2.test.on/image.png but really goes to http://user2.test.on/custom_publics/user2/image.png ?

Or, how can I set asset() function path?

Any one has tried? Any help?

Regards, Eric.

One way to potentially solve this is by using Apache rewrites so that a request to http://user2.test.on/image.png actually goes to http://test.on/custom_publics/user2/image.png.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.test\.on
RewriteRule ^(.*)$ http://test.on/custom_publics/%1/$1 [L,NC,QSA]

You might need to tweak the above example for your specific purpose. See more information here: .htaccess rewrite subdomain to directory

Ok, I finally get the answer.

Create an AssetsController controller like this:

<?php

namespace app\Http\Controllers;

use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Response;
use app\Http\Controllers\Controller;
use Redirect;

class AssetsController extends Controller
{
    /**
     * Get asset url and return response based on file type
     *
     * @param $filename
     * @return mixed
     */
    public function data($filename)
    {
        $path = public_path().'/'; // You need to change your public path before this
        $path =  $path.$filename;

        $file = File::get($path);
        $type = File::mimeType($path);

        $response = Response::make($file, 200);
        $response->header("Content-Type", $type);

        return $response;
    }

}

So now, I need to change asset helper function. I created a ServiceProvider to make the magic Register under config/app.php:

<?php

namespace app\Providers;

use Illuminate\Support\ServiceProvider;

class SitePathServiceProvider extends ServiceProvider
{

    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = false;

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Reset asset function
     * 
     * @param $path
     * @return string
     */
    public function asset($path)
    {
        return url('assets/'.$path);
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

The trick is to do a route helper:

Route::get('/assets/{params}', 'AssetsController@data')->where('params', '.*');

This route catch all url like:

http://www.example.com/assets/demo.jpg
http://www.example.com/assets/uploads/demo.jpg
http://www.example.com/assets/large/structure/folder/demo.jpg

I know it's something complicated, but works like a charm.

King regards