Laravel:无法重定向以获取Route

时间:2013-07-20 05:21:36

标签: redirect get laravel routes

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");
    }
}

}

&GT;

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字段被添加到数据库中时,除了空白页面外,我看不到任何内容。

1 个答案:

答案 0 :(得分:2)

您需要将模型的add方法的响应返回给Controller的响应。而不是:

ControllerModel::add():

尝试

return ControllerModel:add();