PUT表单导致找不到404(Laravel)

时间:2019-04-17 17:22:21

标签: php laravel

我有一个模型,一个User UserController和一个登录表单,其中指定了PUT作为方法。我的路线也已定义并与表单路线匹配。提交表单时,出现错误404 Not Found。

我尝试将路由命名为无效。

用户模型     

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $table = "panel_users";
    protected $primaryKey = "id";
    public $incrementing = true;
    public $timestamps = false;

    protected $fillable = ['username', 'password'];
}

UserController     

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index() {
        $user = new User;
        return view('login', ['user' => $user]);
    }

    public function attemptLogin(Request $request, User $user) {
        error_log($user);
        session(['username' => $user->username]);
        return redirect('/');
    }
}

表格

{{ Form::model($user, ['route' => ['login.attemptlogin', $user], 'method' => 'PUT']) }}
                    <div class="form-group">
                        {{ Form::label('username', 'Username: ') }}
                        {{ Form::text('username', null, ['class' => 'form-control']) }}
                    </div>

                    <div class="form-group">
                        {{ Form::label('password', 'Password: ') }}
                        {{ Form::text('password', null, ['class' => 'form-control']) }}
                    </div>

                    <button class="btn btn-success" type="submit">Login</button>
{{ Form::close() }}

已定义路线

Route::put('/login/attemptlogins/{user}', 'UserController@attemptLogin')->name('login.attemptlogin');

应设置将用户名添加到会话变量,并将模型输出到控制台。都没有发生。

2 个答案:

答案 0 :(得分:1)

尝试一下,

{{ Form::model($user, ['route' => ['login.attemptlogin', $user->id], 'method' => 'PUT']) }}

在控制器中

public function attemptLogin(Request $request, $id) {
    // ...
}

希望这会有所帮助:)

答案 1 :(得分:0)

您必须更改

{{ Form::model($user, ['route' => ['login.attemptlogin', $user->id], 'method' => 'PUT']) }}