0

路由.php

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

主控制器.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();
    }

}

?>

类别模型.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

2

您需要将模型的add方法的响应返回给Controller的响应。代替:

ControllerModel::add():

尝试

return ControllerModel:add();
于 2013-07-22T06:20:35.263 回答