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/...
- Rename
public
to desired name (new root folder, for examplelumenapp
) - Move everything else into a new folder, named e.g.
project_src
- 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 thedispatch
method (line ~1027) to remove thelumenapp
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);
}
}
The mentioned dispatch method looks for the folder name in the .env File, so define it there: ROOT_FOLDER=lumenapp
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]
Referenced images, CSS files in the lumenapp
directory need the new path (\lumenapp\css\...
) or some extra handling.
Just saved me now! thanks!