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

    when the url is /*

  • login

    when url is /admin/*

    not registered
  • admin

    when the url is /admin/*

    u registered.

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}

    does not support files from subfolders.
  • When I am not registered, /admin/

    redirect me to /login

    .
  • There should be a way to write these routes using regular expressions.

How can I solve the previous points?

+3


source to share


1 answer


  • Don't know what the problem is with you. Possible problem:
    • You enter the extension as a slug, like slug.html. In this case, the standard larvel htaccess tries to find a file that does not exist. So laravel won't load! To fix this problem, you can change htaccess or not use extensions in your route.
  • You have added the same route url 2 times. The Laravel standard takes one and skips the other. Therefore, the first administrator route is taken. Therefore, the auth filter is always excluded. In the standard filter, which is found in the filters.php file, the user who is not logged in is redirected to the login. Therefore, you are redirected to login. You can solve this problem by changing the filter or removing the filter and use it, for example, in your 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!

+3


source







All Articles