2014-10-16 148 views
2

我可以通过我的$ app变量(Silex \ Application)调用访问Web根目录和应用程序根目录的函数吗?Silex - 如何访问我的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'); 

我已经找到了我可以使用在树枝模板但没有方便的方法在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()等,但是好奇如果有另一种方式

+0

我有一个'bootstrap'脚本,用于加载一个具有所有'配置目录'作为属性和'成员'的类。这在'silex'之前运行,所以'随处可用'。您可以轻松将其添加到'silex'。它还具有'dev','test'等'runtime_environment'。 – 2014-10-16 16:14:14

回答

8

Twig的app.request.baseurl是在PHP中可用$app["request"]->getBaseUrl()$request->getBaseUrl()如果您有请求注入到您的控制器。

至于应用程序根目录,我一直在我的引导文件中完成define("ROOT", __DIR__ . "/../")

+0

不知道为什么,但getBaseUrl和getBasePath都返回空字符串。然而,Request对象上的其他方法似乎可以正常工作,例如getPathInfo和getRequestUri。 – Carlton 2014-10-17 15:58:10

+0

根解决方案似乎很好,顺便说一下:D – Carlton 2014-10-17 15:58:32

+0

这回答我的原始问题,现在要找到为什么“getBaseUrl”和“getBasePath”返回空字符串 – Carlton 2014-10-21 17:02:01