Laravel AJAX request, Method not allowed - php

I have a problem when trying to upload an image via bluimp's jQueryFileUpload.
In my routes i have this: Route::post('image/upload/{folder}', 'ImageController#upload');
my file input that is outside the <form> tags because it is independent to the form:
<input id="imageupload" type="file" name="image" multiple="" data-url="{{ url('admin/image/upload/members') }}" >
my jQuery function points to the data-url attribute value.:
$('#imageupload').fileupload({
dataType: 'json',
maxFileSize: 5000000,
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
done: function (e, data) {
Members.handle_image(data);
}
});
The weird thing is that when i call this method from example.app/admin/members/create it works, but when i'm trying to access it from example.app/admin/members/1/edit i get a 405, Method not allowed.
In both cases, the Method is POST.
My routes for create and edit URIs:
Route::get('members/create', [
'uses' => 'MembersController#create', 'as' => 'admin/members/create'
]);
Route::get('members/{member}/edit', [
'uses' => 'MembersController#edit', 'as' => 'admin/members/edit'
]);
I'm sure is something really stupid that i can't see.
PS. I have a Project resource, where i also upload images, using the same route and function. It works on both cases (create and edit).
Anybody had this problem ?
Thank you!

Ok, i managed to solve this, but really i don't understand why it was not working.
In my routes i have this, where the ajax url points as POST:
Route::post('image/upload/{folder}', 'ImageController#upload');
This did not work.
I changed it to:
Route::any('image/upload/{folder}', 'ImageController#upload');
And now it works.
It is strange because on my request headers i have POST method, but with post (in routes) i did not work.

HTTP 405 indicates that the request method is not supported.
Both of your routes listen for get requests
Route::get('members/create', [
'uses' => 'MembersController#create', 'as' => 'admin/members/create'
]);
Route::get('members/{member}/edit', [
'uses' => 'MembersController#edit', 'as' => 'admin/members/edit'
]);
are you sure that you don't want one, or both to be post?
Route::post('members/create', [
'uses' => 'MembersController#create', 'as' => 'admin/members/create'
]);
Route::post('members/{member}/edit', [
'uses' => 'MembersController#edit', 'as' => 'admin/members/edit'
]);

Related

Laravel: To pass common parameter value for all route in laravel

** I have Multiple Route in Laravel. I wanna pass common parameter for every Route i,e If i passed POST as a parameter in any route, I need to Call POST controller for all URL.
My Routes are below:
Original URL: www.mydomain.com/home/
Required URL: www.mydomain.com/home/post
Original URL: www.mydomain.com/follow/
Required URL: www.mydomain.com/follow/post
In above URL I have two separate blades like home.blade.php AND post.blade.php,
In Second Example I have two separate blades like follow.blade.php AND post.blade.php. Here Post.blade.php is common.
For Both home and follow must call postcontroller. My Route Controllers . **
//for follow Route
Route::get('Follow', [
'as' => 'Follow',
'uses' => 'PageController#getFollow'
]);
//for Home Route
Route::get('Home', [
'as' => 'Home',
'uses' => 'PageController#getHome'
]);
Post Route controller is below
Route::get('Post', [
'as' => 'Post',
'uses' => 'PostController#getPOST'
]);

generate route for routes defined inside group

I have this route defined inside a group
Route::group(['domain' => '{subdomain}.test.com'], function () {
Route::get('/models/{id?}', [
'as' => 'car-model',
'uses' => 'CarModelController#details'
]);
});
I want to avoid hardcoding URLs in blade
{{route('car-model', 'ford', '100) }}
but that returs this url
ford.test.com/models
no model id!
Not sure if is relevant but in my controller CarModelController.php
I defined
public function details($subdomain, $id)
why is not sending the id to the generated url? Do I need to send the $subdomain parameter to the detail function?
I found that
{{route('car-model', ['make' => 'ford', 'id' => '100]) }}
works! thanks for watching :)

Laravel Add Post Route to Resource Route

I have a Laravel 5.2 app that is using Resource routes. I have one as follows:
Route::resource('submissions', 'SubmissionsController');
I want to add a new Post route to it for a sorting form on my index page.
Route::post('submissions', [
'as' => 'submissions.index',
'uses' => 'SubmissionsController#index'
]);
I have placed the Post route above my Resource route in my routes.php.
However, a validation Request named SubmissionRequest that is meant for forms within the Submission Resource is being executed on my new Post route. Here is my SubmissionsController Method.
public function index(SortRequest $req)
{
$submission = new Submission;
$submission = $submission->join('mcd_forms', 'mcd_forms.submission_id', '=', 'submissions.id')->where('user_id', Auth::user()->id);
$data['sort_types'] = [
'name' => 'Name',
'form_type' => 'Type'
];
$data['direction'] = ( !empty($req['asc']) ? 'asc' : 'desc' );
$data['dataVal'] = ( !empty($req['sort_type']) ? $req['sort_type'] : 'submissions.id' );
$submission->whereNull('submissions.deleted_at')->orderBy(
$data['dataVal'],
$data['direction']
);
$data['submissions'] = $submission->get();
return view('submissions.index')->with($data);
}
So, when submitting the sorting form from my index page, it is running the SubmissionRequest validation even though I am specifically calling the SortRequest validation. What am I doing wrong?
I resolved it.
Since my Post route was conflicting with my Get route for submissions.index I added below the Resource route the following:
Route::match(['get', 'post'], 'submissions', [
'as' => 'submissions.index',
'uses' => 'SubmissionsController#index'
]);
This allows the route to accept both Get and Post requests by overriding the automatically generated one.
The documentation is here: https://laravel.com/docs/master/routing#basic-routing
Route::match(['get', 'post'], 'submissions', [
'as' => 'submissions.index',
'uses' => 'SubmissionsController#index'
]);
in laravel 5 its conflict with #store action

Named Routes Conflict Laravel 5.2

I am creating a Multilingual Laravel 5.2 application and I am trying to figure out how to change language when I already have the content.
routes.php
...
Route::get('home', [
'as' => 'index',
'uses' => 'SiteController#home'
]);
Route::get([
'as' => 'index',
'uses' => 'SiteController#inicio'
]);
...
I have SiteController#home and SiteController#inicio. So I change session('language') in SiteController#change_language like:
...
public function change_language ($lang){
session(['language' => $lang]);
return redirect()->action(SAME NAMED ROUTE, DIFFERENT LANGUAGE);
}
...
So, When I click on a button with
English
from /inicio (SiteController#inicio) I should be redirected to the same named route (SiteController#home) so I can check the language and display appropriate content.
Any ideas of how to get the named route or something helpful?
Thank you :)

Laravel 4 - API Endpoint returns no response

I'm building a REST API using Laravel 4, and I am having an issue. When I try to send a request to the API in Postman, it will send back errors such as 405, but it simply won't respond to the POST request, which it is designated for. Postman simply says that the server didn't respond. I have tried everything, including changing the code of the controller method to return a plain old 200 code. But, that didn't work either. There is too much code for me to post, but if anyone has any solutions, please let me know. (Btw all other endpoints can be accessed just fine)
My route definition:
Route::group(array(
'name' => 'social-conversation'
), function() {
Route::group(array(
'before' => 'auth'
), function() {
Route::post('social/conversation/create', array(
'as' => 'social-conversation-create',
'uses' => 'Social_ConversationController#createx'
));
Route::put('social/conversation/{id}/update', array(
'as' => 'social-conversation-update',
'uses' => 'Social_ConversationController#update'
));
Route::delete('social/conversation/{id}/delete', array(
'as' => 'social-conversation-delete',
'uses' => 'Social_ConversationController#delete'
));
});
});
My controller definition:
class Social_ConversationController extends BaseController {
public function create() { /* Code goes here */ }

Categories