dpthuyh1678 2016-10-27 13:02
浏览 27
已采纳

Laravel在没有记录时引用应用程序URL

I've routing file below

Route::group(['middleware' => ['auth:web', 'web'],  'prefix' => 'api/1.0'], function () {

    Route::get('dashboard',  'DashboardController@index');
    Route::get('site/{id}', 'SiteController@index');

});

When I'm not logged, and trying to refer to url http://app.url/api/1.0/, is return login panel. How can I set instead login panel, return json data(for example[ message: forbidden]). Im was trying in middleware settings but laraver was crashed. Thank you

  • 写回答

1条回答 默认 最新

  • duanhe4267 2016-10-27 13:10
    关注

    You can use auth:api middleware for this as:

    Route::group(['middleware' => ['auth:api'],  'prefix' => 'api/1.0'], function () {
    
        Route::get('dashboard',  'DashboardController@index');
        Route::get('site/{id}', 'SiteController@index');
    
    });
    

    Update

    All exceptions are handled by the App\Exceptions\Handler class. You can edit the render() fuction of this class to return JSON response as:

    public function render($request, Exception $exception)
    {
        if ($request->is('api/*');) {
            return $this->renderRestException($request, $exception);
        }
    
        return parent::render($request, $exception);
    }
    

    Add two functions in the same file as:

    /**
     * Render an exception into a response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\JsonResponse
     */
    public function renderRestException(Request $request, Exception $e)
    {
        switch($e)
        {
            case ($e instanceof HttpResponseException):
                return response()->json($e->getResponse()->getContent(), $e->getResponse()->getStatusCode());
    
            case ($e instanceof ModelNotFoundException):
                return response()->json($e->getMessage(), 404);
    
            case ($e instanceof AuthenticationException):
                return response()->json('Unauthorized', 401);
    
            case ($e instanceof AuthorizationException):
                return response()->json($e->getMessage(), 403);
    
            default:
                return $this->convertExceptionToJsonResponse($e);
        }
    }
    
    /**
     * Create a Symfony response for the given exception.
     *
     * @param  \Exception  $e
     * @return \Illuminate\Http\JsonResponse
     */
    protected function convertExceptionToJsonResponse(Exception $e)
    {
        $e = FlattenException::create($e);
    
        return response()->json(array_get(SymfonyResponse::$statusTexts, $e->getStatusCode()), $e->getStatusCode());
    }
    

    Remember to import all these classes at the top of the class:

    use Exception;
    use Illuminate\Http\Request;
    use Illuminate\Auth\AuthenticationException;
    use Illuminate\Auth\Access\AuthorizationException;
    use Illuminate\Http\Exception\HttpResponseException;
    use Symfony\Component\Debug\Exception\FlattenException;
    use Illuminate\Database\Eloquent\ModelNotFoundException;
    use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入