在Laravel(PHP)控制器中使用JavaScript功能
我想在Laravel中间件中重新使用JavaScript代码,以从后端进行安全身份验证。
当前,JS代码使用Crypto-JS库,它的工作原理很好,因此我想简单地使用Laravel控制器中的JS功能。 我尝试使用本文中的JS代码: htttps:/ /www.geeksforgeeks.org/how-to-run-javascript-from-php/
但不是作品,它只是在前端回荡了纯文本。
这是我在PHP中尝试的:
<?php
namespace App\Http\Controllers;
class OAuthController extends Controller
{
public function testjsfunction() {
return '<script type="text/javascript">
function myFunction(a, b) {
return a * b;
}
// var x = myFunction(11, 10);
</script>';
}
/**
* my function called from web.php
* Route::get('/login', ([\App\Http\Controllers\OAuthController::class, 'getToken']));
*
*/
public function getToken($email, $password)
{
$response = $this->testjsfunction();
return $response;
}
?>
这是可能的还是我应该使用PHP库重新创建所有内容?
I wanted to re-use my javascript code in a Laravel middleware to make a secure authentication from my back-end.
Currently, the js code uses the crypto-js library and it works perfectly fine, so I would like to simply use the JS functions from my Laravel controller.
I tried to use a js code like in this article : https://www.geeksforgeeks.org/how-to-run-javascript-from-php/
But it doesn't works, it just echoes the plain text in the front-end.
Here is what I tried in PHP:
<?php
namespace App\Http\Controllers;
class OAuthController extends Controller
{
public function testjsfunction() {
return '<script type="text/javascript">
function myFunction(a, b) {
return a * b;
}
// var x = myFunction(11, 10);
</script>';
}
/**
* my function called from web.php
* Route::get('/login', ([\App\Http\Controllers\OAuthController::class, 'getToken']));
*
*/
public function getToken($email, $password)
{
$response = $this->testjsfunction();
return $response;
}
?>
Is it possible or should I just recreate everything with a PHP library ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要添加内容类型
尝试
You need to add content type
Try this