php - Laravel 路由和路由参数

标签 php laravel routes assets

我在我的 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/

相关文章:

routes - 抛出新的 InvalidArgumentException ("Route [{$name}] not defined."); Laravel 5.6 中的路由错误

ruby-on-rails - 仅设置成员路由时排除 CRUD 资源

javascript - 在提交 PHP 表单之前显示确认提示框

php - Laravel 中 with() 和 compact() 有什么区别

php - 组织帖子数组

javascript - 如何将变量数据传递给 View

php - 我的多对多关系没有返回所需的用户角色

php - 未找到 Laravel excel 类 'ZipArchive'

ruby-on-rails-3 - Rails 将路由与 slugs 匹配,而无需在链接中使用 ID

php - file_get_contents() 突然不起作用