卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16333本站已运行3317

在Laravel的@if语句中如何获取当前URL?

在Laravel的@if语句中如何获取当前URL?

要获取当前URL,您可以使用下面示例中解释的方法−

示例1

<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsUser; use IlluminateHttpResponse; class UserController extends Controller { public function index(Request $request) { return view('test'); } } 

test.blade.php 是−

<!DOCTYPE html> <html> <head> <style> body {
         font-family: 'Nunito', sans-serif;
      } </style> </head> <body class="antialiased"> <div> @if (Request::path() == 'users') <h1>The path is users</h1> @endif </div> </body> </html> 

test.blade.php 文件中,使用 Request::path() 来检查是否指向用户,然后只显示 h1 标签。 Request::path() 返回当前正在使用的 URL

示例2

在这个例子中,让我们使用 url()->current() 方法,如下面的例子所示。 url()->current() 返回当前URL的完整路径。

<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsUser; class UserController extends Controller { public function index(Request $request) { return view('test'); } } 

Test.blade.php

<!DOCTYPE html> <html> <head> <style> body {
         font-family: 'Nunito', sans-serif;
      } </style> </head> <body class="antialiased"> <div> @if (url()->current() == 'http://localhost:8000/users') <h1>The path is users</h1> @endif </div> </body> </html> 

在执行上面的示例时,它会在浏览器上打印以下内容−

The path is users

示例3

在这个例子中,我们将使用 Request::url()。它的输出与 url()->current() 相同,它返回完整的URL,如下面的例子所示−

<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsUser; class UserController extends Controller { public function index(Request $request) { return view('test'); } } 

Test.blade.php

<!DOCTYPE html> <html> <head> <style> body { font-family: 'Nunito', sans-serif; } </style> </head> <body class="antialiased"> <div> @if (Request::url() == 'http://localhost:8000/users') <h1>The path is users</h1> @endif </div> </body> </html> 

在执行上面的示例时,它会在浏览器上打印以下内容−

The path is users

示例4

使用 Request::is()

<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsUser; class UserController extends Controller{ public function index(Request $request) { return view('test'); } } 

Test.blade.php

<!DOCTYPE html> <html> <head> <style> body { font-family: 'Nunito', sans-serif; } </style> </head> <body class="antialiased"> <div> @if (Request::is('users')) <h1>The path is users</h1> @endif </div> </body> </html> 

在上面的示例中,Request::is() 被使用。它会返回 true/false,表示给定的字符串是否存在于 URL 中。

在执行上面的示例时,它会在浏览器上打印以下内容−

The path is users


卓越飞翔博客
上一篇: go语言dns是异步的吗
下一篇: php怎么批量正则数字替换
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏