简体   繁体   中英

MethodNotAllowedHttpException in RouteCollection.php line 219 Error Lravel 5.2

Working on a eCommerace App , using Laravel 5.2 ! I just added a view for inserting categories into Database but when I hit submit it throws the below error.

PS : Tried all the answers on stackoverflow but none worked!

ERROR:

    MethodNotAllowedHttpException in RouteCollection.php line 219:
    in RouteCollection.php line 219
    at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE')) in RouteCollection.php line 206
    at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE')) in RouteCollection.php line 158
    at RouteCollection->match(object(Request)) in Router.php line 823
    at Router->findRoute(object(Request)) in Router.php line 691
    at Router->dispatchToRoute(object(Request)) in Router.php line 675
    at Router->dispatch(object(Request)) in Kernel.php line 246
    at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
    at CheckForMaintenanceMode->handle(object(Request), object(Closure))
    at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
    at Pipeline->Illuminate\Routing\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
    at Pipeline->then(object(Closure)) in Kernel.php line 132
    at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
    at Kernel->handle(object(Request)) in index.php line 54
    at require_once('D:\xampp\htdocs\ecom\public\index.php') in index.php line 21

VIEW Code:

      {!! Form::open(array('url' => 'http://localhost/ecom/admin/categories/create' , 'method' => 'post')) !!}
      <input type="hidden" name="_token" value="{{ csrf_token() }}">

                          <div class="form-group">
                            <label for="username">Category Name:</label>
                            <input type="username" class="form-control" name="name" id="name">
                          </div>    
                          <button type="submit" class="btn btn-default">Submit</button>
   {!! Form::close() !!}

Model Goes here:

<?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

        class Category extends Model
        {
            protected $fillable = array('name');
            public static $rules = array('name' => 'required|min:3');
        }

Controller code :

    <?php

    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use App\Http\Requests;
    use View;
    use Illuminate\Support\Facades\Input;
    class CategoriesController extends Controller
    {

        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function index()
        {
            return View::make('categories.index')->with('Categories', Category::all());
        }

        /**
         * Show the form for creating a new resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function create()
        {
            return View::make('InsertCategory');
        }

        /**
         * Store a newly created resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
        public function store(Request $request)
        {
            $validator = Validator::make(Input::all(), Category::$rules);
            if($validator->passes()){
                $category = new Category;
                $category->name = Input::get('name');
                $category-save();

                return Redirect::to('admin/categories/index')->with('message', 'Category Created');
            }
            return Redirect::to('admin/categories/index')->with('message', 'Category Created')->withErrors($validator)->withInput();
        }

        /**
         * 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)
        {
            //
        }
    }

ROUTES.PHP

<?php

/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

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

Route::resource('admin/categories', 'CategoriesController');

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/

Route::group(['middleware' => ['web']], function () {
    //
});

You should use {!! Form::open(array('url' => 'http://localhost/ecom/admin/categories')) !!} {!! Form::open(array('url' => 'http://localhost/ecom/admin/categories')) !!} when you submit the form.

According to RESTful resource controller the URL for form action will be the main route not /store or /create.

May be it help you :

Example :

 <form id="categoryForm" method="post" action="{{ route('categories.store') }}"/>

Route for store :

Route::resource('categories', 'CategoryController');

Controller :

 public function store(Request $request)
{

    $id = (int)$request->get("id", 0);

    $validator = Categories::validator($request->all(), $id);
    if ($validator->fails()) {
        return redirect()->back()
            ->withErrors($validator->getMessageBag())
            ->withInput($request->all());
    }

    try {
        Categories::updateOrCreate([
            'id' => $id,
        ], [
            'name'   => $request->get('name'),
            'status' => $request->get('status')
        ]);

        return redirect()->route('categories.index')->with('flash_success', 'Category data saved!');
    } catch (\Exception $e) {

        return redirect()->route('categories.create')->with('flash_danger', 'Category data not saved!');
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM