Running Laravel Lumen from sub-directory
Bismillah :)
Tutorial
About Me
Home
Running Laravel Lumen from sub-directory
April 15, 2015 Todi Adiatmo 11 comments Posted in Tutorial
Lumen is the new micro framework from laravel, you can found more info here : Lumen Website .
I ran lumen without homestead, just plain apache. After opening the /public folder i got this error :
Next, i try to run lumen with php artisan serve , and its work 😀
I’am curious, so i dig into the lumen framework bootstraper. If you open public/index.php , it will trigger $app->run() . The app is instance of Laravel\Lumen\Application and you can find the source on vendor/laravel/lumen-framework/src/Application.php .
the run method in turn will call dispatch :
1 2 3 4 5 6 7 8 9 10
public function dispatch ( $request = null ) { if ( $request ) { $method = $request -> getMethod ( ) ; $pathInfo = $request -> getPathInfo ( ) ; } else { $method = $this -> getMethod ( ) ; $pathInfo = $this -> getPathInfo ( ) ; } }
From the code we can infer that if no request object is pass (default is null) then lumen will try to get the path info from its own method. In my case, if we run Lumen from sub directory, the path will be absolute path from your server hostname. Which lead to route not found problem.
My solution for this is to create Symfony request object on public/index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
< ? php use Symfony \ Component \ HttpFoundation \ Request as Request ; /* |-------------------------------------------------------------------------- | Create The Application |-------------------------------------------------------------------------- | | First we need to get an application instance. This creates an instance | of the application / container and bootstraps the application so it | is ready to receive HTTP / Console requests from the environment. | */ $ app = require __DIR__ . '/../bootstrap/app.php' ; /* |-------------------------------------------------------------------------- | Run The Application |-------------------------------------------------------------------------- | | Once we have the application, we can handle the incoming request | through the kernel, and send the associated response back to | the client's browser allowing them to enjoy the creative | and wonderful application we have prepared for them. | */ $ request = Request :: createFromGlobals ( ) ; $ app -> run ( $ request ) ;
Thats all, it work on my case. Hope this work on yours too !
Andrey Sokolov 4 years ago Thanks, you solved my problem ^_^ Reply Todi Adiyatmo 4 years ago glad to hear that ! Reply
Thanks, you solved my problem ^_^
Todi Adiyatmo 4 years ago glad to hear that ! Reply
glad to hear that !
someone 4 years ago TYSVM!!!!!! Reply
TYSVM!!!!!!
Krik Batner 4 years ago Thanks for this! Couldn’t figure this out for the life of me! Reply
Thanks for this! Couldn’t figure this out for the life of me!
mattgreen110 4 years ago Thank you so much for this! Reply
Thank you so much for this!
thx 4 years ago very usefull. thank you for that post. wish that was mentioned in the lumen documentation Reply thx 4 years ago Just a note – when doing anything usefull you might encounter an error like “must be an instance of IlluminateHttpRequest, instance of SymfonyComponentHttpFoundationRequest” with this fix. That can be solved by using IlluminateHttpRequest instead of SymfonyComponentHttpFoundationRequest and use Request::capture(); instead of Request::createFromGlobals(); Reply
very usefull. thank you for that post. wish that was mentioned in the lumen documentation
thx 4 years ago Just a note – when doing anything usefull you might encounter an error like “must be an instance of IlluminateHttpRequest, instance of SymfonyComponentHttpFoundationRequest” with this fix. That can be solved by using IlluminateHttpRequest instead of SymfonyComponentHttpFoundationRequest and use Request::capture(); instead of Request::createFromGlobals(); Reply
Just a note – when doing anything usefull you might encounter an error like “must be an instance of IlluminateHttpRequest, instance of SymfonyComponentHttpFoundationRequest” with this fix.
That can be solved by using
IlluminateHttpRequest instead of SymfonyComponentHttpFoundationRequest
and use Request::capture(); instead of Request::createFromGlobals();
ask a lawyer 4 years ago They should add your article…I spend almost 2~3 hours try to fix this by changing the .htaccess Reply
They should add your article…I spend almost 2~3 hours try to fix this by changing the .htaccess
expired domains with traffic 4 years ago it works, thanks Reply
it works, thanks
tripper54 3 years ago Thank you, this is the best solution I’ve seen! Reply
Thank you, this is the best solution I’ve seen!
Ibrahim Lawal 3 years ago Awesome solution. For Lumen 5.3, this will fail, so any one trying this now should use “IlluminateHttpRequest” instead of “SymfonyComponentHttpFoundationRequest” Hope this helps someone Reply
Awesome solution.
For Lumen 5.3, this will fail, so any one trying this now should use “IlluminateHttpRequest” instead of “SymfonyComponentHttpFoundationRequest”
Hope this helps someone
Manajemen baterai dan Sleep mode yang failed pada sebagian Ubuntu (dan distro linux lain) June 4, 2019
Running Synergy With Extended Monitor September 2, 2016
Running Laravel Lumen from sub-directory April 15, 2015
Tutorial Semantik Web + Ontology Building menggunakan protege July 3, 2014
Konfigurasi Permalink WordPress untuk NGINX July 16, 2013