php - Catch exception and redirect -
i'm making installation script laravel application , i'm trying create admin account 1 of installation steps. when error arises, application should redirect same page alert message, laravel shows error message , stops 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 possible check errors using try/catch , redirect if error arises , how?
laravel catches errors, able process them yourself, need edit app/exceptions/handler.php
and, maybe, use render method handle own exceptions:
public function render($request, exception $e) { if ($result = myexceptionhandler::handle($e)) { return $result; } return parent::render($request, $e); }
Comments
Post a Comment