求助PHP-Laravel-API大佬
本帖最后由 Samding 于 2020-4-3 16:07 编辑目的:输入数据库中存在的id就返回图1的内容,输入不存在的id就返回图2的错误信息
现在只能实现前半句,输入不存在的id并不会返回错误信息,什么都不会返回,求大佬看下为什么
Hander.php
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* @Param\Exception$exception
* @Return void
*/
public function report(Exception $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param\Illuminate\Http\Request$request
* @param\Exception$exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
if ($exception instanceof ModelNotFoundException) {
return response()->json([
'error' => 'Resource not found.'
],404);
}
return parent::render($request, $exception);
}
}
Api.php
<?php
use Illuminate\Http\Request;
use App\Article;
use Illuminate\Support\Facades\Log;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::get('articles', 'ArticleController@index');
Route::get('articles/{id}', 'ArticleController@show');
Route::post('articles', 'ArticleController@store');
Route::put('articles/{id}', 'ArticleController@update');
Route::delete('articles/{id}', 'ArticleController@delete');
ArticleController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Article;
class ArticleController extends Controller
{
public function index()
{
return Article::all();
}
public function show($id)
{
return Article::find($id);
}
public function store(Request $request)
{
return Article::create($request->all());
}
public function update(Request $request, $id)
{
$article = Article::findOrFail($id);
$article->update($request->all());
return $article;
}
public function delete(Request $request, $id)
{
$article = Article::findOrFail($id);
$article->delete();
return 204;
}
} public function render($request, Exception $exception)
{
if ($exception instanceof ModelNotFoundException) {
return response()->json([
'error' => 'Resource not found.'
],404);
}
return parent::render($request, $exception);
}
删除if判断即可 琴似蓝调 发表于 2020-4-3 15:23
public function render($request, Exception $exception)
{
if ($exception instanc ...
删掉了还是和之前一样呢 51-53也屏蔽掉,,, 你的操作真的是让我狠吃惊。。。。 <?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Article;
class ArticleController extends Controller
{
public function index()
{
return Article::all();
}
public function show($id)
{
if($article = Article::find($id)){
return response()->json($article);
}
return response()->json(['error'=> 'resource not found'], 404);
}
public function store(Request $request)
{
return Article::create($request->all());
}
public function update(Request $request, $id)
{
$article = Article::findOrFail($id);
$article->update($request->all());
return $article;
}
public function delete(Request $request, $id)
{
$article = Article::findOrFail($id);
$article->delete();
return 204;
}
} 查询数据库不存在, 并不会出现异常, 所以去异常那里处理本身就是不对的, 再者, 你如果写接口,就应该有规范, 你现在这种格式返回是不行的。 建议看看接口规范。 猜你应该是新手 本帖最后由 唯一丶 于 2020-4-3 15:47 编辑
如果你要在异常处理器中处理的话,
```php
public function show($id)
{
return Article::find($id);
}
```
这里就应该使用
```php
public function show($id)
{
return Article::findOrFail($id);
}
```
这样当不存在时才会抛出模型不存在异常,Model::find($id) 如果不存在,返回的是 null
[快速入门](https://learnku.com/docs/laravel/5.5/eloquent/1332#475874)
唯一丶 发表于 2020-4-3 15:45
如果你要在异常处理器中处理的话,
```php
懂了,终于搞定了,谢谢 本帖最后由 Samding 于 2020-4-3 16:15 编辑
琴似蓝调 发表于 2020-4-3 15:38
51-53也屏蔽掉,,,
这样直接凉了呀,52也屏蔽掉还是返回空呀
页:
[1]