user-4653387 user-4653387 - 4 years ago 524
PHP Question

1/1 NotFoundHttpException in Controller.php line 268: Controller method not found. in laravel 5

I am trying to post some data into laravel controller using jquery ajax, but it shows error


"1/1 NotFoundHttpException in Controller.php line 268: Controller
method not found."


and in console is throws

"NetworkError: 404 Not Found "

here is my code below:

in routes.php

Route::controller('contact', 'ContactController');
Route::post('searchDependency','ContactController@postSearchDependency');


ContactController.php

public function postSearchDependency(){

//dd('hello');
}


ajax into index.blade.php

$('#deleteRecord').click(function(){

var total = $('input[class="ids"]:checkbox:checked').length;
var ContactId=[];
$('input[class="ids"]:checked').each(function() {
ContactId.push($(this).val());
});
//alert(ContactId);
$.ajax({
type: 'POST',
data: 'contactId: ContactId',
url: '<?php echo URL::to('contact/searchDependency')?>',
success: function(contacts){
}
});

});


i already posted data using ajax to different method of another controller successfully this way, i already tried the possible solutions, but don't know what happens in this case, please anyone helps me out.

Answer Source

Assuming that the routes.php file contains the line:

Route::controller('contact', 'ContactController');

and your ContactController has the following function defined:

public function postSearchDependency() {
    dd('hello');
}

Then this function would be called by a post request to the url contact/search-dependency instead of contact/searchDependency

This is due to transformation of CamelCase function names to more friendly and readable URL's when using Route::controller.

If you additionally define a direct route to your controller methode like:

Route::post('searchDependency','ContactController@postSearchDependency');

This route gets called directly by searchDependency in the URL (without contact before).

You can always check your existing routes and urls by typing

php artisan route:list

in your terminal.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download