开发者

Get Symfony URL relative to the index file

I have a symfony project which, because of DNS issues, is http://<project-name>/ locally, but it needs to be http://<qa-host-name>/<project-name>;/ when hosted in a more QA level environment but it may be http://<domain-name>/ for production (so, I need this to work for both). Now, the images folder will always be relative to the <project-name> directory, so locally it will be http://<project-name>/my-smilie.png and on QA it will be http://<qa-host-name>/<project-name>/my-smilie.png

Since everything is relative to the URL of the index.php, I thought that Symfony would have something to create dynamic URLs which work even if the context is different so that my template.php could have something like

<?php echo image_url("my-smilie.png"); 
      /*see below for potential implementation*/?>

and it would output http://<project-name>/my-smilie.png, http://<qa-host-name>/<project-name>/my-smilie.png, or http://<domain-name>/my-smilie.png. (Relative URLs are fine, but absolute would be better).

Below is an example of what I am looking for, but I feel like I am trying to re-invent the wheel and that Symfony has already accomplished this.

function image_url($img)
{
    return get_base_url() . '/images/' . $img;
}


functio开发者_开发百科n get_base_url()
{
    $par = dirname( $_SERVER[ 'SCRIPT_NAME' ] );
    if($par == "/") $par = "";
    return $par;
}


try public_path('images/smilie.jpg') function

public_path() manual


I just came across a situation where the public_path helper does not work terribly well, but there is an alternative:

    // this populates $host with absolue URL of the parent directory 
    // of the Symfony script
    $host = $request->isSecure()? 'http://':'https://';
    $host .= $request->getHost() . $request->getRelativeUrlRoot();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜