要获取当前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 是−
body { font-family: 'Nunito', sans-serif; }@if (Request::path() == 'users')The path is users
@endif
登录后复制
在 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
body { font-family: 'Nunito', sans-serif; }@if (url()->current() == 'http://localhost:8000/users')The path is users
@endif
登录后复制
在执行上面的示例时,它会在浏览器上打印以下内容−
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
body { font-family: 'Nunito', sans-serif; }@if (Request::url() == 'http://localhost:8000/users')The path is users
@endif
登录后复制
在执行上面的示例时,它会在浏览器上打印以下内容−
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
body { font-family: 'Nunito', sans-serif; }@if (Request::is('users'))The path is users
@endif
登录后复制
在上面的示例中,Request::is() 被使用。它会返回 true/false,表示给定的字符串是否存在于 URL 中。
在执行上面的示例时,它会在浏览器上打印以下内容−
The path is users
登录后复制登录后复制登录后复制
以上就是在Laravel的@if语句中如何获取当前URL?的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/1794243.html