简体   繁体   中英

How to serve multiple static folders in Laravel?

I have three folders with static files (Angular apps): web, login, admin.

I want to serve:

  • web folder when the URL is /*
  • login when the URL is /admin/* and not logged
  • admin when the URL is /admin/* and logged.

My routes.php :

// admin folder
    Route::get('/admin', ['before' => 'auth', function()
    {
        return File::get(public_path() . '/admin/index.html');
    }]);

    Route::get('/admin/{slug}', ['before' => 'auth', function($slug)
    {
        return File::get(public_path() . '/admin/' . $slug);
    }]);


    //login folder
    Route::get('/admin', function()
    {
        return File::get(public_path() . '/login/index.html');
    });

    Route::get('/admin/{slug}', function($slug)
    {
        return File::get(public_path() . '/login/' . $slug);
    });


    // web folder
    Route::get('/', function()
    {
        return File::get(public_path() . '/web/index.html');
    });

    Route::get('/{slug}', function($slug)
    {
        return File::get(public_path() . '/web/' . $slug);
    });

Problems:

  • /{slug} doesn't serve files from subfolders.
  • When I am not logged, /admin/ redirect me to /login .
  • Must have a way to write this routes with regular expressions.

How can I solve the previous points?

  • Don't know what's the problem with you're first point. Possible problem:
    • You enter a extension as slug, for example slug.html. In that case the standard laravel htaccess tries to search for a file which not exists. So laravel isn't booted! To solve this problem you can change the htaccess or don't use extensions in you're route.
  • You've added the same route url 2 times. Laravel standard takes one and skip the other. Therefore the first admin route is taken. Therefore the auth filter is always excecuted. In the standard filter, which is located in the filters.php file, a user which is not logged in is redirected to login. Therefore you're redirected to login. You can solve this by changing the filter or remove the filter and use this for example in you're route:

     Route::get('/admin',function() { if(Auth::check()) return File::get(public_path() . '/admin/index.html'); return File::get(public_path() . '/login/index.html'); }); Route::get('/admin/{slug}',function($slug) { if(Auth::check()) return File::get(public_path() . '/admin/' . $slug); return File::get(public_path() . '/login/' . $slug); }); 
  • As you can read here you can use regular expressions. For example ->where('slug', '[A-Za-z]+');

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM