Laravel POST调用返回index()函数而不是store()

时间:2016-03-23 09:05:50

标签: php laravel http-post laravel-routing laravel-5.2

我正在用laravel编写我的第一个应用程序,这对于新的laravel开发人员来说是非常好的环境。缩短故事:我通过php artisan命令创建了两个雄辩的模型,并以相同的方式创建了相应的控制器。其中一个是“成员”模型,我在控制器的类中创建了公共函数,但是当我尝试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路线:列表是:

+--------+-----------+-------------------------------+------------------------+--------------------------------------------------------+------------+
| 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()。

2 个答案:

答案 0 :(得分:6)

您的问题已由@Arangor here

解决

引用:

  

问题是在URL中添加了一个尾随'/'。所以,而不是使用此URL:

mydomain.com/api/v1/messages/
  

我试过这个:

mydomain.com/api/v1/messages
  

并且有效

答案 1 :(得分:0)

将CSRF字段添加到您的表单

例如:

<form id="form-login" class="m-t form-horizontal" role="form" method="post" action="/login">
                                    {{csrf_field()}}