使用Laravel 5.2开发eCommerace应用程序!我刚刚添加了一个用于将类别插入数据库的视图,但是当我点击提交时,它会抛出以下错误。
PS :在堆栈溢出上尝试了所有答案,但没有一个有效!
错误:
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)
{
//
}
}
路线。.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 () {
//
});
提交
表单时应使用{!! Form::open(array('url' => 'http://localhost/ecom/admin/categories')) !!}
。
根据 RESTful 资源控制器,表单操作的 URL 将是主要路由,而不是/store 或 /create。
也许它可以帮助你:
例:
<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!');
}
}