Laravel:在自定义类中重定向? [英] Laravel: Redirect within custom class?
问题描述
我不确定这是正确"的处理方式,但是逻辑在起作用.我在Laravel设置中有自己的类,我在控制器中use
.在我的控制器中,我在自定义类中调用了一个函数,但是,如果该函数中发生某些事情,我想重定向用户.
I'm not sure this is the "proper" way of doing things, but the logic is working. I have my own class in my Laravel setup, which I use
in a controller. Within my controller I call a function in my custom class, however I'd like to Redirect the user if something happens within that function.
在IRC上聊天后,我被告知您不能在自己的类中执行重定向,您必须从控制器中返回重定向响应对象 ".
After chatting on IRC, I'm told you can't do a Redirect within your own class, you have to "return the redirect response object from the controller".
不能完全确定这是什么意思,但是我想您必须从控制器执行重定向.
Not entirely sure what this means, but I guess you have to do the redirects from the controller instead.
代码(简体,正在运行):
Code (simplified, it is working):
Controller method:
// Validate the incoming user
$v = new SteamValidation( $steam64Id );
// Check whether they're the first user
$v->checkFirstTimeUser();
这将转到我的SteamValidation类(app/Acme/Steam/SteamValidation.php和命名空间),并进行检查:
This goes away to my SteamValidation class (app/Acme/Steam/SteamValidation.php and namespaced), and it does a check:
public function checkFirstTimeUser() {
// Is there any users?
if( \User::count() == 0 ) {
$user_data = [
// Data
];
// Create the new user
$newUser = \User::create( $user_data );
// Log that user in
\Auth::login($newUser);
// Redirect to specific page
return \Redirect::route('settings');
}
return;
}
现在,如果计数超过0,则它将仅返回到控制器,而我很乐意继续.但是,如果它是新用户,而我尝试进行重定向(return \Redirect::route('settings');
),则会得到一个空白页面!
Now if the count is over 0, then it just returns back to the controller and I happily carry on. However, if it's a new user and I try to do a redirect (return \Redirect::route('settings');
) I get a blank page!
所以我的问题是:
- 为什么我不能从这里重定向?
- 将响应返回给控制器然后进行重定向的正确方法是什么?
推荐答案
无法从嵌套方法进行重定向的原因是,仅调用Redirect :: route()不会触发重定向.控制器的方法将返回Laravel然后确定要执行的操作-如果是View,它将显示它,如果是Redirect,它将执行重定向.在您的嵌套方法中,您可以返回所需的内容,但是只要在控制器上没有将其传递下去,那对您就没有好处.
The reason you can't redirect from your nested method is that simply calling Redirect::route() does not fire a redirect off. Your controller's method will return something Laravel then looks at to decide what to do - if it'a a View it'll display it, if it's a Redirect it'll do a redirect. In your nested method, you can return what you want but as long at the controller it's not passing that down the line then it's no good for you.
此外,您可能也不应该在辅助函数中返回重定向.如果您的validate函数有一个布尔响应(是的,没什么不好),那么您可以简单地返回true/false,然后在控制器中进行选择以进行重定向:
Also you probably shouldn't return a Redirect in a helper function anyway. If your validate function has a boolean response (yes all good, no something's bad) then you can simply return true/false and then pick that up in the controller to do the redirect:
// Validate the incoming user
$v = new SteamValidation( $steam64Id );
// Check whether they're the first user
if ($v->isFirstTimeUser()) { // note I renamed this method, see below
return \Redirect::route('settings');
}
但是,在我们承担重定向重定向验证方法的责任时,您还应该承担创建用户的责任:
However, while we're taking the responsibility of redirecting away from your validation method, you should also take the responsibility of creating the user away:
// SteamValidation
public function isFirstTimeUser() {
return (\User::count() == 0);
}
// Controller
// Validate the incoming user
$v = new SteamValidation( $steam64Id );
// Check whether they're the first user
if ($v->isFirstTimeUser()) {
// you may even wish to extract this user creation code out to something like a repository if you wanna go for it
$user_data = [
// Data
];
// Create the new user
$newUser = \User::create( $user_data );
// Log that user in
\Auth::login($newUser);
// Redirect to specific page
return \Redirect::route('settings');
}
这篇关于Laravel:在自定义类中重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!