利用Laravel 5(Lumen)中的基本路径 [英] Making use of the base path in Laravel 5 (Lumen)

查看:285
本文介绍了利用Laravel 5(Lumen)中的基本路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

laraveltest.dev.当我打开此URL时,项目运行正常,没有问题.

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

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

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

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

I thought this might be because of the base path, so I did the following in the 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.但是,当现在打开

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

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

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

推荐答案

不需要需要更改basePath,除非您使用自定义文件夹应用程序结构.有点像这样:

You don't need to change basePath, except if you use custom folder application structure. 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:

  • 检查.htaccess配置.服务器是否允许.htaccess文件覆盖特定的路径配置?

  • Check .htaccess configuration. Does server allow .htaccess file to override specific path configuration?

检查您的public/index.php文件.更改此行:

Check your public/index.php file. 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']);

希望这会有所帮助.

如果您想知道Lumen如何在子文件夹中不起作用.您可能会看到Laravel\Lumen\Application::getPathInfo()1359.要使Lumen在子文件夹中工作,请更改此方法,只需创建一个扩展Laravel\Lumen\Application的类即可.

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.

<?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中,更改此设置:

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

/*
|------------------------
| 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文件,只需将其设置为默认值即可:

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

/*
|---------------------
| 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();

这篇关于利用Laravel 5(Lumen)中的基本路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆