Reputation: 3081
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.
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();
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;
}
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!
So my questions are:
Upvotes: 4
Views: 3191
Reputation: 14202
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.
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');
}
Upvotes: 8