简体   繁体   English

Laravel 5.4:路由模型绑定的自定义视图未找到ID

[英]Laravel 5.4: Custom view for route-model-binding not finding the ID

As I'm beggining with Laravel, this should be a simple one: How can I define a custom view to be rendered when my route model binding just can't find the given ID? 正如我开始使用Laravel一样,这应该很简单:当我的路由模型绑定找不到给定的ID时,如何定义要渲染的自定义视图?

Here's my route: 这是我的路线:

Route::get('/empresa/edit/{empresa}', 'EmpresaController@edit');

Here's my controller's method: 这是我的控制器的方法:

public function edit(Empresa $empresa)
{
    if ((!isset($empresa)) || ($empresa == null)):
        //I get that this won't work...
        return 'Empresa não encontrada';
    endif;

    return view('Empresa.dadosEmpresa')->with('empresa', $empresa)->with('action', URL::route('empresa_update', ['id', $empresa->id]))->with('method', 'PATCH');
}

Here's my "attempt" to use an error handler: 这是我使用错误处理程序的“尝试”:

public function render($request, Exception $exception)
{
    if ($e instanceof ModelNotFoundException)
    {
        //this is just giving me a completely blank response page
        return 'Empresa não encontrada';
    }
    return parent::render($request, $exception);
}

How is this really done? 实际如何完成?

1. The formal way (but would it be really needed to customize in this way?) 1.正式方式(但是否真的需要以这种方式进行自定义?)

First of all, what Laravel does is, if there is not Model Row in DB with the given id, it sends 404 response, automatically. 首先,Laravel要做的是,如果DB中没有给定id的Model Row,它将自动发送404响应。

If a matching model instance is not found in the database, a 404 HTTP response will be automatically generated. 如果在数据库中找不到匹配的模型实例,则会自动生成404 HTTP响应。

So if you wanna show your customized view, you need to customize error handling. 因此,如果要显示自定义视图,则需要自定义错误处理。 So in RouteServiceProvider file, make sure it throws custom exception using 3rd param like follwoing: 因此,在RouteServiceProvider文件中,确保使用第3个参数(如RouteServiceProvider引发自定义异常:

public function boot()
{
    parent::boot();

    Route::model('empresa', App\Empresa::class, function () {
        throw new NotFoundEmpresaModelException;
    });
}

And then do same thing in the render function as you tried before. 然后在渲染函数中执行与之前尝试相同的操作。

2. The casual way - Pretty easy to go 2.休闲方式-相当容易

I d rather suggest that you do not use model injection ability, but handle the request yourself. 我建议您不要使用模型注入功能,而应自己处理请求。 So take the empresa id value as it is, and then try to find the right data, and if not found, then make your custom logic. 因此,请按原样使用empresa id值,然后尝试查找正确的数据,如果找不到,则进行自定义逻辑。 That should be pretty easy to go. 那应该很容易进行。

public function edit(Request $request, $empresa)
{
    $empresaObj = Empresa::find($empresa);
    if (!$empresa) {
      return 'Empresa não encontrada';
    }

    return view('Empresa.dadosEmpresa')->with('empresa', $empresa)->with('action', URL::route('empresa_update', ['id', $empresa->id]))->with('method', 'PATCH');
}

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

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