Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Laravel exception context solution?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What is the Laravel exception context solution, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Preface

When an exception occurs, we usually want to give a friendly prompt on the user side, but the exception handling scheme that uses the framework by default is not OK.

Recently, there is a situation in the project. When we encounter that a user does not have permission to access certain information, we want to prompt for detailed reasons. For example, when visiting a team resource that is not a member of a non-member scenario, we will prompt a: you are not a member of the [xxxxxx] team and cannot view it for the time being. At the same time, you need to display the team name after coding, as well as the join button. However, the logic of the interface side is to abort directly when there is no limit of authority:

Abort_if (! $user- > isMember ($resouce- > team), 403, 'you do not have permission to access this resource')

The response results are as follows:

HTTP/1.0 403 Forbidden {"message": "you do not have access to this resource"}

It is impossible for us to use html to display the front-end prompt page of message, which is too coupled and violates the principle of front-end separation. Our goal is to return the following format:

HTTP/1.0 403 Forbidden {"message": "you do not have access to this resource", "team": {"id": "abxT8sioa0Ms", "name": "CoDesign****"}}

It is convenient for the front-end students to combine freely through the method of transmitting data with context.

Start the transformation.

Of course, this is not a complicated matter, which can be solved by directly modifying the original abort_if:

-abort_if (! $user- > isMember ($resouce- > team), 403, 'you do not have permission to access this resource') + if (! $user- > isMember ($resouce- > team)) {+ return response ()-> json ([+ 'message' = >' you do not have access to this resource', + 'team' = > [+' id' = > $resouce- > team_id,+ 'name'= > $resouce- > team- > desensitised_name,+] +], +}); +}

This seems to solve the problem, but just imagine, if an exception is detected in the closure and wants to exit, the above return-style writing will be more difficult. After all, return will only terminate the recent context, and we still hope to terminate the execution of the entire application like abort and make another modification.

Optimization and realization

Looking at the abort source code, I found that its first parameter actually supports the\ Symfony\ Component\ HttpFoundation\ Response instance, and the above? The result of our return is an example of it, so we just need to change it like this:

If (! $user- > isMember ($resouce- > team)) {abort (response ()-> json (['message' = >' you do not have access to the resource', 'team' = > [' id' = > $resouce- > team_id, 'name'= > $resouce- > team- > desensitised_name,]], 403);}

The new problem is that if it is still awkward when we need to reuse, this code will be repeated in various places with this permission, which is not what we want.

Logic reuse

In order to achieve logical reuse, I verified that I looked at the implementation of\ App\ Exceptions\ Handler and found that the render method of the parent class also had 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);} / /.

So, we can extract this logic into a separate exception class and implement the render method:

$. / artisan make:exception NotTeamMemberException

The code is as follows:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report