【问题标题】:MethodNotAllowedHttpException in RouteCollection.php line 219 Error Lravel 5.2RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException 错误 Lravel 5.2
【发布时间】:2016-03-14 10:45:30
【问题描述】:

使用 Laravel 5.2 开发电子商务应用程序!我刚刚添加了一个用于将类别插入数据库的视图,但是当我点击提交时,它会引发以下错误。

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

【问题讨论】:

  • 您在哪条路线上收到错误或 url?

标签: php laravel-5.2


【解决方案1】:

您应该在提交表单时使用{!! Form::open(array('url' =&gt; 'http://localhost/ecom/admin/categories')) !!}

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

【讨论】:

  • 不使用资源,而是在 routes.php 中使用特定的 http 动词,例如 Route::post('admin/categories/create', 'CategoriesController@store');
  • 太难了,laravel 文档推荐资源控制器
  • 并不难,因为我更喜欢这个,因为使用特定的 http 动词很容易维护您的路线
  • 它不工作,你能建议我在这一点上卡住的其他东西。
  • 改成 Route::post('admin/categories/create', 'CategoriesController@store') 和 {!! Form::open(array('url' => 'localhost/ecom/admin/categories/create' , 'method' => 'post')) !!}
【解决方案2】:

也许对你有帮助:

例子:

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

【讨论】:

    猜你喜欢
    • 2016-06-15
    • 2016-06-20
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 2016-07-30
    • 2016-08-30
    • 1970-01-01
    相关资源
    最近更新 更多