PostsController.php
public function store(Request $request)
{
Post::create($request->all());
return redirect('/posts');
}
my view create.blade.php in posts directory
#extends('layout.app')
#section('content')
<h1>Create Post</h1>
<form action="/posts" method="post">
<input type="text" name="title" placeholder="Enter title"> <!-- this name=title comes from create_posts_table -->
{{--{{csrf_field()}}--}}
<input type="submit" name="submit">
</form>
#stop
Route
Route::resource('/posts','PostsController');
when I submit browser goes to localhost/posts and it says:
Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.33 (Win32) OpenSSL/1.1.0h PHP/7.2.5
and no record wont save
try :
<form action="{{ route('posts.store') }}" method="post">
//
</form>
or you can read this document for more information. and take note you CAN NOT send post request by browser calling url. you can submit a form for it
There are two things wrong with this.
You're using a resource route so you need to route appropriately to the store method:
<form action="{{ route('posts.store') }}" method="post">
Also, you need to spoof methods on your other controller endpoints such as the DELETE method as it will fail with a POST request you can do that like so:
<form action="{{ route('posts.delete') }}" method="post">
#csrf
{{ method_field('DELETE') }}
Take a look here I cover just that on this video https://youtu.be/kPYtJo7RyUQ
Related
I have a form in my Laravel 5.7 application to allow a user to upload a CSV file for importing data. It has been working for a few weeks. However, suddenly it started returning 404 errors for all POST requests where the form had multipart/form-data as its enctype. The strange thing is that when I change it to URLEncoded* there is no 404.
I have tried several things.
Changing the route name.
Checking php artisan route:list output to verify routes exist.
Accessing the route via GET method, and I get method not allowed exception.
Clearing the cache.
Blade Form
<form action="/import/createParts/upload/" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="csv_upload_file">Select the File</label>
<input type="file" name="csv_upload_file">
</div>
#csrf
<button type="submit">Upload</button>
</form>
Routes
Route::prefix('import')->group(function () {
Route::get('/createParts', 'Import\CreatePartsController#index');
Route::post('/createParts/upload', 'Import\CreatePartsController#upload');
});
Ideally, this should pass the form over to the function, and another process happens.
Make sure you set a name for your route, it's useful.
Routes
Route::prefix('import')->group(function () {
Route::post('createparts/upload', 'Import\CreatePartsController#upload')
->name('import.createparts.upload');
});
Blade
<form method="post" action="{{ route('import.createparts.upload') }}"
enctype="multipart/form-data">
#csrf
<div class="form-group">
<label for="csv_upload_file">Select the File</label>
<input type="file" name="csv_upload_file">
</div>
<button type="submit">Upload</button>
</form>
Named route concept is much more easy way handling the routes.
In web.php
Route::post('import/createParts/upload', 'Import\CreatePartsController#upload')
->name('createparts.upload');
In blade
<form method="post" action="{{ route('createparts.upload') }}"
enctype="multipart/form-data">
#csrf
<div class="form-group">
<label for="csv_upload_file">Select the File</label>
<input type="file" name="csv_upload_file">
</div>
<input type="submit" value="Upload"/>
</form>
I am a beginner in laravel framework. Now I am creating a form which will send post request to /abc.php. However, after submitting the form, error unknown server error with status 419 is reported.
I have googled about this issue and I figured out that it was caused by csrf_token. I tried to except verify csrf token in this route and forms were submitted successfully.
Therefore, I have added {{ csrf_field() }} after the <form>tag and submit the form again but the form submit failed. Except not verifying the csrf token in my form, what can cause this problem? Thank you very much!
My route
Route::post('/abc.php','formSubmitController#submit');
My form
<form class="myform" name="myform" id="myform" method="post" action="/abc.php" onsubmit="return validation();" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
....
</form>
try so...
Route::post('/abc','formSubmitController#submit')->name('abc');
<form class="myform" method="post" action="{{route('abc')}}" onsubmit="return validation();" enctype="multipart/form-data">
#csrf
....
</form>
here's my routes file:
Route::any('target',function(){
return 'got it!';
});
here's my form:
<form method='post' action='example.com/target'>
<input type='text' name='message' />
<input type='submit' name='submit' />
</form>
so It doesn't matter what I fill as the message field , It should return me got it!
but when I enter a url in message field , no matter the method is POST or GET , whether the path exist's or not, I get redirected to the root (example.com)
I don't have this problem on localhost, but on the shared host
thanks
change your action into this action='/target'
Check your APP_URL env variable in config/app.php
Make sure once you are in production (or any other env for that matter), this value is changed accordingly.
edit:
Also check your action, either use named routes and route('route-name') or the url helpers to get proper urls.
Url helpers:
<form method="post" action="{{ Url::to('/target') }}">
Named routes:
Route::any('target', function(){
return 'got it!';
})->name('target-name');
With your form action like this:
<form method="post" action="{{ route('target-name') }}">
Also, is that the only route you have specified? Or are there more routes defined where you use target ?
You can change your code in 2 ways.
First of all change the route like this
Route::get('/target', array('as'=>'target_name','uses'=>function(){
return 'got it!';
}));
After that you can use like this way
1) <form method="post" action="{{ URL::to('/target') }}">
or
2) <form method="post" action="{{ URL::route('target_name') }}">
Thanks
I'm using resource controller in Laravel 5.3 and I'm having problem with deleting a record. I would like to use simple HTML code and I know that I have to add a hidden method input to make it work.
My code is very simple:
<form action="{{ url('/task', $task->id) }}">
{{ method_field('DELETE') }}
<input type="submit" value="Delete" />
</form>
After clicking submit app redirects to blank page - it doesn't go to destroy function in controller. I don't have any idea, why it's not working. I'm not using facades, is it necessary in operation like this? I'll be very glad for every tip, thank you.
You're most likely running into a TokenMismatchException. Laravel considers the DELETE method a "writable" method, so it expects a CSRF token.
You can either add a CSRF token to your form, or, if appropriate, you can add your URI to the except array in your app/Http/Middleware/VerifyCsrfToken.php file.
To add the token to your form:
<form action="{{ url('/task', $task->id) }}">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<input type="submit" value="Delete" />
</form>
I have form like this
<form action="{{ Request::root() }}/articles/update/" method="post">
<input type="hidden" name="id" value="{{ $article->id }}" />
<input type="submit" name="submit" value="Submit" />
</form>
And route like this
Route::post('articles/update', array('as' => 'articleUpdate', 'uses' => 'ArticlesController#update'));
But when I submit the form, I get MethodNotAllowedHttpException. In error report I can see that request method is GET. I have also tried using caps for method method="POST" but it didn't work.
Any ideas?
What does FireBug/Web console inspector show you? is the form being sent via GET or POST, any redirects?
Seems a redirection problem to me, after reaching the server Laravel redirects to the URL the form sent the post request.
you must use put method here. Form change like this
{{Form::open(array('url'=>'/articles/update','method' => 'PUT'))}}
Routes like this
Route::put('/articles/update','ArticlesController#update');