【问题标题】:Method not allowed exception on form tag表单标签上的方法不允许异常
【发布时间】:2018-08-23 08:13:32
【问题描述】:

我是 Laravel 新手,并使用 laravel 购物车 libraryLaravelcollective 5.4 创建了一个 ecomm 网站。我目前卡在 Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException 上,它出现在单个删除按钮上,但是如果我们创建另一个删除按钮,第二个按钮可以使用相同的代码正常工作,而第一个按钮会产生错误。请帮我解决这个错误。

我的错误是: 我的路线文件是:

    <?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')->name('index');
    Route::get('shop', 'FrontController@shop')->name('shop');
    Route::get('details', 'FrontController@details')->name('productdetails');
    Route::get('/logout', 'Auth\LoginController@logout');
    Auth::routes();

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

    Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function () {
        Route::get('/', 'AdminController@index')->name('admin.index');
        Route::resource('product','ProductsController');
        Route::resource('category','CategoriesController');
    });

我的错误创建页面是here

顶部的表单标签会产生错误,底部的表单标签可以正常工作。

我的控制器是:

<?php

namespace App\Http\Controllers;

use Gloudemans\Shoppingcart\Facades\Cart;
use App\Product;
use Illuminate\Http\Request;

class CartController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $products=Product::orderBy('id','DESC')->take(4)->get();
        $cartItems = Cart::content();
        return view('cart.index',compact('products','cartItems'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * 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)
    {
        $product = Product::find($id);
        Cart::add($id,$product->name,1,$product->price);
        return back();
    }


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

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

我只希望最上面的表单标签可以工作。请指导我。谢谢

【问题讨论】:

  • 你能粘贴你的代码而不是它的图像吗?
  • 您要访问的网址是什么?
  • 发布了我的代码!
  • 我正在尝试访问购物车/{id)
  • 使用哪种方法访问删除删除或正常获取的路由

标签: php html laravel laravel-5.4


【解决方案1】:

因为您正在为购物车使用资源控制器。所以你应该对删除按钮使用delete方法。

<form action="{{route('cart.destroy',$cartItem->rowId)}}">
    {{ method_field('DELETE') }}
    {{ csrf_field() }}
    <button>Delete</button>
</form>

【讨论】:

  • 感谢您的回复。您的方法不起作用,它显示相同的错误。
  • 再试一下删除方法后
【解决方案2】:

你可以试试

Route::resource('/cart','CartController')-&gt;name('cart');

【讨论】:

    猜你喜欢
    • 2014-07-16
    • 2018-02-02
    • 2012-08-10
    • 2018-05-28
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多