6

我在一个项目中使用 laravel。在我的本地机器上,我必须访问的服务器只是

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 错误,我不知道为什么。我究竟做错了什么?

4

3 回答 3

8

不需要更改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']);

希望这可以帮助。

额外的

如果您想知道 Lumen 如何在子文件夹中不起作用。你可能会看到Laravel\Lumen\Application::getPathInfo()line 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']
                )
            ),
        '/');
    }
}

然后,在 you 中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();
于 2015-05-29T03:42:35.457 回答
0

在我的情况下,我将 .htaccess 从公共目录移动到根目录,并在我的项目根目录中创建了一个 index.php 文件,它看起来像这样。如果你有一个 laravel 项目,你可以从根目录复制它的 server.php 并将其名称更改为 index.php

索引.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';
于 2018-11-22T05:02:21.653 回答
0

使用 Lumen 5.4 / apache / FPM/FastCGI 测试:

文件夹结构:

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

网络根:/ 网址: http: //www.domain.dev/api

将文件从目录复制/lumen-api/public/api

改变/api/index.php

1) 使用 lumen bootstrap 适应目录的路径

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

2)修复错误的baseURL 在“$app = require __DIR_....”之后添加这个来修复/vendor/symfony/http-foundation/Request.php的错误baseURL:受保护的函数prepareBaseUrl()

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

3)样品/lumen-api/routes/web.php

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

4) 测试http://www.domain.dev/api 结果应为:

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

如果你得到http://www.domain.dev/api/api 两次/api/api- 来自 2) 的 baseURL 修复不起作用!

于 2017-01-25T22:18:05.427 回答