使用Laravel将表单保存到数据库后如何重定向页面?

时间:2014-10-28 06:44:20

标签: php url laravel

我有这个问题。我正在学习Laravel,我正在建立一个表格。我的简单场景就是这样。用户完成注册表后,需要单击“提交”按钮。然后,如果有验证它应该显示,表单不应该保存。如果没有错误,它应该保存。并重定向到主页。我的错误是这样,在保存数据库中的数据后,我无法将页面重定向到索引。它将显示错误403禁止页面。

这是我路线中的代码:

Route::model('employee','Employee');

Route::get('/','EmployeesController@index');
Route::get('/register', 'EmployeesController@register');
Route::get('/handleRegister', 'EmployeesController@handleRegister');

Route::post('/handleRegister', function() 
    {

        $rules = array(
            'emp_code'      =>  'numeric',
            'lastname'      =>  'required|min:2|max:15',
            'firstname'     =>  'required|min:2|max:20',
            'middlename'    =>  'min:2|max:20',
            'password'      =>  'required|min:8|max:30',
            'cpassword'     =>  'required|same:password'
        );

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

        if($validator->fails()) {

            $messages = $validator->messages();

            return Redirect::to('register')
                                ->withErrors($messages)
                                ->withInput(Input::except('password','cpassword'));

        } else {

            $employee = neW Employee;

            $employee->emp_code     = Input::get('emp_code');
            $employee->lastname     = Input::get('lastname');
            $employee->firstname    = Input::get('firstname');
            $employee->middlename   = Input::get('middlename');
            $employee->gender       = Input::get('gender');
            $employee->birthday     = Input::get('birthday');
            $employee->password     = Hash::make(Input::get('password'));

            $employee->save();

            return Redirect::action('EmployeesController@index');

        }

    }
);

这是我的索引函数:

public function index()
    {
        return View::make('index', array(
            'page_title'    => 'Flax: Food Ordering',
            'login_footer'  => 'Flax Inc. @ '. date('Y'),
            'register_link' => action('EmployeesController@register')
        ));
    }

然后在我的浏览器中我有这个:

Forbidden

You don't have permission to access /http://dev.flax_order.local on this server.

Apache/2.4.9 (Win64) OpenSSL/1.0.1g PHP/5.5.12 Server at dev.flax_order.local Port 80

在网址中我注意到了这一点:

http://dev.flax_order.local/http://dev.flax_order.local

它使链接加倍。

我不知道我的错误在哪里。你能帮我解决这个问题吗?

顺便说一句,这是运行

的路线
php artisan routes

C:\wamp\vhosts\flax_order>php artisan routes
+--------+---------------------+------+------------------------------+----------------+---------------+
| Domain | URI                 | Name | Action                       | Before Filters | After Filters |
+--------+---------------------+------+------------------------------+----------------+---------------+
|        | GET|HEAD /          |      | EmployeesController@index    |                |               |
|        | GET|HEAD register   |      | EmployeesController@register |                |               |
|        | POST handleRegister |      | Closure                      |                |               |
+--------+---------------------+------+------------------------------+----------------+---------------+

1 个答案:

答案 0 :(得分:-1)

尝试更改

return Redirect::action('EmployeesController@index');

成:

return Redirect::to('/');

虽然我在这里看不到错误,但是主机名中的下划线会导致此问题。