gpt4 book ai didi

php - Laravel 路由和路由参数

转载 作者:搜寻专家 更新时间:2023-10-31 21:27:31 24 4
gpt4 key购买 nike

我在我的 route.php 文件中定义了路由,如下所示:

Route::get('{test1}/{test2}/{test3}', function($test1, $test2, $test3) {
$result = [$test1, $test2, $test3];
return view('view', compact('result'));
});

它在我的 Controller 中工作正常,但在 View 部分,当我在浏览器中看到它时,当我在浏览器中写这样的东西时:

http://localhost/mysitefolder/public/test1/test2/test3

它加载 View 并传递所有数据,但它从如下 url 获取我的所有 Assets ,如我的样式表、图像和脚本:

http://localhost/mysitefolder/public/test1/test2/js/jquery.js

为什么会这样?
提前致谢!

最佳答案

两种解决方案要么在 URL 的开头使用 / 以便不使用相对寻址,要么使用 laravel 的辅助函数 {{ asset('/js/script.js') }}.

当您不在您的 url 开头使用“/”时,它会被视为一个相对地址,并且当前位置会添加到它的开头。

如果您的应用程序未在根级别提供服务,有时甚至“/”也不起作用。例如,您的应用程序位于 http://localhost/yourapplication 然后 / 将引用您的本地主机而不是应用程序,这就是为什么最好的方法是使用 laravel 的辅助函数。

关于php - Laravel 路由和路由参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34020194/

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