简体   繁体   English

从Laravel控制器调用Javascript函数并返回数据

[英]Calling Javascript function from Laravel controller and return data

I want to call a firebase auth function (which is in Javascript) from my controller and I want the returned token back in my controller, but I do not know how to proceed. 我想从控制器中调用firebase auth函数(在Javascript中),并且希望将返回的token返回到控制器中,但是我不知道如何继续。

I know how to post ajax request to controller and get data back, however I don't know how to make a request the other way around. 我知道如何将ajax请求发布到控制器并获取数据,但是我不知道如何以其他方式提出请求。

I would be thankful if you can put me and other future users in right direction. 如果您能使我和其他未来的用户走上正确的方向,我将不胜感激。

This is not going to work. 这是行不通的。

You know a webserver is always supposed to be online. 您知道Web服务器始终应该处于联机状态。 That way, you can always make a request. 这样,您可以随时提出请求。

A client-side (user), does not need, and isn't always online. 一个客户端(用户), 并不需要,而不是永远在线。 If they close the browser window, you can't interact with them anymore. 如果他们关闭浏览器窗口,您将无法再与他们互动。 The server would have a hard time checking what user just went down, out of the thousands of users you have. 服务器将很难检查您所拥有的数千个用户中刚刚掉线的用户。

Making a call to a webserver is possible, but making a call to a client is not. 可以致电Web服务器,但不能致电客户。

There are, next to that reason, security issues if you would be able to make a request to the client-side. 除此之外,如果您能够向客户端提出请求,还会有安全问题。

What you should probably do, is make a seperate request for the authentication token from Javascript, make Laravel call the Firebase AUTH server and then return the token it received. 您可能应该做的是,从Javascript分别请求身份验证令牌,让Laravel调用Firebase AUTH服务器,然后返回它收到的令牌。


update 更新

It is possible to make requests to a client using HTTP2, by using server-push . 通过使用server-push ,可以使用HTTP2向客户端发出请求。 To do that properly, especially with an authentication token, you will need some experience in building webservices and security. 为了正确执行此操作,尤其是使用身份验证令牌,您将需要一些构建Web服务和安全性的经验。

PHP is a server side language and Javascript is client side. PHP是服务器端语言,而Javascript是客户端。 You cannot javascript on server and vice-versa. 您不能在服务器上使用JavaScript,反之亦然。 If you just need to generate JWT token, there are PHP libraries available for that already. 如果您只需要生成JWT令牌,则已经可以使用PHP库。 But if you really want to execute javascript on server, that I think is not the case, you'll have to use nodejs 但是,如果您真的想在服务器上执行javascript,我认为并非如此,则必须使用nodejs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM