duanlun4411 2019-05-15 12:39
浏览 151

如何修复'此路由不支持POST方法。 支持的方法:GET,HEAD。'?

I'm trying to make a website where you can upload games and see them on the home page, but because I need a two-step form I can't directly send them to the database.

my controller:

public function createstep1(Request $request)
{
    $naam = $request->session()->get('naam');
    return view('games.game',compact('naam', $naam))->with('games',          game::all());
}

public function postcreatestep1(Request $request)
{
    $validatedData = $request->validate([
        'naam' => 'required',
        'geslacht' => 'required',
    ]);

    if(empty($request->session()->get('naam'))){
    return redirect('/game');
    }else{
        $naam = $request->session()->get('naam');
        $naam->fill($validatedData);
        $request->session()->put('naam', $naam);
    }
    return redirect('/newgame');
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create(Request $request)
{
    $naam = $request->session()->get('naam');
    return view('games.newgame',compact('naam',$naam));
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $game= new Game();
    $game->game= $request['game'];
    $game->naam= $request['naam'];
    $game->geslacht= $request['geslacht'];
    $game->save();

    return redirect('/game');
}





<div id="newgame" style="height: 500px; width: 250px; float: left;">
<form method="post" name="naam">
    @csrf
<input id="naam" name="naam" placeholder="naam">
    <select name="geslacht" type="text">
        <option name="man">man</option>
        <option name="vrouw">vrouw</option>
    </select>
<a type="submit"><button>volgende</button></a>
</form>
</div>
<div id="games" style="float: right">
@foreach($games as $game)
    {{$game->game}} <br><br>
@endforeach
</div>




<h1>welkom {{$naam}}</h1>
<form method="post">
    @csrf
    <h3>game invoeren</h3>
    <input type="text" id="gamenaam" name="gamenaam" placeholder="game">
<a id="submit" type="post" name="game" href="/newgame/store">     
<button>verstuur</button></a>
</form>



Route::get('/', function () {
    return view('welcome');
});

Route::get('/newgame', function () {
    return view('games.newgame');
});

//Route::post('/newgames', ['as' => '/newgames', 'uses' => 'GameController@create']);

Route::get('/game', 'GameController@createstep1');
Route::post('/game', 'GameController@postcreatestep1');

Route::get('/newgame', 'GameController@create');
Route::post('/newgame/store', 'GameController@store');

I expect it will drop the game + name and gender in the database but the actual code gives an error message. This work is for school but they can't help me if you have suggestions or can help me fix the problem that would be great.

  • 写回答

1条回答 默认 最新

  • doukuangxun5382 2019-05-15 12:51
    关注

    When registering a new route you need to specify which HTTP method the route is for. Usually Route::get() is alright but since you're sending a POST request with your form to the controller method postcreatestep1, this one needs to be registered as a POST route with Route::post().

    You may also declare a route available for multiple HTTP methods with the Route::match() method.

    To get an overview of all available router methods the official documentation of Laravel is a good starting point.

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧