首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在使用Laravel将表单保存到数据库后重定向页面?

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

Stack Overflow用户
提问于 2014-10-28 06:44:20
回答 1查看 3.2K关注 0票数 0

我对此有意见。我正在学习拉拉维尔,我正在制作一份表格。我的简单场景是这样的。当用户完成他/她的注册表格后,他们需要点击提交按钮。然后,如果有一个验证,它应该显示,表单不应该保存。如果没有错误,它应该保存。然后重定向到主页。我的错误是,在将数据保存在数据库中之后,我无法将页面重定向到索引。它将显示一个错误403禁止页面。

下面是我路线上的代码:

代码语言:javascript
复制
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');

        }

    }
);

这是我的索引函数:

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

然后,在我的浏览器中,我看到了以下内容:

代码语言:javascript
复制
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

在URL中,我注意到以下内容:

代码语言:javascript
复制
http://dev.flax_order.local/http://dev.flax_order.local

它使链接加倍。

我不知道我的错误在哪里。你能帮我做这个吗?

顺便说一句,这是跑步的路线

代码语言:javascript
复制
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                      |                |               |
+--------+---------------------+------+------------------------------+----------------+---------------+
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-28 07:02:45

试着改变

代码语言:javascript
复制
return Redirect::action('EmployeesController@index');

转入:

代码语言:javascript
复制
return Redirect::to('/');

虽然我在这里没有看到错误,但是主机名中的下划线可能会导致这个问题。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26602334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档