Peter Jewicz
Peter Jewicz

Reputation: 666

Laravel - Redirect from a Model?

I'm building a simple application with laravel, and I've run into a problem with a redirect that won't work. I currently have a sign up form, that posts to a controller method, that invokes a static method on a model that will take care of validation as well as saving that user to the database.

Here's my code so far.

Routes.php

Route::get('/', function()
{   
    return View::make('index');
});


Route::post('/sign_up', 'userController@create_user');

userController.php

public function create_user()
{
    $data = Input::all();
    USER::validate($data);
}

User.php

public static function validate($input) 
{


$rules = array
(
    'firstname' => 'Required|Min:3|Max:80|Alpha',
    'lastname'  => 'Required|Min:3|Max:80|Alpha',
    'password'  =>'Required|AlphaNum|Between:4,12|Confirmed',
    'password_confirmation'=>'Required|AlphaNum|Between:4,12'
);


$v = Validator::make($input, $rules);
        if( $v->passes() ) 
        {
            $user = new User;
            $user->firstname = Input::get('firstname');
            $user->lastname = Input::get('lastname');
            $user->password = Input::get('password');
            return var_dump($user); // Save user code here
        }

             return Redirect::to('/')->withErrors($v);


}

Now as it stands I just get a blank screen. No errors, I can even change the Redirect:to to any route I want, even one that's not set and still won't give any errors.

I did manage to get it working making the following changes to userController and User:

public function create_user()
{
    $data = Input::all();
    $response = USER::validate($data);

    **if($response == 2)**
    {
    return Redirect::to('/');
    }
}

user.php

$v = Validator::make($input, $rules);
        if( $v->passes() ) 
        {
            $user = new User;
            $user->firstname = Input::get('firstname');
            $user->lastname = Input::get('lastname');
            $user->password = Input::get('password');
            return var_dump($user);
        }

             return 2;


}

This works as expected calling the redirect from the controller instead of the model.

This leads me to a couple of questions.

  1. Why does it work in the controller and not the model, is there a way to make it work as I originally intended?

  2. If not, how am I to grab the errors from the validation?

  3. Is this a poor way to layout my application? I'm trying to maintain the logic steps in the model, but so far it would seem to be easier just throwing that into the controller and being done with it.

Thanks for any responses.

Upvotes: 1

Views: 5359

Answers (2)

Wistar
Wistar

Reputation: 3790

You miss a return in your controller. You should return the result` of the static function.

However, I would split it so you can have everything in your controller because it's fairly simple. If you are having more code, you may want to consider to create a UserValidation class.

Also, I'd put the rules in a config files that you can just call from anywhere. That way you don't hardcode your rules within your validation and you can edit all validations rules of your app within a file.

Controller

public function create_user()
{
    $data = Input::all();
    USER::validate($data);

    $v = Validator::make($input, \Config::get('Foo.Rules'));
    if( $v->passes() ) 
    {
        self::insert_user($data);
    }

         return Redirect::to('/')->withErrors($v);
 }

 //This could be in a user validation class
public function insert_user($data){

    $user = new User;
    $user->firstname = Input::get('firstname');
    $user->lastname = Input::get('lastname');
    $user->password = Input::get('password');
    return var_dump($user); // Save user code here
}

Config Foo

$rules = array
(
    'firstname' => 'Required|Min:3|Max:80|Alpha',
    'lastname'  => 'Required|Min:3|Max:80|Alpha',
    'password'  =>'Required|AlphaNum|Between:4,12|Confirmed',
    'password_confirmation'=>'Required|AlphaNum|Between:4,12'
),

Upvotes: 1

andyw_
andyw_

Reputation: 500

  1. The controller is the one that that's meant to return a response, not the Validator - you could potentially pass this back to the controller then make the controller return it - but that's not really its purpose.

  2. You could potentially return the Validator object itself:

return $this;

and then in your controller you could get the error messages

$error = $response->messages();

  1. It's usually a good idea to keep things separate to keep your application DRY, this makes finding and fixing bugs easier - but this is really up to you - over engineering can also be a problem.

Upvotes: 0

Related Questions