laravel 5可以获取路由,但不能POST

时间:2015-11-24 23:40:11

标签: php routing laravel-5

在这里阅读Laravel 5的教程: http://laravel.com/docs/5.1/quickstart

现在,当尝试将表单数据发布到/ task时,我收到错误...

Not Found
The requested URL /task was not found on this server.

更糟糕的是,如果我设置一条到GET /任务的路线并回应一些简单的东西 - 这是有效的。请问有什么我缺少的POST吗?

这是我的完整路线档案:

<?php

use App\Task;
use Illuminate\Http\Request;

/**
 * Display All Tasks
 */
Route::get('/', function () {
    $tasks = Task::orderBy('created_at', 'asc')->get();

    return view('tasks', [
        'tasks' => $tasks
    ]);
});

/**
 * Add A New Task
 */
Route::post('/task', function (Request $request) {
    $validator = Validator::make($request->all(), [
        'name' => 'required|max:255',
    ]);

    if ($validator->fails()) {
        //return redirect('/')->withInput()->withErrors($validator);
    }

    $task = new Task;
    $task->name = $request->name;
    $task->save();

    //return redirect('/');
});

/**
 * Delete An Existing Task
 */
Route::delete('/task/{id}', function ($id) {
    Task::findOrFail($id)->delete();

    return redirect('/');
});


Route::get('/task', function() {
    echo 'ds';
});

谢谢大家。

DS

编辑....这是我用

发布的表单代码
@extends('layouts.scaffold')

@section('main')

    <!-- Create Task Form... -->
    <form action="/task" method="POST" class="form-horizontal">
        {{ csrf_field() }}

        <!-- Task Name -->
        <div class="form-group">
            <label for="task" class="col-sm-3 control-label">Task</label>

            <div class="col-sm-6">
                <input type="text" name="name" id="task-name" class="form-control">
            </div>
        </div>

        <!-- Add Task Button -->
        <div class="form-group">
            <div class="col-sm-offset-3 col-sm-6">
                <button type="submit" class="btn btn-default">
                    <i class="fa fa-plus"></i> Add Task
                </button>
            </div>
        </div>
    </form>

    <!-- Current Tasks -->
    @if (count($tasks) > 0)
        <div class="panel panel-default">
            <div class="panel-heading">
                Current Tasks
            </div>

            <div class="panel-body">
                <table class="table table-striped task-table">

                    <!-- Table Headings -->
                    <thead>
                        <th>Task</th>
                        <th>&nbsp;</th>
                    </thead>

                    <!-- Table Body -->
                    <tbody>
                        @foreach ($tasks as $task)
                            <tr>
                                <!-- Task Name -->
                                <td class="table-text">
                                    <div>{{ $task->name }}</div>
                                </td>

                                <!-- Delete Button -->
                                <td>
                                    <form action="/task/{{ $task->id }}" method="POST">
                                        {{ csrf_field() }}
                                        {{ method_field('DELETE') }}

                                        <button>Delete Task</button>
                                    </form>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    @endif
@endsection

1 个答案:

答案 0 :(得分:2)

Laravel无法解析POST路由的原因是/task是路由的名称,而不是完整的地址。它不是寻找http://localhost/project/task,而是寻找/task,而不是有效的地址。解决方案是使用Laravel 5的url()辅助方法来指定表单的action

<form method="POST" action="{{ url("/task") }}">...</form>
<!-- {{ ... }} is .blade syntax for <?php ... ?> -->

此外,请注意GET路由与redirect("/")类似的url("/")功能