dongxiangxie8181 2014-03-12 07:32
浏览 14

提交表格时在laravel中路由

I just started laravel . I was able to fetch the data from mysql table but i was unable to post data. I am confused on defining route for 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('/');

        }

}

VIEW : 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>

ROUTES


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

On homepage i can easily pull the records from db but when i try to post new record it will show the error

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
  • 写回答

1条回答 默认 最新

  • dsijovl015728613 2014-03-12 07:42
    关注

    You are using blade, you can start using the blade syntaxis which will make it probably a lot more easy to create your forms.

    {{ 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() }}
    

    A reference for creating the components:

    http://laravel.com/docs/html

    Note: Don't forget to use the Laravel validators when checking your input in your controller.

    http://laravel.com/docs/validation

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100