Laravel - 表格动作网址未找到& PUT方法不起作用

时间:2016-03-11 07:18:59

标签: php laravel laravel-5.2

我在Laravel 5.2中工作,并使用Eloquent ORM处理数据库。我尝试更新数据库中的用户数据,但是当我点击我的UPDATE按钮时,它会在下面给出错误。我正在使用PUT方法根据Laravel REST规则更新数据库中的数据。

错误网址:

  

http://localhost/users?_token=wazgR1tQaznQwRdejXdx4g3jLgbtlfPLIeIiXdRy&name=warka&email=&password=

错误:

  

找不到对象!

     

在此服务器上找不到请求的URL。关于的链接   引用页面似乎是错误的或过时的。请通知作者   关于错误的那个页面。

     

如果您认为这是服务器错误,请与网站管理员联系。

     

错误404

     

localhost Apache / 2.4.12(Win32)OpenSSL / 1.0.1l PHP / 5.6.8

用户控制器:

   <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use Illuminate\Support\Facades\Validator;

use Illuminate\Support\Facades\Input;

use Illuminate\Support\Facades\Redirect;

use Illuminate\Support\Facades\View;

use App\User;

class UsersController extends Controller
{
    public function create(){
        return view('create');
    }

    public function store(){
        $rules = array(
            'name' => 'required|unique:users',
            'email' => 'required|unique:users',
            'password' => 'required|min:5'

        );
        $validator = Validator::make(Input::all(),$rules);

        if($validator->fails()){
            return Redirect::to('http://localhost/laravelking/users/create')->withInput()->withErrors($validator);
         }else{
            User::create(array(
                'name' => Input::get('name'),
                'email' => Input::get('email'),
                'password' => Input::get('password')
            ));
            return Redirect::to('http://localhost/laravelking/users');
        }

    }

    public function index(){
        return view::make('users')->withUsers(User::all());
    }
    public function show($id){
        $user = User::find($id);

        if($user == null){
            return Redirect::to('http://localhost/laravelking/users');
        }else{
            return View::make('profile')->withUser($user);
        }
        return 'list '.$id;
    }
    public function update($id){
        $rules = array(
            'name' => 'required|unique:users',
            'email' => 'required|unique:users',
            'password' => 'required|min:5'

        );
        $validator = Validator::make(Input::all(),$rules);

        if($validator->fails()){
            return Redirect::to('http://localhost/laravelking/users/'.$id.'/edit')->withInput()->withErrors($validator);
         }else{
            $user = User::find($id);
            if(Input::has('name')) $user->name = Input::get('name');
            if(Input::has('email')) $user->email = Input::get('email');
            if(Input::has('password')) $user->password = Input::get('password');
            $user->save();
            return Redirect::to('http://localhost/laravelking/users/'.$id);
        }
    }
    public function edit($id){
        $user = User::find($id);
        if($user == null){
            return Redirect::to('http://localhost/laravelking/users');
        }else{
            return View::make('edit')->with('id',$id);
        }
    }

    public function delete($id){
        return 'list'.$id;
    }
}

查看更新表单

<form role="form" method="PUT" action="users/".$id>
  <input type="hidden" name="_token" value="{{ csrf_token() }}">
  <div class="form-group">
    <label for="username">New Name:</label>
    <input type="username" class="form-control" name="name" id="name">
  </div>    
  <div class="form-group">
    <label for="email">New Email address:</label>
    <input type="email" name="email" class="form-control" id="email">
  </div>
  <div class="form-group">
    <label for="pwd">New Password:</label>
    <input type="password" name="password" class="form-control" id="pwd">
  </div>
  <div class="checkbox">
    <label><input type="checkbox"> Remember me</label>
  </div>
  <button type="submit" class="btn btn-default">Update</button>
</form>

个人资料视图从我输入的位置更新视图是:

<div class="container">
    <div class="col-md-8 col-lg-8 col-sm-12">
        <div class="jumbotron">
            <h1> Hello {!! $user->name !!}</h1>
            <h3> Your Email is {!! $user->email !!}</h3>
            <h3 style="color:red"> Your Password is {!! $user->password !!}</h3>
            <h1> {!! Html::link('users/'.$user->id.'/edit','Edit ') !!}</h1>
        </div>
    </div>
</div>

路线:

Route::group(['middleware' => ['web']], function () {
    //
    Route::resource('users','UsersController');
});

已更新:

刚刚将表单方法更改为: <form role="form" action='http://localhost/laravelking/users/<?php echo $id; ?>' method="PUT">

I.e $ id到PHP echo并且它工作但数据库中的数据没有更新!

新网址现在如下所示:

http://localhost/laravelking/users/1?_token=wazgR1tQaznQwRdejXdx4g3jLgbtlfPLIeIiXdRy&name=Lololololol&email=Lololololol%40gaic.com&password=Lololololol

但问题是数据未在DB中更新

1 个答案:

答案 0 :(得分:1)

您的应用程序位于 laravelking 目录中,但您的表单只是将请求发送到 http://localhost

您最好将表单操作更改为以下内容:

 <form action="{{url('users/' . $id)}}" ... >

因为HTML表单不支持PUT,PATCH和DELETE方法,所以需要添加一个额外的字段才能使其工作:

<input name="_method" type="hidden" value="PUT">

或者您可以使用一些Laravel助手:

// Plain PHP
echo method_field('PUT'); 

//Blade template engine
{{ method_field('PUT') }}

您可以在此处详细了解:Laravel HTTP Routing - Method spoofing