I have a form that sends back data with a POST to Laravel. The issue is that the POST is not captured by the route that I created. I get a 404.
The form
<form action="/articles" method="post">
<div class='field'>
<label class='label' for='title'>Title</label>
<div class='control'>
<input class='input' type='text' name='title' id='title'>
</div>
</div>
<div class='field'>
<label class='label' for='excerpt'>Excerpt</label>
<div class='control'>
<textarea class='textarea' name='excerpt' id='excerpt'></textarea>
</div>
</div>
<div class='field'>
<label class='label' for='body'>Body</label>
<div class='control'>
<textarea class="textarea" name='body' id='body'></textarea>
</div>
</div>
<div class='field is-grouped'>
<div class='control'>
<button class='button is-link' type='submit'>Submit</button>
</div>
</div>
</form>
My route file:
<?php
use Illuminate\Support\Facades\Route;
Route::get('/articles', 'ArticlesController#index');
Route::post('/articles', 'ArticlesController#store');
Route::get('/articles/create', 'ArticlesController#create');
I have tried all kinds of variations in the action and the method, but I always get a 404.
Any idea what I am doing wrong?
When you use action="/articles", you wind up with what is known as a "relative path", i.e. it tries to append /articles to whatever your current URL is. Looking at your Routes, this means it's trying to POST to
http://localhost:8000/articles/create/articles
The url() helper creates an "absolute path", which has no ambiguity, and results in the correct URL:
http://localhost:8000/articles
Note: Your URL might be a different host, but the logic still applies.
Related
I'm trying to learn Laravel, and I'm following a series of tutorials called laracast. I'm at episode 24, "Forms that submit PUT requests. The short story is that the markup uses a hidden value to set the method to PUT, although the forms method is set to POST. Still, when I do this, I get the error message from the title:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The PUT method is not supported for this route. Supported methods: GET, HEAD.
From the tutorials, I'd expect POST to also be a supported method. However, when I try to fix this, all resources I can find simply tells me what I already know. PUT is not supported, but I can fake it/override it, and then they refer to what I have already done... Are there any other reasons why I might get this error message?
HTML Form:
<form method="POST" action="/competition-categories">
#csrf
#method('PUT')
<div class="form-group row">
<label for="competition-category-name-input" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="competition-category-name-input" name="competition-category-name-input" type="text" class="form-control" required="required" value="{{ $competitionCategory->name }}">
</div>
</div>
<div class="form-group row">
<label for="competition-category-abbreviation-input" class="col-4 col-form-label">Abbreviation</label>
<div class="col-8">
<input id="competition-category-abbreviation-input" name="competition-category-abbreviation-input" type="text" class="form-control" required="required" value="{{ $competitionCategory->abbreviation }}">
</div>
</div>
<div class="form-group row">
<div class="offset-4 col-8">
<button name="submit" type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
web.php snippet:
//Competition Categories
Route::get('/competition-categories', 'CompetitionCategoryController#index');
Route::get('/competition-categories/create', 'CompetitionCategoryController#create');
Route::get('/competition-categories/{competitionCategory}', 'CompetitionCategoryController#show');
Route::get('/competition-categories/{competitionCategory}/edit', 'CompetitionCategoryController#edit');
Route::post('/competition-categories/{competitionCategory}', 'CompetitionCategoryController#store');
Route::put('/competition-categories/{competitionCategory}', 'CompetitionCategoryController#udpate');
Route::delete('/competition-categories/{competitionCategory}', 'CompetitionCategoryController#destroy');
Snippet from the controller:
public function update(Request $request, CompetitionCategory $competitionCategory)
{
$competitionCategory->update($this->validateCompetitionCategory());
return redirect()->route('competition-categories' , [$competitionCategory]);
}
You're forgetting the id in form, this should fix your problem:
action="/competition-categories/{{$competitionCategory->id}}"
The most common thing that this happens is your cache. When you add a new route or change something in your routes, always run after php artisan optimize to refresh you cache.
I recommend using the named-routes for more informations and messages see =>
https://laravel.com/docs/7.x/routing#named-routes
Here how to use it according to its GitHub
I already install the package:
Use Opengraph
$data = OpenGraph::fetch("http://www.addemyplus.com/blogs/my-faith-for-your-love");
that may return an array:
{"type":"website","title":"My Faith For Your Love","description":"","image":"http:\/\/www.addemyplus.com\/images\/frontend_images\/blogs\/medium\/57670.jpg"}
My problem is how can I use it on my blade that whenever I type a link in the form it will fetch the Og data of that site.
and show below the image and description
I have this code on form:
<form action="{{route('store_post_path')}}" method="POST">
{{csrf_field()}}
<div class="form-group">
<label class="sr-only" for="title">title</label>
<input type="text" class="form-control" name="title" placeholder="Your Post Title or Question">
</div>
<div class="form-group">
<label class="sr-only" for="post">post</label>
<textarea class="form-control" name="post_content" id="post_content" rows="3" placeholder="Briefly explain your question or Your Post Content"></textarea>
</div>
<div class="tab-pane fade" id="images" role="tabpanel" aria-labelledby="images-tab">
<div class="form-group">
<div class="custom-file">
</div>
</div>
<div class="py-4"></div>
</div>
<div class="btn-toolbar justify-content-between">
<div class="btn-group">
<button type="Submit" class="btn btn-primary">Share your Post</button>
</div>
</div>
</form>
How can I use the code to fetch the Og property of the link? I want to view the image and title of the link below before submitting my post.. Please show some tutorial sites for this.
Get the link from the form in post content. You can do it using regex.
function getUrl($string)
{
$regex = '/https?\:\/\/[^\" ]+/i';
preg_match_all($regex, $string, $match);
return ($match);
}
$url = getUrl($string)
If there are any links in post content you will get array of links.
Use OpenGraph::fetch($url) to fetch the OG data of the url. That you can show in the view.
I'm trying to update inputs using html form in laravel:
<form action="{!! route('users.update',['id' => $users->id]) !!}" method="post">
<div class="form-group row">
<label for="colFormLabelLg" class="col-sm-3 col-form-label col-form-label-lg">customer_name</label>
<div class="col-sm-10">
<input value="{{$name}}" class="form-control form-control-lg" placeholder="col-form-label-lg">
</div>
<button type="submit" class="btn btn-primary btn-lg" > Edit</button>
</form>
Everything in the controller work perfectly however in the view page I received this error:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
No message
What am I doing wrong?
Please correct your route as POST like:
Route::post('update/{id}', 'YourController#update')->name('users.update');
You need to spoof the method as you are using to post the data. Since HTML forms can't make PUT, PATCH, or DELETE requests, you will need to add a hidden _method field to spoof these HTTP verbs. The #method Blade directive can create this field for you like this:
<form action="/foo/bar" method="POST">
#method('PUT') //add this to your form
</form>
or
<form action="/foo/bar" method="POST">
{{ method_field('patch')}} //add this to your form
</form>
You need to put #csrf and #method('PATCH') inside your form view.
I have the same problem and I had it worked after adding some lines in my code:
Just use $users->id instead of doing it as array ['id' => $users->id]
Use csrf and spoof the method by adding #method('PUT')
Your code should look like this:
<form action="{!! route('users.update', $users->id) !!}" method="post">
#csrf
<!--Some fields-->
#method('PUT')
</form>
I am working on the live search of an HTML form in which if I type La, everything should show up related to La. For Example: La Man, La Fa etc.
The HTML code which I have used for that are:
<form autocomplete="off" id="search_form" method="GET" action="/search/helloWorld/{chars}">
<div style="padding-top:18%;margin-left:auto; margin-right:auto;">
<!--
<div class="dates">
<div class="start_date" style="width:50%;margin-right:3%;">
<input readonly="readonly" class="form-control start_date mb-4" type="text" placeholder="start date" id="startdate_datepicker">
</div>
<div class="end_date" style="width:50%;margin-left:3%;">
<input readonly="readonly" class="form-control end_date mb-4" type="text" placeholder="end date" id="enddate_datepicker">
</div>
</div>
-->
<button type="submit" class="btn btn-default start_search_button" style="padding-left:12%;padding-right:12%;background-color: rgb(247, 144, 99);" >start search</button>
</div>
</form>
The routing which I have done for the routes.php file is:
//helloworld routes
Route::get('search/helloWorld/{chars}','SearchController#helloWorld');
Problem Statement:
I am wondering if I have written action="/search/helloWorld/{chars}"> in the right way in the html code above. Do we write form action by seeing the routes in in Laravel (5) ?
If u are using laravel 5.4, try this:
Add name to your route for convenient mapping like so:
Route::get ('/search/helloworld/{id}', Controller#method)->name ('search');
Then in your form,map your action to the name of the route;
<form action="{{ route ('search', $char->id) }}">
etc..
Please replace my $char->Id with your real params
Sorry im sending this on a mobile.Hope it helps.
I have an ASP .Net Core 1.1 MVC web app. When I add the [ValidateAntiForgeryToken] decoration to my Edit/Delete/Create controllers, the pages don't load (
HTTP 400 error). Any ideas why? I read somewhere that I have to add a corresponding #HtmlHelper.AntiForgeryToken to my Views, or something like that? But not sure where to put it... However, I've also read that it's not necessary to do this anymoer in ASP .Net Core...
Here is an example of my Edit view for a "Users" controller:
#model InspectionsData.Models.User
#{
ViewData["Title"] = "Edit";
}
<h2>Edit</h2>
<form asp-action="Edit">
<div class="form-horizontal">
<h4>User</h4>
<hr />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group" hidden>
<label asp-for="UserId" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="UserId" class="form-control" />
<span asp-validation-for="UserId" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="FirstName" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="FirstName" class="form-control" />
<span asp-validation-for="FirstName" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="LastName" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="LastName" class="form-control" />
<span asp-validation-for="LastName" class="text-danger"></span>
</div>
</div>
</div>
</form>
<div>
<a asp-action="Index">Back to List</a>
</div>
Thank you
You need to change your form declaration to something like the following.
<form asp-controller="User"
asp-action="Edit" method="post" asp-antiforgery="true">
Like you mention, the asp-antiforgery="true" may be optional, but I like to always add that to make sure that I show my intentions. This works for me every time.
Hope this helps.
Yes, you are correct. You don't have to manually put AntiForgeryToken anymore in ASP.NET Core.
The Form tag helper generates a hidden Request Verification Token to
prevent cross-site request forgery (when used with the
[ValidateAntiForgeryToken] attribute in the HTTP Post action method)
Taken from ASP.NET Core docs
Having that said, are you sure your actions correspond to POST request?
Also specifying the Controller is also a good idea.
<form asp-controller="Users" asp-action="Edit">
P.S: Adding the code for your action methods would help solve the issue faster.