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

时间:2015-05-28 16:02:44

标签: php laravel laravel-5 lumen

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

num_square = reactive({input$num^2}) observe(print(num_square())) 。当我打开这个URL时,项目工作正常,没有问题。

但是,当我在测试服务器上上传时,这些东西位于子编辑器中,如下所示:laraveltest.dev。公用文件夹位于laraveltest.de/test2/,但在调用laraveltest.de/test2/public/时,应用程序始终返回404错误。

我认为这可能是因为基本路径,所以我在laraveltest.de/test2/public

中执行了以下操作
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我总是收到404错误而且我不知道为什么。我做错了什么?

3 个答案:

答案 0 :(得分:6)

需要更改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()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();

答案 1 :(得分:0)

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

文件夹结构:

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

Web Root:/ 网址:http://www.domain.dev/api

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

更改/api/index.php

1)使用流明引导程序调整目录路径

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

2)修复错误的baseURL 在&#34; $ app = require __DIR _....&#34;之后添加此项修复/vendor/symfony/http-foundation/Request.php:protected function prepareBaseUrl()

的错误baseURL
$_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修复程序无效!

答案 2 :(得分:0)

在我的情况下,我将.htaccess从公共目录移到了根目录,并在项目根目录中创建了一个index.php文件,其外观如下所示。如果您有laravel项目,则可以从根目录复制其server.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';