简体   繁体   中英

Route issue in Laravel with several controllers and different route with same name

I have added below routes in web.php but it's not working.

Route::post('show', [
'as' => 'usersinformation.show',
'uses' => 'usersinformationController@show'


 ]);



 Route::post('store', [
    'as' => 'usersinformation.store',
    'uses' => 'usersinformationController@store'
  ]);



 Route::get('store',[usersController::class, 'store'])->name('usersinformation.store');
    Route::post('/store', 'usersController@store');
    Route::post('store',[usersController::class, 'store'])->name('users.store');
    Route::get('/index', 'usersController@index');

my controller is as below and I am using Ajax to send data but the error I receive is Method not allowed exception.

public function store(Request $request)
{
    //
    $fname = $request -> fname;
    $lname = $request -> lname;
    $pnumber = $request -> pnumber; 

    
}

Ajax Code ----------------

data = {
    _token: $('input#usersinformation-token').val(),
    'fname': $('input#first_name').val(), 
    'lname': $('input#last_name').val(),
    'pnumber': $('input#phonenumber').val()

};
$.post(url, data, function(data, status){
    alert('working' + data + "    " + status );
    $('div#load-content').html(data);
} );

I have resolved the issue by very much adding namespaces to my route contents like below:

route::post('usersinformation/store', 'usersinformationController@store');
route::post('usresinformation/destroy', 'usersinformationController@destroy');

you can easily manage your routes by adding routes like above that I have added and will never face issues for the routes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM