【问题标题】:How can I get current URL using smarty如何使用 smarty 获取当前 URL
【发布时间】:2012-10-19 18:40:38
【问题描述】:

如何使用类似于JavaScript 中的window.locationSmarty 获得URL

我做了这个

{$smarty.server.PHP_SELF} 

但它不起作用。

【问题讨论】:

    标签: smarty


    【解决方案1】:

    你也可以用这个。

    {$smarty.server.HTTP_HOST}{$smarty.server.REQUEST_URI}
    

    【讨论】:

    【解决方案2】:

    这是控制器代码:

    <?php
    require_once('libs/Smarty.class.php');
    $smarty = new Smarty();
    if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
        $pro = 'https';
    } else {
        $pro = 'http';
    }
    $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
    $current_url =  $pro."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
    
    $smarty->assign("current_url",$current_url);
    $smarty->display('index.tpl');
    
    ?>
    

    您可以在 index.tpl 模板文件中显示变量:

    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
            <title>Title</title>
        </head>
        <body>
    
            {$current_url}
    
        </body>
    </html>
    

    【讨论】:

    • 这是一个简单的PHP文件,不用担心,你可以直接使用这段代码
    【解决方案3】:

    看看有什么可用的:

    {$smarty.server|@var_dump}
    

    几个网址示例https://some.example.com/some/path?param=1

    {$smarty.server.SCRIPT_URI}
    

    https://some.example.com/some/path

    {$smarty.server.SERVER_NAME}
    

    some.example.com

    {$smarty.server.SCRIPT_URL}
    

    /一些/路径

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-26
      • 2013-10-09
      • 1970-01-01
      • 2010-11-05
      • 2010-09-29
      相关资源
      最近更新 更多