提交表格时在laravel中路由

时间:2014-03-12 07:32:04

标签: php mysql laravel

我刚开始拉拉维拉。我能够从mysql表中获取数据,但我无法发布数据。我很难为post url定义路线

MODEL:Entry.php


<?php

class Entry extends Eloquent {

        /**
         * The database table used by the model.
         *
         * @var string
         */
        protected $table = 'entries';

}

CONTROLLER:EntriesController.php


class EntriesController extends BaseController {

        # Handles "GET /" request
        public function getIndex()
        {
            return View::make('home')->with('entries', Entry::all());
        }

        # Handles "POST /"  request
        public function postIndex()
        {
            // get form input data
    $entry = array(
        'username' => Input::get('frmName'),
        'email'    => Input::get('frmEmail'),
        'comment'  => Input::get('frmComment'),
    );

    // save the guestbook entry to the database
    Entry::create($entry);

    return Redirect::to('/');

        }

}

查看:entry.blade.php


<HTML>
<HEAD>
    <TITLE>Laravel Guestbook</TITLE>
</HEAD>
<BODY>
    @foreach($entries as $entry)
      <p>{{ $entry->comment }}</p>
      <p>Posted on {{ $entry->created_at }}  by
         <a href="mailto:{{ $entry->email }}">{{ $entry->username}}</a>
      </p><hr />
    @endforeach

    <form action="submit/" method="POST">
        <table border="0">
            <tr>
                <td>Name</td>
                <td><input type="text" name="frmName" value="" size="30" maxlength="50"></td>
            </tr>
            <tr>
                <td>Email</td>
                <td><input type="text" name="frmEmail" value="" size="30" maxlength="100"></td>
            </tr>
            <tr>
                <td>Comment</td>
                <td><textarea name="frmComment" rows="5" cols="30"></textarea></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" name="submit" value="submit">
                    <input type="reset" name="reset" value="reset"></td>
            </tr>
        </table>
    </form>
</BODY>


</HTML>

路线


Route::get('/', 'EntriesController@getIndex');
Route::get('submit', 'EntriesController@postIndex');

在主页上我可以轻松地从数据库中提取记录但是当我尝试发布新记录时会显示错误

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

1 个答案:

答案 0 :(得分:1)

您正在使用刀片服务器,您可以开始使用刀片语法,这将使创建表单变得更加容易。

{{ Form::open(array('url' => 'submit')) }}
// The 'url' => 'submit' wil refer to your route and fetch your postFunction you made.
// You can here setup your form components...
{{ Form::close() }}

创建组件的参考:

http://laravel.com/docs/html

注意: 检查控制器中的输入时,不要忘记使用Laravel验证器。

http://laravel.com/docs/validation