我在一个项目中使用拉维。在我的本地计算机上,我必须访问的服务器是
laraveltest.dev当我打开这个url时,项目运行良好,没有问题。
但是,当我在一个测试服务器上上传这个文件时,它就位于一个子文件夹中,比如:laraveltest.de/test2/。公用文件夹位于laraveltest.de/test2/public/,但是当调用laraveltest.de/test2/public时,应用程序总是返回404错误。
我想这可能是因为基路径,所以我在bootstrap/app.php中执行了以下操作

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

其中env('APP_BASE_PATH')是子文件夹。
所以app->basePath()返回/var/www/laraveltest/test2/public。不过,现在开业时
laraveltest.de/test2/public我总是收到404错误,我不知道为什么。我做错什么了?

最佳答案

您不需要更改basePath,除非使用自定义文件夹应用程序结构。有点像这样:

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

所以,就你而言,你要做的就是:
检查htaccess配置。服务器是否允许.htaccess文件覆盖特定的路径配置?
检查您的public/index.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.
|
*/

$app->run();

// into something like this
$app->run($app['request']);

希望这有帮助。
附加的
如果你想知道流明如何不工作在子文件夹。您可以看到Laravel\Lumen\Application::getPathInfo()1359。要使lumen在子文件夹中工作,请更改此方法,只需创建一个扩展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']
                )
            ),
        '/');
    }
}

然后,在您的bootstrap/app.php中,更改以下内容:
/*
|------------------------
| 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__.'/../')
);

在此之后,您无需更改public/index.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.
|
*/

$app->run();

07-28 07:33