0

Routes.php

Route::get("/", "MasterController@home");
Route::get("add/a/category", "MasterController@add_a_category");
Route::post("add/a/category", "MasterController@add_a_category_post");

MasterController.php

<?php

class MasterController extends BaseController {

    public function add_a_category()
    {
        // titling
        $data['title'] = "Price Soldier - Add a Category";
        // viewing
        return View::make("pages.add_a_category", $data);
    }

    public function add_a_category_post()
    {
        // titling
        $data['title'] = "Price Soldier - Add a Category";
        // controlling
        CategoryModel::add();
    }

}

?>

CategoryModel.php

<?php

class CategoryModel {

protected $fillable = array("category_name", "updated_at", "created_at");

public static function add()
{
    // Validation
    $rules = array("category_name" => "required|min:3|max:20");
    $validation = Validator::make(Input::except("submit"), $rules);

    if ( $validation->fails() )
    {
        return Redirect::to("add/a/category")->withErrors($validation);
    } 
    else
    {
        $result = DB::table("categories")
        ->insert(
            array(Input::except("submit"))
        );
        return Redirect::to("add/a/category");
    }
}

}

?>

add_a_category.blade.php

@extends("layouts.master")

@section("content")
<h1>Add a Category</h1>
<form action="{{ URL::to("/") }}/add/a/category" method="POST">
    <label for="category_name">Category Name</label>
    <input type="text" name="category_name" value="">
    {{ $errors->first("email", "<span class='error'>:error</span>") }}
    <div style="clear: both;"></div>
    <div class="submit">
        <input type="submit" name="submit" value="Add">
    </div>
</form>
@stop

add/a/category検証が成功または失敗すると、ルートにリダイレクトされます。category_nameしかし、フィールドがデータベースに追加されている間、空白のページ以外は何も表示されません。

4

1 に答える 1