I cant delete the first row that was added to the cart laravel 5.8 bumbummen99 library - e-commerce

I cant delete the first row from the item added to cart. I used Laravel 5.8 with bumbummen99/shoppingcart. Other rows can be deleted successfully but
I can't delete the first row. Please I need help
<form action="{{ url('cart', [$item->rowId]) }}" method="POST">
{!! csrf_field() !!}
<input type="hidden" name="_method" value="DELETE">
<td><button type="submit" class="btn btn-primary btn-sm">X</button></td>
</form>
Above is my view
Below is my route
Route::delete('/cart/{product}', 'CartController#destroy')->name('cart.destroy');
Below is my controller with the destroy method
public function destroy($id)
{
Cart::remove($id);
return back()->with('success', 'Item has been removed');
}
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST.

The problem may be solved by changing your method attribute on your <form> to from POST to DELETE:
<form action="{{ url('cart', [$item->rowId]) }}" method="DELETE">
{!! csrf_field() !!}
<input type="hidden" name="_method" value="DELETE">
<td><button type="submit" class="btn btn-primary btn-sm">X</button></td>
</form>

Related

The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST in laravel 8

I get the following error with the code below.
The DELETE method is not supported for this route. Supported methods:
GET, HEAD, POST
Controller
public function destroy(Post $post)
{
$post->delete();
return back();
}
Route
Route::delete('/posts/{post}', [PostController::class, 'destroy']);
View
<form action="{{ route('posts', $post) }}" method="POST" >
#csrf
#method('delete')
<button type="submit" class="text-blue-500">Delete</button>
</form>
Route:
Route::delete('/admin/user_list/{id}', [UserController::class, 'destroy'])->name('admin.user_list');
Controller:
public function destroy(Request $request, $id)
{
User::where('id', $id)->delete();
return redirect()->back()->withSuccess('Your record deleted successfuly');
}
View:
form action="{{ route('admin.user_list', $rows->id) }}" method="post"
#method('DELETE')
#csrf
<button onclick="return confirm('Are you sure you want to delete this?');" type="submit" value="delete" class="btn btn-danger btn-xs">
<span>DELETE</span>
</button>

sending get parameter from a input text in Laravel 8

I need to collect a get variable in Laravel 8 passed as follows:
127.0.0.1/search/keywordToSearch
I have this route created in my web.php file:
Route::get('/search/{keyword}', [SearchController::class, "search"])->name('search');
My form is:
<form class="form-horizontal" action="{{ route('search') }}" >
<fieldset>
<div class="form-group">
<label for="keyword" class="col-lg-2 control-label">Keyword</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="keyword" name="pelicula" placeholder="keyword">
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button class="btn btn-default">Cancel</button>
<button type="submit" class="btn btn-primary">Search</button>
</div>
</div>
</fieldset>
</form>
But when I push the send button the url obtained is this:
http://127.0.0.1:8000/search?keyword=safafs
How I must do about passing the input value to my controller?
I think in this particular scenario you don't need to pass a parameter trough the route. Make it simple, like
Route::get('/search', [SearchController::class, "search"])->name('search');
In the controller declare the action like
public function search(Request $request)
{
dd($request->pelicula); // here you have your keyword data
}

Why did i get Status Code: 405; Method Not Allowed after trying to execute the delete action?

ASP.NET Core 5.0, EF Core 5.0
My action :
[HttpPost("id")]
public IActionResult DeleteEquipment(int id)
{
repository.DeleteEquipment(id);
return View("WorkoutDB");
}
My ef method:
public void DeleteEquipment(int Id)
{
_context.Remove(new Equipment() { EquipmentId = Id });
_context.SaveChanges();
}
My button from partial view:
<input type="button" value="Delete" class="btn btn-danger" onclick="location.href='#Url.Action("DeleteEquipment", "Admin", item.EquipmentId)'" /
I don't know is it normal code or not, i am newbie, but i spent 4 hours and couldn't get the result.
Thanks for you attention!
UPD:
I changed my button
<td>
<form asp-action="DeleteEquipment" method="post">
<input type="hidden" name="EquipmentId" value="#item.EquipmentId" />
<button type="submit" class="btn btn-danger btn-sm">
Delete
</button>
</form>
</td>
But its still not working. I cant bind correct value to EquipmentId. Always 0 value. Why?
location.href='#Url.Action("DeleteEquipment", "Admin", item.EquipmentId)' cause browser to redirect user to new url. Redirect cause GET Request but your DeleteEquipment method has HttpPost attribute.
Replace
[HttpPost("id")]
with
[HttpGet("{id}")]
As #mason mentioned if you need to call your api with POST Request you need to use form.
<form asp-action="DeleteEquipment" asp-controller="Admin" asp-route-id="#item.EquipmentId">
<input type="submit" class="btn btn-danger" value="Delete" />
</form>

Generating a valid __RequestVerificationToken from C#

One of the most popular books on ASP.NET Core is "Pro ASP.NET Core 3" by Adam Freeman.
In chapters 7-11, he builds an example application, SportsStore.
As you can see, each product in the listing gets its own 'Add To Cart' button:
If we do 'view source' on this page, we'll see the following HTML for that item in the product list:
<div class="card card-outline-primary m-1 p-1">
<div class="bg-faded p-1">
<h4>
Kayak
<span class="badge badge-pill badge-primary" style="float:right">
<small>$275.00</small>
</span>
</h4>
</div>
<form id="1" method="post" action="/Cart">
<input type="hidden" data-val="true" data-val-required="The ID field is required." id="ID" name="ID" value="1" />
<input type="hidden" name="returnUrl" value="/" />
<span class="card-text p-1">
A boat for one person
<button type="submit" class="btn btn-success btn-sm pull-right" style="float:right">
Add To Cart
</button>
</span>
<input name="__RequestVerificationToken" type="hidden" value="CfDJ8KKqNOS0gwdMvC0-bdjTwWlvCcBJldeidwIX5b2f24gYblS9X1sqCwJWIEsKKOSf8kut0SQsQRLF3R1XBSYZkPGnta9YzRK4tcQl8dq_0uWmjeUhm8yMe90fWDt_x0smmAD1lmb9-BxQF8y_7-IQSz4" /></form>
</div>
Note the input tag towards the bottom:
<input name="__RequestVerificationToken" type="hidden" value="CfDJ8KKqNOS0gwdMvC0-bdjTwWlvCcBJldeidwIX5b2f24gYblS9X1sqCwJWIEsKKOSf8kut0SQsQRLF3R1XBSYZkPGnta9YzRK4tcQl8dq_0uWmjeUhm8yMe90fWDt_x0smmAD1lmb9-BxQF8y_7-IQSz4" />
If we look at the Views\Shared\ProductSummary.cshtml file in the SportsStore project, we'll see the code that is involved with generating these listing items:
#model Product
<div class="card card-outline-primary m-1 p-1">
<div class="bg-faded p-1">
<h4>
#Model.Name
<span class="badge badge-pill badge-primary" style="float:right">
<small>#Model.Price.ToString("c")</small>
</span>
</h4>
</div>
<form id="#Model.ID" asp-page="/Cart" method="post">
<input type="hidden" asp-for="ID" />
<input type="hidden" name="returnUrl" value="#ViewContext.HttpContext.Request.PathAndQuery()" />
<span class="card-text p-1">
#Model.Description
<button type="submit" class="btn btn-success btn-sm pull-right" style="float:right">
Add To Cart
</button>
</span>
</form>
</div>
As you can see, the form element in this case doesn't have an explicit inclusion of the input tag with the __RequestVerificationToken value. This form thus appears to be a tag helper which takes care of generting the input tag with the __RequestVerificationToken token.
As an experiment, let's suppose I have added the following method to Controllers\HomeController:
[HttpGet]
public ContentResult ButtonExample()
{
var token = "...";
return new ContentResult()
{
ContentType = "text/html",
StatusCode = (int)HttpStatusCode.OK,
Content =
String.Format(
#"<!DOCTYPE html>
<html>
<body>
<form id=""1"" method=""post"" action=""/Cart"">
<input type=""hidden"" data-val=""true"" id=""ID"" name=""ID"" value=""1"" />
<button type=""submit"">Add to Cart</button>
</form>
<input name=""__RequestVerificationToken"" type=""hidden"" value=""{0}"" />
</body>
</html>",
token)
};
}
As you can see, this generates a very simple page with a single button which is intended to add the product with ID value 1 (i.e. the Kayak) to the cart.
I of course need to pass an appropriate value for the __RequestVerificationToken.
My question is, is there a way to get this value from C# so that I can include it in the method above?
The idea as shown above would be to set the token value here:
var token = "...";
This is then interpolated into the string that generates the HTML using String.Format.
UPDATE
This page mentions the following:
To generate the anti-XSRF tokens, call the #Html.AntiForgeryToken method from an MVC view or #AntiForgery.GetHtml() from a Razor page.
So I guess the question is, how do we do the equivalent from C# directly instead of from an MVC view or Razor page?
You can add the below code to your form which will generate the __RequestVerificationToken. It is used to prevent CSRF attacks Prevent XSRF/CSRF attacks.
<form action="/" method="post">
#Html.AntiForgeryToken()
</form>

[Symfony]--sfGuardPlugin--SignIn

I use symfony 1.4
In my index page, I have created some fields as login form.
When using sfGuardPlugin, it generate automaticly its form.
So, what I'm searching for is how to replace the default form created by the new which I have created.
thanks
By default, sfGuardAuth module comes with 2 very simple templates:
signinSuccess.php
secureSuccess.php
If you want to customize one of these templates:
Create a sfGuardAuth module in your application (don't use the
init-module task, just create a sfGuardAuth directory)
Create a template with the name of the template you want to customize in
the sfGuardAuth/templates directory
symfony now renders your template instead of the default one
More info: https://github.com/Garfield-fr/sfDoctrineGuardPlugin
Edit:
You must set up the settings.yml
enabled_modules: [default, sfGuardAuth, sfGuardUser]
.actions:
login_module: sfGuardAuth
login_action: signin
This is my signinSuccess.php
<h2>Bejelentkezés</h2>
<form id="loginform" action="<?php echo url_for('#sf_guard_signin') ?>" method="post">
<input type="hidden" name="signin[_csrf_token]" value="2a831d070cdd61d81bb1572be3f52d21" id="signin__csrf_token" /> <p>
<label class="required" for="username">Felhasználónév vagy Email:</label><br/>
<input type="text" name="signin[username]" id="signin_username" class="text" /> </p>
<p>
<label class="required" for="password">Jelszó:</label><br/>
<input type="password" name="signin[password]" id="signin_password" class="text" /> </p>
<p>
<input type="submit" class="btn btn-green big" value="Signin" />
</p>
<div class="clear"> </div>
<?php echo $form->renderHiddenFields(); ?>
</form>