Dre*_*lon 18
您需要先手动设置基本URL,然后才能获得它,如下所示:
$app->hook('slim.before', function () use ($app) {
$app->view()->appendData(array('baseUrl' => '/base/url/here'));
});
Run Code Online (Sandbox Code Playgroud)
Al *_* ѫ 13
使用Slim v3,因为它实现了PSR7,你可以获得一个PSR7 Uri对象并调用Slim3在其上添加的getBasePath()方法.只需写:
$basePath = $request->getUri()->getBasePath();
Run Code Online (Sandbox Code Playgroud)
基本路径
如果Slim应用程序的前端控制器位于文档根目录下的物理子目录中,则可以使用Uri对象的getBasePath()方法获取HTTP请求的物理基本路径(相对于文档根目录).如果Slim应用程序安装在文档根目录的最顶层目录中,那么这将是一个空字符串.
请注意,框架添加了getBasePath()方法,而不是PSR7 UriInterface的一部分.
在最近我们使用Twig的应用程序中,我们按如下方式分配httpBasePath:
$view = $app->view()->getEnvironment();
$view->addGlobal('httpBasePath', $app->request->getScriptName());
Run Code Online (Sandbox Code Playgroud)
这个addGlobal()方法可能相当于$app->view()->appendData(),我不确定.
使用的优点$app->request->getScriptName()是我们不必手动设置文件夹名称或关心它是什么 - 一个开发人员可以将repo放在哪里http://example.localhost,另一个可以拥有它,http://localhost/projects/slim并且不需要配置.