gpt4 book ai didi

php - Laravel:按名称从路由获取 URL

转载 作者:IT王子 更新时间:2023-10-28 23:52:34 26 4
gpt4 key购买 nike

我正在尝试做一些不同的事情,但找不到任何方法。也许我的方法是错误的,但无论哪种方式,我都认为我可能会在这里找到一些帮助。

我有一个 Laravel 5 项目,你知道如何使用以下方法获取当前路由名称:

\Request::route()->getName();

所以我实际上想做恰恰相反的事情。也许不是完全相反,但我需要的是根据我为该路由指定的名称检索我的路由 URL。这是我的梦想场景。

我的 routes.php:

Route::any('/hos', "HospitalController@index")->name("hospital");

我在我的 Controller 中想做但我不知道如何做,甚至不知道是否可能做的事情:

// I have no idea if this is possible but thats what I'm trying to accomplish
$my_route_url = \Request::route()->getURLByName("hospital");

echo $my_route_url; // this would echo: "/hos"

我可能在这里使用了错误的方法,所以也许你们可以帮助我解决这个问题。

谢谢!

最佳答案

$url = route('routeName');

如果有参数

$url = route('routeName', ['id' => 1]);

https://laravel.com/docs/5.1/helpers#method-route

关于php - Laravel:按名称从路由获取 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35752155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com