mpet
mpet

Reputation: 1014

Catch exception and redirect

I'm making an installation script for a Laravel application and I'm trying to create admin account as one of the installation steps. When an error arises, the application should redirect back to the same page with an alert message, but Laravel shows error message and stops the application:

public function postAdmin() {

$validation_rules = array(
    'username' => 'required|min:4|alpha_dash|unique:users',
    'email' => 'required|email|unique:users',
    'password' => 'required|min:4',
    );

$validator = Validator::make(Input::all(), $validation_rules);

if ($validator->fails()) {
    return Redirect::to('install/admin')
    ->withInput()
    ->withErrors($validator);
}

else {

    try {
     $username = Input::get('username');
     $email = Input::get('email');
     $password = Hash::make(Input::get('password'));

     $admin_user = new User;
     $admin_user->username = $username;
     $admin_user->email = $email;
     $admin_user->password = $password;
     $admin_user->admin = 1;

     $user_data_saved = $admin_user->save();

     if ($user_data_saved) {
        Auth::attempt(array('email' => Input::get('email'), 'password' => Input::get('password')));
        return Redirect::to('install/finish')->with('success', "Admin account created");
    } 
    else {
        return Redirect::to('install/admin')
        ->with('error', 'Error adding admin info');
    }
}
catch(Exception $e) {
    return Redirect::to('install/admin')->with('error', "Error creating admin account");
}

} }

Is it possible to check for errors using try/catch and redirect if an error arises and how?

Upvotes: 0

Views: 2131

Answers (2)

mpet
mpet

Reputation: 1014

Restructuring try/catch solved the problem for now:

public function postAdmin() {

try {
    $validation_rules = array(
        'username' => 'required|min:4|alpha_dash|unique:users',
        'email' => 'required|email|unique:users',
        'password' => 'required|min:4',
        );

    $validator = Validator::make(Input::all(), $validation_rules);

    if ($validator->fails()) {
        return Redirect::to('install/admin')
        ->withInput()
        ->withErrors($validator);
    }

    else {

        $username = Input::get('username');
        $email = Input::get('email');
        $password = Hash::make(Input::get('password'));

        $admin_user = new User;
        $admin_user->username = $username;
        $admin_user->email = $email;
        $admin_user->password = $password;
        $admin_user->admin = 1;

        $user_data_saved = $admin_user->save();

        if ($user_data_saved) {
            Auth::attempt(array('email' => Input::get('email'), 'password' => Input::get('password')));
            return Redirect::to('install/finish')->with('success', "Admin account created");
        } 
        else {
            return Redirect::to('install/admin')
            ->with('error', 'Error adding admin info');
        }

    }     
}
catch(Exception $e) {
    return Redirect::to('install/admin')->with('error', "Error creating admin account");
} 
 }

Upvotes: 0

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

Laravel catches some errors, so to be able to process them yourself, you need to edit the app/Exceptions/Handler.php and, maybe, use the render method to handle your own exceptions:

public function render($request, Exception $e)
{
    if ($result = MyExceptionHandler::handle($e))
    {
        return $result;
    }

    return parent::render($request, $e);
}

Upvotes: 1

Related Questions