Home>Article>PHP Framework> Share a Laravel exception context solution

Share a Laravel exception context solution

藏色散人
藏色散人 forward
2021-01-21 11:05:06 1878browse

The following column will introduce you to a Laravel exception context solution from theLaravel Tutorialcolumn, I hope it will be helpful to friends in need!

Recently, the project encountered a situation. When we encounter a user who does not have permission to access certain information, we hope to prompt a detailed reason. For example, when accessing a team resource and non-members access it, a prompt will be given:You are not a member of the [xxxxxx] team and cannot view it temporarily. You can 77e8b2882cf3b4fb0801ff5c616a6d8a. At the same time, the coded team name and the join button need to be displayed, but the logic of the interface is that there is no permission. At that time, it wasabortdirectly:

abort_if(!$user->isMember($resouce->team), 403, '您无权访问该资源');

The response result obtained is as follows:

HTTP/1.0 403 Forbidden{ "message": "您无权访问该资源"}

It is impossible for us to use html to display the front-end prompt page. This would be too coupled. Strong and violates the principle of separation of front and back ends. Our goal is to return the following format to solve the problem:

HTTP/1.0 403 Forbidden{ "message": "您无权访问该资源", "team": { "id": "abxT8sioa0Ms", "name": "CoDesign****" }}

Passing data by carrying context makes it easier for front-end students to freely combine.

Start the transformation

Of course, this is not a complicated matter. It can be solved by directly modifying the originalabort_if:

- abort_if(!$user->isMember($resouce->team), 403, '您无权访问该资源'); + if (!$user->isMember($resouce->team)) { + return response()->json([ + 'message' => '您无权访问该资源', + 'team' => [ + 'id' => $resouce->team_id, + 'name'=> $resouce->team->desensitised_name, + ] + ], 403); + }

This seems to solve the problem, but just imagine, if an exception is detected in the closure and you want to exit, the abovereturnstyle of writing will be more difficult to do. , after all,returnwill only terminate the latest context environment, we still hope to terminate the execution of the entire application likeabort, and then carry out another transformation.

Optimization implementation

After reading theabortsource code, I found that its first parameter actually supports\Symfony\Component\HttpFoundation\Responseinstance, and the result of ourreturnabove is its instance, so we only need to change it to this:

if (!$user->isMember($resouce->team)) { abort(response()->json([ 'message' => '您无权访问该资源', 'team' => [ 'id' => $resouce->team_id, 'name'=> $resouce->team->desensitised_name, ] ], 403)); }

It looks like Abnormal interruption has been implemented, but a new problem has arisen. It is still embarrassing if it needs to be reused. This code will appear repeatedly in various places where this permission is judged. This is not what we want.

Logical reuse

In order to achieve logical reuse, I looked at the implementation of\App\Exceptions\Handlerand found the ## of the parent class The #rendermethod also has such a design:

public function render($request, Throwable $e) { if (method_exists($e, 'render') && $response = $e->render($request)) { return Router::toResponse($request, $response); } elseif ($e instanceof Responsable) { return $e->toResponse($request); } //...

Therefore, we can extract this logic into an independent exception class and implement the

rendermethod:

We first create an exception class:

$ ./artisan make:exception NotTeamMemberException

The implementation code is as follows:

team = $team; parent::__construct($message, 403); } public function render() { return response()->json( [ 'message' => !empty($this->message) ? $this->message : '您无权访问该资源', 'team' => [ 'id' => $this->team->id, 'name' => $this->team->desensitised_name, ], ], 403 ); } }

In this way, our logic becomes:

if (!$user->isMember($resouce->team)) { throw new NotTeamMemberException($resouce->team, '您无权访问该资源'); }

Of course it can also The abbreviation is:

\throw_if(!$user->isMember($resouce->team), NotTeamMemberException::class, $resouce->team, '您无权访问该资源');

The problem has finally been solved in a relatively perfect way. If you have a better solution, please comment and discuss it.

The above is the detailed content of Share a Laravel exception context solution. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete