简体   繁体   English

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

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

I am using laravel in a project. 我在一个项目中使用laravel。 On my local machine the server I have to access is just 在我的本地机器上,我必须访问的服务器就是

laraveltest.dev . laraveltest.dev When I open this URL the project works fine and without problems. 当我打开这个URL时,该项目工作正常,没有问题。

However, when I upload this on a testing server, where the stuff is located in a sub-foder, like this: laraveltest.de/test2/ . 但是,当我在测试服务器上上传时,其中的东西位于sub- laraveltest.de/test2/ ,如下所示: 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. 公用文件夹位于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 . 所以app->basePath()返回/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. laraveltest.de/test2/public我总是得到404错误,我不知道为什么。 What am I doing wrong? 我究竟做错了什么?

You don't need to change basePath , except if you use custom folder application structure. 并不需要改变basePath ,除非您使用自定义文件夹的应用程序结构。 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. 检查.htaccess配置。 Does server allow .htaccess file to override specific path configuration? 服务器是否允许.htaccess文件覆盖特定的路径配置?

  • Check your public/index.php 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'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. 如果你想知道Lumen在子文件夹中是如何工作的。 You may see Laravel\\Lumen\\Application::getPathInfo() line 1359 . 你可能会看到Laravel\\Lumen\\Application::getPathInfo()1359行。 To make Lumen works in subfolder, change this method, just make a class that extends Laravel\\Lumen\\Application . 要使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.php file, 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();

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 Web Root: / URL: http//www.domain.dev/api

copy Files from directory /lumen-api/public to /api 将文件从目录/lumen-api/public复制到/api

change /api/index.php : 更改/api/index.php

1) adapt path to directory with lumen bootstrap 1)使用流明引导程序调整目录路径

$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() 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/api Result 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/api two times /api/api - the fix for the baseURL from 2) is not working! 如果你得到http://www.domain.dev/api/api 两次/api/api - 来自2的baseURL修复程序无效!

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. 在我的情况下,我将.htaccess从公共目录移动到根目录,并在我的项目根目录中创建了一个index.php文件,它看起来像这样。 If you have a laravel project you can copy its server.php from root and change its name to index.php 如果您有laravel项目,则可以从root用户复制其server.php并将其名称更改为index.php

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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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