利用 Laravel 5 (Lumen) 中的基本路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30511703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:39:41  来源:igfitidea点击:

Making use of the base path in Laravel 5 (Lumen)

phplaravellaravel-5lumen

提问by Musterknabe

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

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

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

laraveltest.dev. 当我打开此 URL 时,项目运行良好且没有问题。

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/publicthe application always returns an 404 error.

然而,当我上传此测试服务器,那里的东西是位于子foder,像这样的:laraveltest.de/test2/。公用文件夹位于laraveltest.de/test2/public/,但在调用laraveltest.de/test2/public应用程序时总是返回 404 错误。

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

我认为这可能是因为基本路径,所以我在 bootstrap/app.php

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

where env('APP_BASE_PATH')is the subfolder.

env('APP_BASE_PATH')子文件夹在哪里。

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

所以app->basePath()返回/var/www/laraveltest/test2/public。然而,当现在开

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

laraveltest.de/test2/public我总是收到 404 错误,我不知道为什么。我究竟做错了什么?

回答by krisanalfa

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

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

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 .htaccessfile to override specific path configuration?

  • Check your public/index.phpfile. Change this line:

  • 检查 .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']);

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.

如果您想知道 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']
                )
            ),
        '/');
    }
}

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

然后,在你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__.'/../')
);

After this, you don't need to change public/index.phpfile, just let it default:

在此之后,您不需要更改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();

回答by Andreas Heissenberger

tested with Lumen 5.4 / apache / FPM/FastCGI:

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

folder structure:

文件夹结构:

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

Web Root: /URL: http://www.domain.dev/api

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

copy Files from directory /lumen-api/publicto /api

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

change /api/index.php:

改变/api/index.php

1) adapt path to directory with lumen bootstrap

1) 使用 lumen bootstrap 将路径调整到目录

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

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

2) 修复错误的 baseURL在 "$app = require __DIR_...." 之后添加这个以修复 /vendor/symfony/http-foundation/Request.php:protected function prepareBaseUrl() 的错误 baseURL

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

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

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

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

4) Test http://www.domain.dev/apiResult should be:

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

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

if you get http://www.domain.dev/api/apitwo times /api/api- the fix for the baseURL from 2) is not working!

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

回答by Mohsin Raza

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

就我而言,我将 .htaccess 从公共目录移动到根目录,并在我的项目根目录中创建了一个 index.php 文件,它看起来像这样。如果你有一个 Laravel 项目,你可以从 root 复制它的 server.php 并将其名称更改为 index.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';