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

Car*_*ton 2 php twig silex

是否有我可以通过我的$ 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()
Run Code Online (Sandbox Code Playgroud)

在src/controllers.php中,我可能想引用我的doc root或app root

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

        return $app['twig']->render('home.html.twig');
    })
    ->bind('home');
Run Code Online (Sandbox Code Playgroud)

我发现了一些我可以在Twig模板中使用但在PHP中没有便捷方法的东西

<link href="{{ app.request.baseUrl }}/libs/bootstrap-3.2.0-dist/css/bootstrap.min.css" media="screen" rel="stylesheet" type="text/css" />
Run Code Online (Sandbox Code Playgroud)

我可以使用__DIR __,dirname()等,但如果有另一种方式,我很好奇

Mae*_*lyn 8

细枝的app.request.baseurl作为是在PHP可用$app["request"]->getBaseUrl()或者$request->getBaseUrl()如果你有请求注入到你的控制器.

对于app root,我总是define("ROOT", __DIR__ . "/../")在我的bootstrap文件中完成.