laravel 如何从控制器方法重定向到路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45440446/
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
How to redirect to a route from a controller method
提问by Muhammad
I have defined a method in my controller, where inputs are first retrieved, and if the email field is present in my database, I would like to return a view. However, if the email field isn't present, I would like to redirect to another route. I would also like to pass in the inputs to that route as well.
我在我的控制器中定义了一个方法,首先检索输入,如果我的数据库中存在电子邮件字段,我想返回一个视图。但是,如果电子邮件字段不存在,我想重定向到另一条路线。我也想将输入传递给该路线。
To better understand what I mean, my code is as follows for my controller:
为了更好地理解我的意思,我的控制器代码如下:
public function index(Request $request) {
$credentials = $request->all();
if (\App\User::where('email','=',$credentials['email'])->exists()){
//if they are registered, return VIEW called welcome, with inputs
return view('welcome', $credentials);
}
else{//If the user is not in the database, redirect to '/profile' route,
//but also send their data
return redirect('profile', $credentials);
}
And my web.php is as follows:
而我的 web.php 如下:
Route::post('/profile', function() {
$m = Request::only('email'); //I only need the email
return view('profile', $m);
});
However, this logic fails with errors: 'HTTP status code '1' is not defined'. Is there anyway to do this properly? (i.e. go from my controller method to another route?)
但是,此逻辑因错误而失败:“未定义 HTTP 状态代码‘1’”。有没有办法正确地做到这一点?(即从我的控制器方法转到另一条路线?)
回答by Jithin Jose
You can use redirect()
method.
您可以使用redirect()
方法。
return redirect()->route('route.name')->with(['email'=> '[email protected]']);
Since using with()
with redirect()
will add 'email' to the session (not request). Then retrieve the email with:
由于使用with()
withredirect()
会将“电子邮件”添加到会话(而不是请求)中。然后检索电子邮件:
request()->session()->get('email')
//Or
session('email')
回答by milo526
You will need to define the route to which you want to redirect()
您将需要定义您想要到达的路线 redirect()
return redirect()->route('profile')->with('credentials', $credentials);
The with
option flashes data to the session which can be accessed as if passed to the view directly.
该with
选项将数据闪烁到会话中,可以像直接传递到视图一样访问该会话。
More information regarding the session
and flashing data can be found here.
session
可以在此处找到有关和 闪烁数据的更多信息。
Information regarding flashing data after a redirect can be found here.
可以在此处找到有关重定向后闪烁数据的信息。
In your case you could use:
在您的情况下,您可以使用:
return redirect()->route('profile')->with('email', $credentials['email']);
In your view you could then use it like:
在您看来,您可以像这样使用它:
@if(session()->has('email')
We have the email of the user: {{ session('email') }}
@endif
回答by Oluwatobi Samuel Omisakin
While @JithinJose question gave the answer, I am adding this as answer for those who consider this in the future, and who does not want to deal with getting things like this in the session:
虽然@JithinJose 问题给出了答案,但我将其添加为那些将来考虑这一点并且不想在会话中处理此类问题的人的答案:
A not-recommended way to do this is to call the controller directly from this controller method, and pass the variable needed to it:
一种不推荐的方法是直接从此控制器方法调用控制器,并将所需的变量传递给它:
$request->setMethod('GET'); //set the Request method
$request->merge(['email' => $email]); //include the request param
$this->index($request)); //call the function
This will be okay if the other controller method exists within the same class, otherwise You just have to get the method you need and reuse it.
如果另一个控制器方法存在于同一个类中,这将没问题,否则您只需要获取所需的方法并重用它。
The best recommended way if you want to avoid sessionis Redirect to controller actioni.e:
如果您想避免会话,最好的推荐方法是重定向到控制器操作,即:
return redirect()->action(
'UserController@index', ['email' => $email]
);
I hope it is useful :)
我希望它有用:)
回答by AddWeb Solution Pvt Ltd
Please change your view path like :
请更改您的视图路径,例如:
return view('welcome', compact(credentials));
return redirect()->route('profile')->with('credentials',$credentials);