简体   繁体   中英

Making use of 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 this on a testing server, where the stuff is located in a sub-foder, like this: laraveltest.de/test2/ . The public folder is at laraveltest.de/test2/public/ , but when calling laraveltest.de/test2/public the application always returns an 404 error.

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

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

where env('APP_BASE_PATH') is the subfolder.

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

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

You don't need to change basePath , except if you use custom folder application structure. Kinda like this:

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 .htaccess configuration. Does server allow .htaccess file to override specific path configuration?

  • Check your public/index.php file. 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's 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 wonder how Lumen does not work in subfolder. You may see Laravel\\Lumen\\Application::getPathInfo() line 1359 . To make Lumen works in subfolder, change this method, just make 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 you 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 this, you don't need to change public/index.php file, just let it 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's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/

$app->run();

tested 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 directory /lumen-api/public to /api

change /api/index.php :

1) adapt path to directory with lumen bootstrap

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

2) fix wrong baseURL add this after "$app = require __DIR_...." to fix wrong baseURL of /vendor/symfony/http-foundation/Request.php:protected function prepareBaseUrl()

$_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 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 the baseURL from 2) is not working!

In my case I moved .htaccess from public directory to root directory and created an index.php file in my project root and its looks like this. If you have a laravel project you can copy its server.php from root and change its name to index.php

index.php

<?php

$uri = urldecode(
    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);

if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
    return false;
}

require_once __DIR__.'/public/index.php';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM