Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 定制";“未找到”;Laravel的行为';“s路由”;明确的约束”;_Php_Laravel_Binding_Model_Routes - Fatal编程技术网

Php 定制";“未找到”;Laravel的行为';“s路由”;明确的约束”;

Php 定制";“未找到”;Laravel的行为';“s路由”;明确的约束”;,php,laravel,binding,model,routes,Php,Laravel,Binding,Model,Routes,以下是文档: 路线: Route::group(['prefix' => 'u'], function () { Route::post('create', ['as' => 'createUser', 'uses' => 'UserController@create']); Route::get('{uuid}', ['as' => 'userDashboard', 'uses' => 'UserController@dashboard']); }

以下是文档:

路线:

Route::group(['prefix' => 'u'], function () {
    Route::post('create', ['as' => 'createUser', 'uses' => 'UserController@create']);
    Route::get('{uuid}', ['as' => 'userDashboard', 'uses' => 'UserController@dashboard']);
});
UserController.php:

public function dashboard(User $uuid)
{
    return View::make('user.dashboard');
}
每当在数据库中找不到用户时,就会抛出以下两个异常:

2/2
NotFoundHttpException in Handler.php line 103:
No query results for model [App\User].

1/2
ModelNotFoundException in Builder.php line 303:
No query results for model [App\User].
如何自定义错误?我想重定向到
createUser
路由。文档指示将
闭包作为第三个参数传递,但我不知道如何使用当前代码来传递

编辑1

这就是我迄今为止一直尝试但没有成功的方法:

   Route::model('{uuid}', ['as' => 'userDashboard', 'uses' => 'UserController@dashboard'], function () {
        App::abort(403, 'Test.');
    });

   Route::get('{uuid}', ['as' => 'userDashboard', 'uses' => 'UserController@dashboard'], function () {
        App::abort(403, 'Test.');
    });

为此,您需要检查模型中是否存在id,如下所示:

public function dashboard(User $uuid)
{
    if(User::find($uuid))
    {
        return View::make('user.dashboard');
    } else {
        redirect('xyz');
    }
}

为此,您需要检查模型中是否存在id,如下所示:

public function dashboard(User $uuid)
{
    if(User::find($uuid))
    {
        return View::make('user.dashboard');
    } else {
        redirect('xyz');
    }
}

您可以在
app/Exceptions/Handler.php

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $e
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $e)
{
    if (!env('APP_DEBUG')) {      
        if ($e instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
            //treat error
            return response()->view('errors.404');
    }

    return parent::render($request, $e);
}
编辑1: 这段代码来自一个正在工作的项目,因此,如果这段代码不起作用,那么它肯定在其他地方有错误:

if ($e instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
    $data= \App\Data::orderBy('order', 'asc')->get();                              
    return response()->view('errors.404', [
        'data' => $data                  
    ], 404);
}
编辑2:
您可以使用上述代码和创建新的异常类型,以获得所需的行为。至少据我所知。:)

您可以在
app/Exceptions/Handler.php

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $e
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $e)
{
    if (!env('APP_DEBUG')) {      
        if ($e instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
            //treat error
            return response()->view('errors.404');
    }

    return parent::render($request, $e);
}
编辑1: 这段代码来自一个正在工作的项目,因此,如果这段代码不起作用,那么它肯定在其他地方有错误:

if ($e instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
    $data= \App\Data::orderBy('order', 'asc')->get();                              
    return response()->view('errors.404', [
        'data' => $data                  
    ], 404);
}
编辑2:
您可以使用上述代码和创建新的异常类型,以获得所需的行为。至少据我所知。:)

我认为本教程将对您有所帮助

我认为本教程将对您有所帮助

对于我做过的类似案例, 我将父函数
illumb\Foundation\Exceptions\Handler ishttpeexception
复制到
app/Exceptions/Handler.php
,并将其名称更改为my
isUserNotFoundException

protected function isUserNotFoundException(Exception $e)
{
    return $e instanceof UserNotFoundException;
}
而不是在渲染函数中添加

  if ($this->isUserNotFoundException($e))
       return redirect('path')->with('error',"Your error message goes here");
以下代码必须放在RouteServiceProvider::boot方法中

$router->model('uuid', 'App\User', function () {
throw new UserNotFoundException;
}))

并确保将其包含在视图文件中

这个论坛帖子可能会对你有所帮助

https://scotch.io/tutorials/creating-a-laravel-404-page-using-custom-exception-handlers
对于我做过的一个类似的案例, 我将父函数
illumb\Foundation\Exceptions\Handler ishttpeexception
复制到
app/Exceptions/Handler.php
,并将其名称更改为my
isUserNotFoundException

protected function isUserNotFoundException(Exception $e)
{
    return $e instanceof UserNotFoundException;
}
而不是在渲染函数中添加

  if ($this->isUserNotFoundException($e))
       return redirect('path')->with('error',"Your error message goes here");
以下代码必须放在RouteServiceProvider::boot方法中

$router->model('uuid', 'App\User', function () {
throw new UserNotFoundException;
}))

并确保将其包含在视图文件中

这个论坛帖子可能会对你有所帮助

https://scotch.io/tutorials/creating-a-laravel-404-page-using-custom-exception-handlers

这其实很简单。因为没有一个答案能给出一个明确的答案,所以我自己来回答

在文件
RouteServiceController.php
boot
函数中添加以下内容:

    $router->model('advertiser', 'App\Advertiser', function () {
        throw new AdvertiserNotFoundException;
    });
然后在
App\Exceptions
中创建一个新的空类,在本例中称为
AdvertiserNotFoundException.php

<?php

namespace App\Exceptions;

use Exception;

class AdvertiserNotFoundException extends Exception
{

}

就这样!:)

这其实很简单。因为没有一个答案能给出一个明确的答案,所以我自己来回答

在文件
RouteServiceController.php
boot
函数中添加以下内容:

    $router->model('advertiser', 'App\Advertiser', function () {
        throw new AdvertiserNotFoundException;
    });
然后在
App\Exceptions
中创建一个新的空类,在本例中称为
AdvertiserNotFoundException.php

<?php

namespace App\Exceptions;

use Exception;

class AdvertiserNotFoundException extends Exception
{

}


就这样!:)

您是否处于调试模式?这里是文档:@clear都是。我要做的是重定向到另一个页面,而不是显示错误。您是否处于调试模式?这里是文档:@clear都是。我要做的是重定向到另一个页面,而不是显示错误。如果在指定模型的(
User
)数据库表中找不到传递的参数,它甚至不会启动函数。hmmm。。我明白了,最好的方法是使用中间件。如果在指定模型的(
User
)数据库表中找不到传递的参数,它甚至不会启动函数。hmmm。。我明白了,最好的方法是使用中间件。如何为不同的模型/路由定制不同的错误?在本例中,我希望重定向到用户创建页面,但在其他实现中,我可能希望重定向到其他页面。您的方法将对每个
ModelNotFoundException
应用相同的规则。如何为不同的模型/路由自定义不同的错误?在本例中,我希望重定向到用户创建页面,但在其他实现中,我可能希望重定向到其他页面。您的方法将对每个
ModelNotFoundException
应用相同的规则。它显示的与文档中的几乎相同。是我感兴趣但不知道如何与控制器一起使用的部分。我正在传递第三个参数,但它没有任何作用。它显示的内容与文档中的内容基本相同。是我感兴趣但不知道如何与控制器一起使用的部分。我通过了第三个论点,但没用。谢谢。您是如何使
->with()
(flash会话)工作的?我已经尝试了所有的方法,但都不起作用。请检查修改后的行以进行重定向,是的,请确保在blade Template中包含“{session('error')}}”错误。我无法使flash会话工作,如果您愿意,请检查我的其他问题:谢谢。您是如何使
->with()
(flash会话)工作的?我已经尝试了所有方法,但根本无法工作。请检查修改后的行以进行重定向,是的,请确保在blade Template中包含“{session('error')}}”错误。我无法使flash会话工作,如果您愿意,请检查我的其他问题: