MethodNotAllowedHttpException-放置和删除

时间:2018-10-13 08:40:47

标签: php laravel

我想删除一个项目,但收到以下错误消息:

 (1/1) MethodNotAllowedHttpException

in RouteCollection.php line 255
at RouteCollection->methodNotAllowed(array('PUT', 'DELETE'))

我的路线:

Route::group(['prefix' => '/Seller', 'middleware' => ['seller_access']], function ()  {

    Route::get('/','Seller\SellerController@index')->name('seller');

    Route::group(['prefix' => '/Products'], function ()  {
        Route::get('/', 'MarketingBundle\Seller\Product\ProductController@index')->name('marketing.seller.product.index');
        Route::delete('/{id}', 'MarketingBundle\Seller\Product\ProductController@delete')->name('marketing.seller.product.delete');
        Route::put('/{id}', 'MarketingBundle\Seller\Product\ProductController@update')->name('marketing.seller.product.update');
    });

我的网址:

Seller/Products/228

我的控制器:

class ProductController extends Controller
{

    public $resources = "marketing.seller.product";


    public function index(Request $request)
    {

        $products = \Auth::user()->sellerProduct()->paginate(10);
        return view($this->resources . '.index', [
            'products' => $products
        ]);
    }

    /**
     * @param $product_id
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete($product_id)
    {
        dd("masoud");
        \Auth::user()->sellerProduct()->detach(['product_id' => $product_id]);
        return response()->json(['status' => true]);
    }

3 个答案:

答案 0 :(得分:1)

像这样重新排序您的路线。

‘a’ is a rigid type variable bound by the type signature for:

答案 1 :(得分:0)

这是来自laravel文档:

  

任何指向POST,PUT或DELETE路由的HTML表单   网络路由文件中定义的内容应包含CSRF令牌   领域。否则,该请求将被拒绝。您可以阅读更多   CSRF文档中有关CSRF保护的信息:

MethodNotAllowedException最有可能与csrf令牌有关,您在发布到delete函数时是否在视图中添加了它?这意味着您的视图需要具有以下内容:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="2">

        <android.support.constraint.ConstraintLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#910FF5">

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@mipmap/ic_launcher" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:gravity="center"
                android:text="Time Table"
                android:textColor="@android:color/white"
                android:textSize="24sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/imageView3" />

            <View
                android:id="@+id/view2"
                android:layout_width="wrap_content"
                android:layout_height="5dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:background="#ffffff"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView4" />

            <TextView
                android:id="@+id/textView6"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:gravity="center"
                android:text="Check your time table here"
                android:textColor="@android:color/white"
                android:textSize="24sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/view2" />
        </android.support.constraint.ConstraintLayout>

        <android.support.constraint.ConstraintLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#910FF5">

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@mipmap/ic_launcher" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:gravity="center"
                android:text="Time Table"
                android:textColor="@android:color/white"
                android:textSize="24sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/imageView4" />

            <View
                android:id="@+id/view3"
                android:layout_width="wrap_content"
                android:layout_height="5dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:background="#ffffff"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView5" />

            <TextView
                android:id="@+id/textView7"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:gravity="center"
                android:text="Check your time table here"
                android:textColor="@android:color/white"
                android:textSize="24sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/view3" />
        </android.support.constraint.ConstraintLayout>
    </TableRow>

</android.support.constraint.ConstraintLayout>

答案 2 :(得分:0)

为了让laravel知道您正在发送补丁或删除请求,您需要在表单中添加一个方法字段。

<form method='POST' action='#'>
@csrf
{{ method_field('PATCH') }} 
</form>

文档 https://laravel.com/docs/5.7/helpers#method-method-field https://laravel.com/docs/5.0/routing#method-spoofing