Custom function in Controller with resource wont work

烈酒焚心 提交于 2019-12-24 20:01:03

问题


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'); 


来源:https://stackoverflow.com/questions/49710175/custom-function-in-controller-with-resource-wont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!