Laravel 从视图重定向到路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23580343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel redirect from view to a route
提问by Niklas
I want to redirect from a view:
我想从视图重定向:
Route::get('/MyRoute', function () {
return View::make('MyView');
});
In MyView I want to execute some JavaScript and then Redirect to a download:
在 MyView 中,我想执行一些 JavaScript,然后重定向到下载:
// some imaginary javascript code, which will get executed
<?php
Redirect::to('/MyRoute2');
I can't seem to find a way, which will work. What am I doing wrong?
我似乎无法找到一种可行的方法。我究竟做错了什么?
Of course I could change the /MyRoute
to:
当然,我可以将其更改/MyRoute
为:
Route::get('/MyRoute', function () {
return Redirect::to('/MyRoute2');
});
but then I can't execute some JavaScript code.
但后来我无法执行一些 JavaScript 代码。
回答by Damien Pirsy
You have to use JS for that, inside your view. All php is handled beforethe page renders, so if you want to redirect after some js code you need to call the redirection from the client-side:
在您的视图中,您必须为此使用 JS。所有的 php 都在页面渲染之前处理,所以如果你想在一些 js 代码之后重定向,你需要从客户端调用重定向:
<script>
// your "Imaginary javascript"
window.location.href = '{{url("yoururl")}}';
// or
window.location.href = '{{route("myRoute")}}'; //using a named route
</script>
回答by Jonathan
You are not returning the Redirect object, just like you must return a View object you must also return the Redirect.
您没有返回 Redirect 对象,就像您必须返回 View 对象一样,您也必须返回 Redirect。
So you will have to return Redirect::to('/MyRoute2');
in your controller.
所以你必须return Redirect::to('/MyRoute2');
在你的控制器中。
Or you could perform the redirect using JS.
或者您可以使用 JS 执行重定向。