Using the base path in Laravel 5 (Lumen)

I am using laravel in a project. On my local machine, the server I have to access is just

laraveltest.dev . When I open this URL, the project works fine and without problems.

However, when I upload it to a test server, where the material is in a sub-foder, like this: laraveltest.de/test2/ . The laraveltest.de/test2/public/ folder is located in laraveltest.de/test2/public/ , but when you call laraveltest.de/test2/public application always returns a 404 error.

I thought this might be due to the base path, so I did the following in bootstrap/app.php

 $app = new Laravel\Lumen\Application( realpath(__DIR__.'/../') . env('APP_BASE_PATH') ); 

where env('APP_BASE_PATH') is a subfolder.

So app->basePath() returns /var/www/laraveltest/test2/public . However upon opening

laraveltest.de/test2/public I always get 404 error and I don't know why. What am I doing wrong?

+6
source share
2 answers

You do not need to modify basePath unless you are using a custom application structure. Kind:

 bootstrap ├── app.php └── autoload.php config ├── app.php ├── auth.php ├── cache.php ├── compile.php [...] src └── Traviola ├── Application.php ├── Commands │  └── Command.php ├── Console │  ├── Commands [...] 

So, in your case, all you have to do is:

  • Check your .htaccess configuration. Does the .htaccess file server support overriding a specific path configuration?

  • Check the file public/index.php . Change this line:


 /* |--------------------- | 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 browser allowing them to enjoy the creative | and wonderful application we have prepared for them. | */ $app->run(); // into something like this $app->run($app['request']); 

Hope this helps.

Additional

If you are wondering how Lumen does not work in a subfolder. You can see Laravel\Lumen\Application::getPathInfo() line 1359 . For Lumen to work in a subfolder, change this method, just create a class that extends Laravel\Lumen\Application .

 <?php namespace App; use Laravel\Lumen\Application as BaseApplication; class Application extends BaseApplication { /** * Get the current HTTP path info. * * @return string */ public function getPathInfo() { $query = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ''; return '/'.ltrim( str_replace( '?'.$query, '', str_replace( rtrim( str_replace( last(explode('/', $_SERVER['PHP_SELF'])), '', $_SERVER['SCRIPT_NAME'] ), '/'), '', $_SERVER['REQUEST_URI'] ) ), '/'); } } 

Then, in bootstrap/app.php , change this:

 /* |------------------------ | Create The Application |------------------------ | | Here we will load the environment and create the application instance | that serves as the central piece of this framework. We'll use this | application as an "IoC" container and router for this framework. | */ $app = new App\Application( realpath(__DIR__.'/../') ); 

After that, you do not need to modify the public/index.php file, just specify it by default:

 /* |--------------------- | 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 browser allowing them to enjoy the creative | and wonderful application we have prepared for them. | */ $app->run(); 
+6
source

with Lumen 5.4 / apache / FPM / FastCGI:

Folder structure

:

 /api ├── index.php ├── .htaccess /lumen-api/ ├── app ├── bootstrap .. 

Web root: / url: http://www.domain.dev/api

copy files from the /lumen-api/public directory to /api

edit /api/index.php :

1) adapt the directory path using a clearance pin

 $app = require __DIR__.'/../lumen-api/bootstrap/app.php'; 

2) fix incorrect baseURL add this after "$ app = require __DIR _...." to fix baseURL / vendor / symfony / http-foundation / Request.php: protected function prepareBaseUrl () error

 $_SERVER['SCRIPT_NAME']='index.php'; 

3) sample /lumen-api/routes/web.php :

 $app->get('/api/', ['as' => 'home',function () use ($app) { return $app->version() . " - " . route('home'); }]); 

4) Test http://www.domain.dev/api The result should be:

 Lumen (5.4.0) (Laravel Components 5.4.*) - http://www.domain.dev/api 

if you get http://www.domain.dev/api/api two times /api/api - the fix for baseURL from 2) does not work!

0
source

Source: https://habr.com/ru/post/988106/


All Articles