To make my life simpler in a huge website, can I do this?
Route::get('view/{id}', 'PostController#show')->name('post');
Route::delete('view/{id}', 'PostController#delete')->name('post');
Route::post('view/{id}', 'PostController#save')->name('post');
And then in my form, I can do this.
<!-- Delete Form -->
<form method="post" action="{{ route('post', $post->id) }}">
<input type="hidden" name="_method" value="DELETE">
<button type="submit">Delete</button>
</form>
<!-- Edit Form -->
<form method="post" action="{{ route('post', $post->id) }}">
<input type="hidden" name="_method" value="PATCH">
<button type="submit">Edit</button>
</form>
<!-- Etc -->
Can I do this? Is this recommended?
Yes, in fact you can use the following to keep your route file clean: https://laravel.com/docs/5.2/controllers#restful-naming-resource-route-parameters
Route::resource('foobar', 'FooBarController');
This will auto generate RESTful routes for you:
if you run php artisan route:list you can see all the HTTP routes.
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>
this is a simple code to search in laravel.
the route "product" has no problem at all, but,
when I use the route "searchproduct", the url in the browser looks like this:
http://example.com/application/public/product/search?q=red+dead
so the application thinks that i'm trying to get the route "product" and send the parameter urlkey as "search?q=red+dead", which ofcourse throw an error.
View
<form method="GET" action="{{ route('searchproduct') }}" >
<input id="q" name="q" class="q" type="text" />
<button type="submit" id="submitButton" class="btn btn-primary">Go</button>
</form>
Routes
Route::get('product/{urlkey}','ProductController#index')->name('product');
Route::get('product/search/{q?}','ProductController#search')->name('searchproduct');
how to solve this issue please?
define routes this order
Route::get('product/search/{q?}','ProductController#search')->name('searchproduct');
Route::get('product/{urlkey}','ProductController#index')->name('product');
I am working on basic CRUD using Laravel. I am getting MethodNotAllowedHttpException when using PUT and DELETE method in Laravel form action. GET and POST action methods work fine.
HTML form only accept either GET or POST method so you can't use PUT and DELETE in form method. However, if you want to use PUT or DELETE then laravel provide Form method spoofing like this
<input type="hidden" name="_method" value="PUT">
Here is the form example
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Short form
<form action="/foo/bar" method="POST">
#method('PUT')
#csrf
</form>
Route
Route::put('foo/bar', 'FooController#bar');
Check details here https://laravel.com/docs/5.6/routing#form-method-spoofing
I am not using resource controller.
The route:
Route::delete('/deleteTag/{tag}','Controller2#deleteTag');
The controller function:
public function deleteTag(Tag $tag){
$Tag = Tag::where('id', $tag->id)->get()->first();
$Tag->delete();
return redirect()->action('Controller2#main');
}
The call:
<form method="delete" action="http://***/public/deleteTag/{{$tag->id}}">
{!! Form::token() !!}
<button type="submit">delete</button>
</form>
The program returns a MethodNotAllowedHttpException.
Thank you.
You may try this (Notice the hidden _method input):
<form method="post" action="http://***/public/deleteTag/{{$tag->id}}">
{!! Form::token() !!}
<input type="hidden" name="_method" value="DELETE">
<button type="submit">delete</button>
</form>
Check Form Method Spoofing.
Update:
In the latest versions of Laravel, it's possible to use blade directives for csrf and method in the form, for example:
<form method="post" action="...">
#csrf
#method('DELETE')
<button type="submit">delete</button>
</form>
It's better to change your route to this mode:
Route::resource('tags','TagController');
You should register a resourceful route to the controller. This single route declaration creates multiple routes to handle a variety of RESTful actions on the Tag resource.
Remember, 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.
<input type="hidden" name="_method" value="DELETE">
or add this in your form
{{method_field('DELETE')}}
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>