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

Mus*_*abe 6 php laravel laravel-5 lumen

我在一个项目中使用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')
);
Run Code Online (Sandbox Code Playgroud)

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

所以app->basePath()回报/var/www/laraveltest/test2/public.但是,现在开放的时候

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

kri*_*lfa 7

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

bootstrap
??? app.php
??? autoload.php
config
??? app.php
??? auth.php
??? cache.php
??? compile.php
[...]
src
??? Traviola
    ??? Application.php
    ??? Commands
    ?   ??? Command.php
    ??? Console
    ?   ??? Commands
    [...]
Run Code Online (Sandbox Code Playgroud)

所以,在你的情况下,你所要做的就是:

  • 检查.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']);
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

额外

如果你想知道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']
                )
            ),
        '/');
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,在你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__.'/../')
);
Run Code Online (Sandbox Code Playgroud)

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