简体   繁体   English

Slim Framework基本URL

[英]Slim Framework Base URL

I'm new to Slim Framework. 我是Slim Framework的新手。 How to get the base URL like with the Codeigniter function base_url() ? 如何像使用Codeigniter函数base_url()一样获得基本URL?

Thanks 谢谢

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

With Slim v3, as it implements PSR7, you can get a PSR7 Uri object and call the getBasePath() method that Slim3 adds on it. 使用Slim v3,由于它实现了PSR7,因此您可以获得PSR7 Uri对象并调用Slim3在其上添加的getBasePath()方法。 Simply write: 只需写:

$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. 如果Slim应用程序的前控制器位于文档根目录下的物理子目录中,则可以使用Uri对象的getBasePath()方法获取HTTP请求的物理基本路径(相对于文档根目录)。 This will be an empty string if the Slim application is installed in the document root's top-most directory. 如果将Slim应用程序安装在文档根目录的最顶层目录中,则该字符串为空。

Be aware that the getBasePath() method is added by the framework and is not part of PSR7 UriInterface . 请注意,getBasePath()方法是由框架添加的,并且不是PSR7 UriInterface的一部分。

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.localhost and another can have it at http://localhost/projects/slim and no configuration is required. 使用$app->request->getScriptName()的优点是,我们不必手动设置文件夹名称或关心文件夹的名称–一个开发人员可以将存储库放置在http://example.localhost而另一个可以在http://localhost/projects/slim ,不需要任何配置。

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 ));
});

I can get the base url with {{ app.request.getRootUri }} (I'm using Twig template engine). 我可以使用{{ app.request.getRootUri }}获取基本网址(我正在使用Twig模板引擎)。 Incidentally, this is the same as the 'SCRIPT_NAME' environment variable. 顺便说一句,这与“ SCRIPT_NAME”环境变量相同。

if you are using TWIG then in Slim v3 call - 如果您使用的是TWIG,则在Slim v3中调用-

{{ base_url() }}

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

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()."/"; $req = $app->request; $base_url = $req->getUrl()."".$req->getRootUri()."/";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM