gpt4 book ai didi

php - 如何在 Symfony 中调试 "MethodNotAllowedHttpException"?

转载 作者:行者123 更新时间:2023-11-29 10:29:28 25 4
gpt4 key购买 nike

我使用“添加产品”功能为我的网站创建了一个后端,但是当我添加它时,我没有在 table 上创建产品,并且出现此错误

我有这个错误:

Symfony \ Component \ HttpKernel \ Exception \
MethodNotAllowedHttpException
No message

我的表格:

@extends('admin.layout.admin')

@section('content')

<h3>Aggiungi Prodotto</h3>

<div class='row'>
<div class="col-md-8 col-md-offset-2">

{!! Form::open(['route'=>'product.create','method'=>'post', 'files'=>true]) !!}

<div class="form-group">
{{ Form::label('nome', 'Nome') }}
{{ Form::text('nome', null, array('class'=> 'form-control')) }}
</div>

<div class="form-group">
{{ Form::label('descrizione', 'Descrizione') }}
{{ Form::text('descrizione', null, array('class'=> 'form-control')) }}
</div>

<div class="form-group">
{{ Form::label('prezzo', 'Prezzo') }}
{{ Form::text('prezzo', null, array('class'=> 'form-control')) }}
</div>

<div class="form-group">
{{ Form::label('category_id', 'Categoria') }}
{{ Form::select('category_id',[1=>'Flauti'],null,['class'=> 'form-control','placeholder'=>'Seleziona Categoria']) }}

</div>

<div class="form-group">
{{ Form::label('image', 'Image') }}
{{ Form::file('image',array('class'=> 'form-control')) }}

</div>

{{ Form::submit('create', array('class'=>'btn btn-->default')) }}

{!! Form::close() !!}

</div>

</div>



@endsection

我的路线:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', 'frontcontroller@index');
Route::get('/index.html', 'frontcontroller@index');
Route::get('/checkout.html', 'frontcontroller@checkout');
Route::get('/furniture.html', 'frontcontroller@furniture');
Route::get('/login.html', 'frontcontroller@login');
Route::get('/products.html', 'frontcontroller@products');
Route::get('/register.html', 'frontcontroller@register');
Route::get('/single.html', 'frontcontroller@single');
Auth::routes();

Route::get('/logout', 'Auth\LoginController@logout')->name('home');;

Route::get('/home', 'HomeController@index')->name('home');

Route::group(['prefix' => 'admin' ,'middleware'=>'auth'], function () {
Route::get('/', function () {
return view('admin.index');
})->name('admin.index');

Route::resource('product','ProductsController');
Route::resource('category','CategoriesController');

});

我的 Controller :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Category;

class ProductsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$categories=Category::pluck('nome','id');
return view('admin.product.create',compact('categories'));
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$formInput=$request->except('image');
//image upload
$image=$request->image;
if ($image){
$imageName=$image->getClientOriginName();
$image->move('image',$imageName);
$formInput['image']=$imageName;
}
Product::create($formInput);
return redirect()->route('admin.index');
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}

最佳答案

请更改表单中的以下行

{!! Form::open(['route'=>'product.create','method'=>'post', 'files'=>true]) !!}

{!! Form::open(['route'=>'product.store','method'=>'post', 'files'=>true]) !!}

基本上,创建路由是为了在使用资源时显示表单。但是,当您想提交表单时,您需要提交到 .store,或者如果它是编辑表单,则提交到 .update 路由。

关于php - 如何在 Symfony 中调试 "MethodNotAllowedHttpException"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47684937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com