【问题标题】:Laravel Routes not working for specific casesLaravel 路线不适用于特定情况
【发布时间】:2018-07-30 19:44:24
【问题描述】:

这里有专家吗?这是我面临的一个非常奇怪的问题。

我的网站托管在 aws 机器上。代码在那里工作得非常好。然后它被转移到另一台服务器上,这个奇怪的问题就出现了。

我有更新汽车的路线

Route::put('vehicles/{vehicle}', 'VehicleController@update');

编辑表格

{!! Form::model($vehicle, ['url' => ['backend/vehicles', $vehicle->id], 'method' => 'put','files' => true , 'class' => 'form-horizontal form-label-left', 'role' => 'form']) !!}

   @include( 'backend.vehicles.form' )

{!! Form::close() !!}

现在这是奇怪行为开始的地方,每当我尝试更新在服务器移动之前创建的汽车时,它会在 RouteCollection.php 中显示 MethodNotAllowedHttpException

但是当我创建一辆车然后更新这辆新车时,操作成功了。请帮忙。

还有一件事,在 routeCollection.php 中,它匹配请求的路由,它显示旧车的 GET 方法,但新车的 put 方法

public function match(Request $request){
   // die($request->getMethod()); $routes = $this->get($request->getMethod());
   $route = $this->check($routes, $request);

    if (! is_null($route)) {
        return $route->bind($request);
    }

    // If no route was found we will now check if a matching route is specified by
    // another HTTP verb. If it is we will need to throw a MethodNotAllowed and
    // inform the user agent of which HTTP verb it should use for this route.
    $others = $this->checkForAlternateVerbs($request);

    if (count($others) > 0) {
        return $this->getRouteForMethods($request, $others);
    }

    throw new NotFoundHttpException;
}

请任何人。

【问题讨论】:

    标签: php laravel-5.2


    【解决方案1】:

    在表单中添加这个

    {{ method_field('PUT') }}
    

    或在 HTML 中

    <input type="hidden" name="_method" value="PUT">
    

    【讨论】:

    • 使用Form::model()时应该会自动添加;
    • 你有没有观察到这是否在 html 中呈现?
    • 我试过这个,但它只会导致两次添加 _method 字段。错误仍然存​​在。
    • @myLibrary 完全正确
    • 我想他也考虑到了crsf_token
    【解决方案2】:

    请尝试以下解决方案:

    Route::put('vehicles/{vehicle}', 'VehicleController@update');
    

    改成

    Route::match(['put', 'patch'], 'vehicles/{vehicle}', 'VehicleController@update');
    

    和表格

    {!! Form::model($vehicle, ['action' => ['VehicleController@update', $vehicle->id], 'method' => 'put','files' => true , 'class' => 'form-horizontal form-label-left', 'role' => 'form']) !!}
    
       @include( 'backend.vehicles.form' )
    
    {!! Form::close() !!}
    

    它对你有用吗?

    【讨论】:

    • 实际上我的控制器位于一个名为 backend 的文件夹中。因此我不能使用这种语法。
    • @SimonWamztech 如果你尝试 {!! Form::model($vehicle, ['url' => 'backend/vehicles/' . $vehicle->id, 'method' => 'put','files' => true , 'class' => 'form -horizo​​ntal form-label-left', 'role' => 'form']) !!} @include( 'backend.vehicles.form' ) {!! Form::close() !!} 这对你有用吗?
    猜你喜欢
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 2017-04-20
    • 2015-12-24
    • 1970-01-01
    相关资源
    最近更新 更多