Skip to content

Instantly share code, notes, and snippets.

@dbaeck
Created May 6, 2015 10:25
Show Gist options
  • Save dbaeck/89d0b52d9d3d2777a96f to your computer and use it in GitHub Desktop.
Save dbaeck/89d0b52d9d3d2777a96f 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.

@carlosflorencio
Copy link

Just saved me now! thanks!

@faizalmansor
Copy link

Cool, very useful tip! Works for Lumen (5.1.6) (Laravel Components 5.1.*) too, except that you don't need to change the dispatch method. Just by moving the folders and changing the path to bootstrap is enough. Thanks.

@tripper54
Copy link

See http://todiadiyatmo.com/running-laravel-lumen-from-sub-directory/ for an alternative to altering .htaccess

@brunoleles
Copy link

I've just added this to public/index.php just after the $app = require __DIR__.'/../bootstrap/app.php'; so there is no need to edit files inside the vendor directory or edit the .htaccess.

$SCRIPT_NAME = str_replace(['\\', '/index.php'], ['/', ''], array_get($_SERVER, 'SCRIPT_NAME', array_get($_SERVER, 'PHP_SELF', '')));
$_SERVER['REQUEST_URI'] = preg_replace('|' . $SCRIPT_NAME . '|', '', $_SERVER['REQUEST_URI'], 1);

I've tested on Ubuntu + Apache, should be okay for any Unix system, maybe for windows users the str_replace will need added something like 'C:\\' to normalize the $SCRIPT_NAME.

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