Skip to content

Instantly share code, notes, and snippets.

@islahh
Forked from dbaeck/lumen_in_subfolder.md
Created September 22, 2020 04:40
Show Gist options
  • Save islahh/cc155c6e6ea29500295bd1c3c917fda6 to your computer and use it in GitHub Desktop.
Save islahh/cc155c6e6ea29500295bd1c3c917fda6 to your computer and use it in GitHub Desktop.
Howto deploy Lumen projects within a subfolder (on a shared hoster)

Howto deploy Lumen projects within a subfolder (on a shared hoster)

For Lumen (5.0.8) (Laravel Components 5.0.*)

Use Case: Deploying Lumen App to Shared Hoster such that it can be called via domain.tld/lumenapp/...

Folder Layout

  • Rename public to desired name (new root folder, for example lumenapp)
  • Move everything else into a new folder, named e.g. project_src

Path Changes

  • In lumenapp/index.php, change the $app = ... line to
    $app = require __DIR__.'/../project_src/bootstrap/app.php';
  • In project_src/vendor/laravel/lumen-framework/Application.php change the dispatch method (line ~1027) to remove the lumenapp part from requests (the part from $rootFolder = ...):
    public function dispatch($request = null)
    {
        if ($request) {
            $this->instance('Illuminate\Http\Request', $request);
            $this->ranServiceBinders['registerRequestBindings'] = true;

            $method = $request->getMethod();
            $pathInfo = $request->getPathInfo();
        } else {
            $method = $this->getMethod();
            $pathInfo = $this->getPathInfo();
        }

        try {

            //Get the root folder from .env file
            //Strap this root folder from the route ($pathInfo)
            $rootFolder = '/'.env('ROOT_FOLDER', 'root');
            if(starts_with($pathInfo, $rootFolder))
            {
                $exp = explode('/',$pathInfo);
                unset($exp[0]);
                unset($exp[1]);
                $pathInfo = '/'.implode('/',$exp);
            }

            if (isset($this->routes[$method.$pathInfo])) {
                return $this->handleFoundRoute([true, $this->routes[$method.$pathInfo]['action'], []]);
            }

            return $this->handleDispatcherResponse(
                $this->createDispatcher()->dispatch($method, $pathInfo)
            );
        } catch (Exception $e) {
            return $this->sendExceptionToHandler($e);
        }
    }

Environment File

The mentioned dispatch method looks for the folder name in the .env File, so define it there: ROOT_FOLDER=lumenapp

.htaccess

This is what my .htaccess within the lumenapp (former public) folder looks like:

RewriteEngine On

RewriteBase /

# Redirect Trailing Slashes...

RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond $1 !^(index\.php|images|quotes|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ quotes/index.php/$1 [L,QSA]

Assets

Referenced images, CSS files in the lumenapp directory need the new path (\lumenapp\css\...) or some extra handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment