Baptiste Arnaud Baptiste Arnaud - 4 months ago 14
PHP Question

Store items with POST method route not working

I'm trying to implement a shopping cart system with only session variable in Laravel.

But when i submit my form:

<form method="POST" action="{{ route('panierAdd') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<table>
<tr>
<td>
Nombre de serveurs physiques
</td>
<td>
<select id="nb_serveurs" name="nb_serveurs"><option value="0">Choisir une option</option><option value="1" selected="selected" >1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option></select>
</td>
</tr>
<tr>
<td>
Type de licence
</td>
<td>
<select id="type" name="type"><option value="0">Choisir une option</option><option value="1" selected="selected" >Illimitée</option><option value="2">Standard</option></select>
</td>
</tr>
<tr>
<td>
Durée
</td>
<td>
<select id="duree" name="duree"><option value="0">Choisir une option</option><option value="1" selected="selected">1 an</option><option value="2">2 ans</option><option value="3">3 ans</option></select>
<a class="reset_variations" href="#">Effacer</a>
</td>
</tr>
</table>
<div style="width:100%;height:50px;" id="div-price">
<div class="price"><span id="total">495,00 € HT</span></div>
</div>
<div class="panier-button">
<input type="submit" class="single_add_to_cart_button button alt joli-boutton" id="panier-go" value="Ajouter au panier"/>
</div>
</form>


There are my routes:

Route::post('/panier/add',[
"uses" => "ProductController@addToCard",
"as" => "panierAdd"
]);

Route::delete('panier/delete',[
"uses" => "ProductController@deleteFromCard",
"as" => "panierDelete"
]);

Route::get('panier',[
"uses" => "ProductController@card",
"as" => "panier"
]);


And here is the ProductController:

class ProductController extends Controller
{
public function addToCard (Request $request){
$nb_serveurs = $request->nb_serveurs;
$type = $request->type;
$duree = $request->duree;
$product = new Produit($nb_serveurs, $type, $duree);
if (Session::has('panier'))
{
$panier = Session::get('panier');
$id = Session::get("id")+1;
}else{
$panier = new Panier();
$id = 1;
}
$panier->addItem($product, $id);
$panier->calcPrixTot();
Session::put("panier", $panier);
Session::put("id", $id);
return Redirect::route('panier');
}

public function deleteFromCard (Request $request){
Session::forget('panier');
if(Session::has('panier')){
return view("panier.panier");
}else{
return view('index');
}

}
public function card (Request $request){
return view('panier.panier');
}
}


When i submit the form, here is what it gives me :

MethodNotAllowedHttpException in RouteCollection.php line 218


What did i make wrong ?

Answer

It will be something to do with your routing. There are two things i would try, removing the first slash from your route uri and swapping the as and uses keys around. Try this below

Route::get('panier/add', ['as' => 'panierAdd', "uses" => "ProductController@addToCard"]);

Comments