RouteCollection.php中的MethodNotAllowedHttpException 219错误Lravel 5.2

时间:2016-03-14 10:45:31

标签: php laravel-5.2

使用Laravel 5.2在eCommerace应用程序上工作!我刚刚添加了一个用于在数据库中插入类别的视图,但是当我点击提交时它会抛出以下错误。

PS:尝试了stackoverflow上的所有答案,但都没有用!

错误:

    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

查看代码:

      {!! 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() !!}

模特走到这里:

<?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

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

控制器代码:

    <?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 () {
    //
});

2 个答案:

答案 0 :(得分:0)

提交表单时应使用{!! Form::open(array('url' => 'http://localhost/ecom/admin/categories')) !!}

根据RESTful资源控制器,表单操作的URL将是 / store / create。

的主要路径。

答案 1 :(得分:0)

可能对您有帮助:

示例:

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

商店路线:

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

控制器:

 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!');
    }
}