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

How to solve the problem that the api routing service of laravel after referencing Dingo is not available

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to solve the problem that the api routing service of laravel after referencing Dingo is unavailable". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve the problem of unavailability of laravel routing service of laravel after referencing Dingo".

Follow-up questions on Wechat web login

In the current situation, if we open it in Wechat, if we do not log in, we will directly jump to the login page to log in, but in fact, in the project, users who open Wechat will directly go to the authorization page instead of login with account password.

Well, the best way to do this is to use middleware to detect what browser the user uses to open the project.

Create middleware to detect browsers

Judge whether to open it from Wechat browser in the middleware.

And define our middleware in the kernel.php file

Then add middleware to the route.

We use the url address when we redirect, which is not good. We need to use routing aliases.

Then modify the jump checkBrowser in the middleware

It used to be like this.

Then let's visit.

At this time, it will be reported that the route does not exist, because Dingo has taken over the routing service of api, so the route that comes with laravel will not load the route in api.

Then we need to define a routing assistant that belongs to api

First, let's take a look at the routing assistant that comes with laravel.

As you can see from the code, the identity of the url bound in the container will be parsed through app, and the corresponding service class UrlGenerator of url can be found through the identity search in the binding container.

Then we open the source code of dingo and we can see that there is a helper function in the helper function of dingo.

According to the annotation, it will return an object of Dingo\ Api\ Routing\ UrlGenerator. The UrlGenerator of this class name of dingo has the same name as the UrlGenerator that comes with laravel, so it will be forwarded via dingo's url route.

So we can understand that the version () of helpers in dingo is the same as app ('url') in laravel's helpers.

Version () = app ('url')

So create a route_api () in dingo\ helpers based on the helper function with route (); copy the contents of route to dingo\ helpers.php

Version means version. Here is the version number of api. Give it a default, which is also the v1 set by env, and then modify the code copied from route.

Then we can do a test, in order to facilitate us to directly write a random one in the route of web.

The test results prove that the route we registered is available.

However, this approach is not very good, because we put the custom route in the helper function of dingo. If we update dingo (composer update) once, the route will not exist, so we are solving this problem.

About the loading of helpers.php by laravel

You can know vendor/laravel/framework/src/Illuminate/Foundation/helpers.php as follows. This is the helpers.php helper function file that comes with laravel.

It is loaded through composer.json loading

Customize a helpare.php to app\ Support

Because the automatic loading of composer is loaded on the second line of the entry file public/index.php.

So we add the following to the root directory of the project, composer.json

Then let's test it. Before testing, we need to delete the routing assistant defined in the helpers.php of dingo.

It is still not possible after visiting here, because we need to recompile the composer.json

Use the command: composer dump-autoload

This is because I made a mistake in composer.json, so we must pay attention to it.

Then OK the access after refreshing, which means that our routing assistant has already registered as

Then go back to our project, visit, and still report that api_route does not exist

Modify the middleware

In this way, it will be accessed directly.

Thank you for your reading. The above is the content of "how to solve the problem that the api routing service of laravel after referencing Dingo is not available". After the study of this article, I believe you have a deeper understanding of how to solve the problem of unavailability of laravel routing service of laravel after referencing Dingo, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report