Laravel POST 调用返回 index() 函数而不是 store()
Posted
技术标签:
【中文标题】Laravel POST 调用返回 index() 函数而不是 store()【英文标题】:Laravel POST call returns index() function instead of store() 【发布时间】:2016-07-10 11:38:26 【问题描述】:我正在用 laravel 编写我的第一个应用程序,对于新的 laravel 开发人员来说,这是一个非常好的环境。简而言之:我通过 php artisan 命令创建了两个 eloquent 模型,并以相同的方式创建了相应的控制器。其中之一是“成员”模型,我在控制器的类中创建了公共函数,但是当我尝试 POST 到应该使用 store() 函数的路由时,该路由实际上使用 index() 函数。
控制器:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Member;
use Response;
class MemberController extends Controller
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
$members = Member::all();
return Response::json([
'data' => $this->transformCollection($members)
], 200);
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
//
/**
* @param Request $request
* @return mixed
*/
public function store(Request $request)
if(! $request->email)
return Response::json([
'error' => [
'message' => 'Please Provide email'
]
], 422);
$Member = Member::create($request->all());
return Response::json([
'message' => 'Member Created Succesfully',
'data' => $this->transform($Member)
]);
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
$Member = Member::find($id);
if(!$Member)
return Response::json([
'error' => [
'message' => 'Member does not exist'
]
], 404);
return Response::json([
'data' => $this->transform($Member)
], 200);
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
//
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update(Request $request, $id)
if(! $request->user_id)
return Response::json([
'error' => [
'message' => 'Please Provide user_id'
]
], 422);
$Member = Member::find($id);
if($request->email)
$Member->email = $request->email;
if($request->first_name)
$Member->first_name = $request->first_name;
if($request->last_name)
$Member->last_name = $request->last_name;
if($request->phone)
$Member->phone = $request->phone;
if($request->address_one)
$Member->address_one = $request->address_one;
if($request->address_two)
$Member->address_two = $request->address_two;
if($request->zip_code)
$Member->zip_code = $request->zip_code;
if($request->city)
$Member->city = $request->city;
if($request->county)
$Member->county = $request->county;
if($request->state_region)
$Member->state_region = $request->state_region;
if($request->country)
$Member->country = $request->country;
if($request->job_title)
$Member->job_title = $request->job_title;
if($request->company)
$Member->company = $request->company;
if($request->clearance_level)
$Member->clearance_level = $request->clearance_level;
$Member->save();
return Response::json([
'message' => 'Member Updated Succesfully',
'data' => $this->transform($Member)
]);
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
Member::destroy($id);
/**
* @Description Data transform
* @param $contacts
* @return array
*/
private function transformCollection($contacts)
return array_map([$this, 'transform'], $contacts->toArray());
/**
* @Description Data transform
* @param $contact
* @return array
*/
private function transform($contact)
return [
'id' => $contact['id'],
'fname' => $contact['first_name'],
'lname' => $contact['last_name'],
'email' => $contact['email'],
'address' => $contact['address_one']
];
我的模型是:
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
class Member extends Model
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'members';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'email',
'first_name',
'last_name',
'phone',
'address_one',
'address_two',
'zip_code',
'city',
'county',
'state_region',
'country',
'job_title',
'company',
'clearance_level',
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
];
我的 php artisan route:list 是:
+--------+-----------+-------------------------------+------------------------+--------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+-------------------------------+------------------------+--------------------------------------------------------+------------+
| | GET|HEAD | / | | Closure | |
| | POST | api/v1/members | api.v1.members.store | App\Http\Controllers\MemberController@store | |
| | GET|HEAD | api/v1/members | api.v1.members.index | App\Http\Controllers\MemberController@index | |
| | GET|HEAD | api/v1/members/create | api.v1.members.create | App\Http\Controllers\MemberController@create | |
| | DELETE | api/v1/members/members | api.v1.members.destroy | App\Http\Controllers\MemberController@destroy | |
| | GET|HEAD | api/v1/members/members | api.v1.members.show | App\Http\Controllers\MemberController@show | |
| | PUT|PATCH | api/v1/members/members | api.v1.members.update | App\Http\Controllers\MemberController@update | |
| | GET|HEAD | api/v1/members/members/edit | api.v1.members.edit | App\Http\Controllers\MemberController@edit | |
| | GET|HEAD | auth/login | | App\Http\Controllers\Auth\AuthController@getLogin | web,guest |
| | POST | auth/login | | App\Http\Controllers\Auth\AuthController@postLogin | web,guest |
| | GET|HEAD | auth/logout | | App\Http\Controllers\Auth\AuthController@getLogout | web,guest |
| | POST | auth/register | | App\Http\Controllers\Auth\AuthController@postRegister | web,guest |
| | GET|HEAD | auth/register | | App\Http\Controllers\Auth\AuthController@getRegister | web,guest |
| | POST | password/email | | App\Http\Controllers\Auth\PasswordController@postEmail | web,guest |
| | GET|HEAD | password/email | | App\Http\Controllers\Auth\PasswordController@getEmail | web,guest |
| | POST | password/reset | | App\Http\Controllers\Auth\PasswordController@postReset | web,guest |
| | GET|HEAD | password/reset/token | | App\Http\Controllers\Auth\PasswordController@getReset | web,guest |
+--------+-----------+-------------------------------+------------------------+--------------------------------------------------------+------------+
路线定义:
Route::group(['prefix' => 'api/v1'], function()
Route::resource('members', 'MemberController');
);
我尝试了 mysite.us/api/v1/members
和 mysite.us/api/v1/members/
他们都在POST
上返回index()
。
【问题讨论】:
也向我们展示您的刀片。确保您的表单是post
方法
我正在使用 POSTMAN chrome 插件通过此 URL 发布它,我在我的其他 Laravel 安装上具有相同的模型,称为 Contacts,它运行顺利。是的
在邮递员中检查您是否发送表单数据而不是(例如)json
你能显示你的路线/资源定义吗?
【参考方案1】:
@Arangor here已解决您的问题
引用:
问题是在 URL 中添加尾随“/”。所以,不要使用这个 URL:
mydomain.com/api/v1/messages/
我试过这个:
mydomain.com/api/v1/messages
成功了
【讨论】:
【参考方案2】:在表单中添加 CSRF 字段
例如:
<form id="form-login" class="m-t form-horizontal" role="form" method="post" action="/login">
csrf_field()
【讨论】:
以上是关于Laravel POST 调用返回 index() 函数而不是 store()的主要内容,如果未能解决你的问题,请参考以下文章
API POST 请求在邮递员和 Laravel 上返回 404 NOT FOUND PAGE
尽管 Laravel 中的令牌正确,Ajax 调用仍返回 419 错误
从 PHP 控制器使用 request.post() 调用 python 脚本永远不会返回。为啥?