php Slim 框架基本 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11481210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:36:48  来源:igfitidea点击:

Slim Framework Base URL

phpslim

提问by Al Foиce ?

I'm new to Slim Framework. How to get the base URL like with the Codeigniter function base_url()?

我是 Slim 框架的新手。如何使用 Codeigniter 函数获取基本 URL base_url()

Thanks

谢谢

回答by Dreaded semicolon

You need to set the base url manually FIRST before you can get it as in this:

您需要首先手动设置基本网址,然后才能获得它,如下所示:

$app->hook('slim.before', function () use ($app) {
    $app->view()->appendData(array('baseUrl' => '/base/url/here'));
});

http://help.slimframework.com/discussions/questions/49-how-to-deal-with-base-path-and-different-routes

http://help.slimframework.com/discussions/questions/49-how-to-deal-with-base-path-and-different-routes

回答by Al Foиce ?

With Slim v3, as it implements PSR7, you can get a PSR7 Uri object and call the getBasePath() method that Slim3 adds on it. Simply write:

使用 Slim v3,因为它实现了 PSR7,您可以获得一个 PSR7 Uri 对象并调用 Slim3 添加到它上面的 getBasePath() 方法。简单地写:

$basePath = $request->getUri()->getBasePath();

From Slim v3 documentation:

来自Slim v3 文档

Base Path

If your Slim application's front-controller lives in a physical subdirectory beneath your document root directory, you can fetch the HTTP request's physical base path (relative to the document root) with the Uri object's getBasePath() method. This will be an empty string if the Slim application is installed in the document root's top-most directory.

基本路径

如果您的 Slim 应用程序的前端控制器位于文档根目录下的物理子目录中,您可以使用 Uri 对象的 getBasePath() 方法获取 HTTP 请求的物理基本路径(相对于文档根)。如果 Slim 应用程序安装在文档根目录的最顶层目录中,这将是一个空字符串。

Be aware that the getBasePath() method is added by the framework and is not part of PSR7 UriInterface.

请注意 getBasePath() 方法是由框架添加的,而不是PSR7 UriInterface 的一部分。

回答by Charlie Schliesser

In a recent app where we're using Twig, we assign the httpBasePath as follows:

在我们最近使用 Twig 的应用程序中,我们按如下方式分配 httpBasePath:

$view = $app->view()->getEnvironment();
$view->addGlobal('httpBasePath', $app->request->getScriptName());

The addGlobal()method is probably equivalent to $app->view()->appendData(), I'm not sure.

addGlobal()方法可能等效于$app->view()->appendData(),我不确定。

The advantage of using $app->request->getScriptName()is that we don't have to manually set a folder name or care what it is – one developer can have the repo located at http://example.localhostand another can have it at http://localhost/projects/slimand no configuration is required.

使用的好处$app->request->getScriptName()是我们不必手动设置文件夹名称或关心它是什么 - 一个开发人员可以将 repo 放在http://example.localhost,另一个可以在http://localhost/projects/slim,不需要配置。

回答by qwertz

Try this in index.php to set the base url for the view

在 index.php 中试试这个来设置视图的基本 url

$app->hook('slim.before', function () use ($app) {
    $posIndex = strpos( $_SERVER['PHP_SELF'], '/index.php');
    $baseUrl = substr( $_SERVER['PHP_SELF'], 0, $posIndex);
    $app->view()->appendData(array('baseUrl' => $baseUrl ));
});

回答by Francesco Nardone

I can get the base url with {{ app.request.getRootUri }}(I'm using Twig template engine). Incidentally, this is the same as the 'SCRIPT_NAME' environment variable.

我可以获得基本网址{{ app.request.getRootUri }}(我使用的是 Twig 模板引擎)。顺便说一下,这与“SCRIPT_NAME”环境变量相同。

回答by HADI

if you are using TWIG then in Slim v3 call -

如果您使用的是 TWIG,那么在 Slim v3 调用中 -

{{ base_url() }}

or use {{ path_for('yourRouteName') }}

或使用 {{ path_for('yourRouteName') }}

回答by Mutinda Boniface

The easiest way to get the base url is to append the request url and the request root url like below: $req = $app->request; $base_url = $req->getUrl()."".$req->getRootUri()."/";

获取基本 url 的最简单方法是附加请求 url 和请求根 url,如下所示: $req = $app->request; $base_url = $req->getUrl()."".$req->getRootUri()."/";