简体   繁体   English

Laravel 中的路由问题,具有多个控制器和具有相同名称的不同路由

[英]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.我在 web.php 中添加了以下路线,但它不起作用。

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.我的 controller 如下所示,我正在使用 Ajax 发送数据,但我收到的错误是 Method not allowed 异常。

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

    
}

Ajax Code ---------------- Ajax 代码----------------

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.您可以通过添加我添加的上述路线来轻松管理您的路线,并且永远不会遇到路线问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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