Custom function in Controller with resource wont w

2019-08-18 02:29发布

问题:

I have created my own custom function in my RoomsController

public function join($id){ 
    return $id; 
}

Then I want to pass variable to it and it says MethodNotAllowedHttpException And my Form looks like this

{{Form::open(['action'=>  ['RoomsController@join', $room->id], 'method' => 'POST' ])}} 
{{Form::submit('Join', ['class' => 'btn btn-danger'])}}
{{Form::close()}}

Also have these routes


Route::get('/','PagesController@index');
Route::get('/about', 'PagesController@about');
Route::get('/services', 'PagesController@services');
Route::get('/register', 'PagesController@register');
Route::get('/logout', 'PagesController@logout'); 
Route::get('/rooms/join', 'RoomsController@join'); 
Route::resource('posts','PostsController');
Route::resource('rooms','RoomsController');
Auth::routes();

Route::get('/dashboard', 'DashboardController@index');

I have tried in many different ways i dont know why it is not working. All update edit destroy resource functions are working. Thank's for helping :)

回答1:

You're submitting a POST request but the route is expecting a GET request. If you change your route to Route::post('/rooms/join', 'RoomsController@join'); it should work



回答2:

change the method to post and put the route below the resource route

Route::resource('rooms','RoomsController');
Route::post('/rooms/join', 'RoomsController@join');