使用 Laravel 5 中的基本路径 (Lumen)
Making use of the base path in Laravel 5 (Lumen)
我正在项目中使用 laravel。在我的本地机器上,我必须访问的服务器只是
laraveltest.dev
。当我打开这个 URL 时,项目工作正常,没有问题。
但是,当我将其上传到测试服务器时,内容位于子文件夹中,如下所示:laraveltest.de/test2/
。 public 文件夹位于 laraveltest.de/test2/public/
,但在调用 laraveltest.de/test2/public
时应用程序总是 returns 404 错误。
我认为这可能是因为基本路径,所以我在 bootstrap/app.php
中做了以下操作
$app = new Laravel\Lumen\Application(
realpath(__DIR__.'/../') . env('APP_BASE_PATH')
);
其中 env('APP_BASE_PATH')
是子文件夹。
所以app->basePath()
returns/var/www/laraveltest/test2/public
。但是,现在开放时
laraveltest.de/test2/public
我总是收到 404 错误,我不知道为什么。我做错了什么?
您不需要更改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
.
的 class
<?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();
使用 Lumen 5.4/apache/FPM/FastCGI 测试:
文件夹结构:
/api
├── index.php
├── .htaccess
/lumen-api/
├── app
├── bootstrap
..
网络根:/
URL: http://www.domain.dev/api
将文件从目录 /lumen-api/public
复制到 /api
更改/api/index.php
:
1) 将路径调整为带 lumen 的目录 bootstrap
$app = require __DIR__.'/../lumen-api/bootstrap/app.php';
2) 修复错误的碱基URL
在“$app = require __DIR_....”之后添加这个以修复 /vendor/symfony/http-foundation/Request.php 的错误 baseURL:受保护的函数 prepareBaseUrl()
$_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) 中的基础 URL 修复无效!
在我的例子中,我将 .htaccess 从 public 目录移动到根目录,并在我的项目根目录中创建了一个 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';
我正在项目中使用 laravel。在我的本地机器上,我必须访问的服务器只是
laraveltest.dev
。当我打开这个 URL 时,项目工作正常,没有问题。
但是,当我将其上传到测试服务器时,内容位于子文件夹中,如下所示:laraveltest.de/test2/
。 public 文件夹位于 laraveltest.de/test2/public/
,但在调用 laraveltest.de/test2/public
时应用程序总是 returns 404 错误。
我认为这可能是因为基本路径,所以我在 bootstrap/app.php
$app = new Laravel\Lumen\Application(
realpath(__DIR__.'/../') . env('APP_BASE_PATH')
);
其中 env('APP_BASE_PATH')
是子文件夹。
所以app->basePath()
returns/var/www/laraveltest/test2/public
。但是,现在开放时
laraveltest.de/test2/public
我总是收到 404 错误,我不知道为什么。我做错了什么?
您不需要更改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();
使用 Lumen 5.4/apache/FPM/FastCGI 测试:
文件夹结构:
/api
├── index.php
├── .htaccess
/lumen-api/
├── app
├── bootstrap
..
网络根:/
URL: http://www.domain.dev/api
将文件从目录 /lumen-api/public
复制到 /api
更改/api/index.php
:
1) 将路径调整为带 lumen 的目录 bootstrap
$app = require __DIR__.'/../lumen-api/bootstrap/app.php';
2) 修复错误的碱基URL 在“$app = require __DIR_....”之后添加这个以修复 /vendor/symfony/http-foundation/Request.php 的错误 baseURL:受保护的函数 prepareBaseUrl()
$_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) 中的基础 URL 修复无效!
在我的例子中,我将 .htaccess 从 public 目录移动到根目录,并在我的项目根目录中创建了一个 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';