gpt4 book ai didi

php - Silex - 如何访问我的网络根目录和应用程序根目录

转载 作者:行者123 更新时间:2023-12-02 06:58:32 25 4
gpt4 key购买 nike

有没有我可以通过 $app 变量 (Silex\Application) 调用的函数来访问 Web 根目录和应用程序根目录?

假设我的项目布局是这样的

/src    <-- Contains bootstrap.php (register all required services), controllers.php (routes)
/vendor <-- Contains autoload.php and Silex source
/web <-- Contains index.php, Creates $app, includes app/bootstrap.php and $app->run()

在 src/controllers.php 中,我可能想引用我的文档根目录或应用程序根目录

$app
->get('/', function () use ($app) {
// Would like to do something like
$webRoot = $app->getWebRoot();

return $app['twig']->render('home.html.twig');
})
->bind('home');

我找到了一些可以在 Twig 模板中使用但在 PHP 中找不到的便捷方法

<link href="{{ app.request.baseUrl }}/libs/bootstrap-3.2.0-dist/css/bootstrap.min.css" media="screen" rel="stylesheet" type="text/css" />

我可以使用 __DIR__、dirname() 等,但很好奇是否还有其他方法

最佳答案

Twig 的 app.request.baseurl 在 php 中可用 $app["request"]->getBaseUrl()$request->getBaseUrl( ) 如果您已将请求注入(inject) Controller 。

至于应用程序根目录,我总是在我的 Bootstrap 文件中完成 define("ROOT", __DIR__ . "/../")

关于php - Silex - 如何访问我的网络根目录和应用程序根目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26403810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com