【问题标题】:Getting the part of the current URL in Laravel 7/8 [closed]在 Laravel 7/8 中获取当前 URL 的一部分 [关闭]
【发布时间】:2021-09-10 18:59:14
【问题描述】:

如何在没有第一部分的情况下动态获取当前 URL?

例如:

www.google.com/en/second => /second

www.google.com/en/second/third => /second/third

在当前刀片视图中,该函数应该放在哪里或如何实现?

【问题讨论】:

  • 看看这个包:github.com/spatie/url
  • @hmrneves 这和 Symfony/Laravel 提供的功能差不多,不是吗?
  • @shaedrich,是的,基本上

标签: php laravel url laravel-blade


【解决方案1】:

全部http链接:

$link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; // www.google.com/en/second/third

刚刚请求的链接:

$requested_link = "$_SERVER[REQUEST_URI]";  // en/second/third

如果您不想要链接的任何部分,请将其替换为 "":

str_replace("en/", "", $requested_link); // second/third

您可以将此代码放在视图或控制器中的任何位置。例如在视图中:

<?php
function get_url(){
   $requested_link = "$_SERVER[REQUEST_URI]";
   return str_replace("en/", "", $requested_link);
}
?>

【讨论】:

    【解决方案2】:

    你可以使用Request::segments:

    implode('/', array_slice(request()->segments(), 1));
    

    【讨论】:

      【解决方案3】:

      获取包含查询字符串的当前 URL...

      echo url()->full();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-20
        • 1970-01-01
        • 2013-10-27
        • 1970-01-01
        • 2021-05-29
        • 2019-03-10
        • 2019-09-06
        • 2013-06-03
        相关资源
        最近更新 更多