我当前正在使用语言检测和语言翻译API。但是,此功能在所有控制器上均有效,但请求ajax,但与标题具有相同的错误。
ajax
$("#translationBtn").click(function(){
$.ajax({
url: '{{route('translation')}}',
type: 'post',
data: {_token: "{{ csrf_token() }}",
'material': $('#translationBox').val(),
},
success: function (data) {
alert(data);
}, error: function () {
alert("error!!!!");
}
});
});
控制器 通过ajax接收的值已由控制器检查。
<?php
namespace App\Http\Controllers\Home;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Helper\Translation;
class TranslationController extends Controller
{
use Translation;
public function translation(Request $request){
return $this->translation($request->material,$this>langCode($request->material));
}
}
路线
Route::post('translation', 'Home\TranslationController@translation')-
>name('translation');
API
<?php
namespace App\Http\Controllers\Helper;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
trait Translation
{
public static function langCode($papago){
$client_id = config('papago.client_sening_id');
$client_secret = config('papago.client_sening_secret');
$encQuery = urlencode($papago);
$postvars = "query=".$encQuery;
$url = "https://openapi.naver.com/v1/papago/detectLangs";
$is_post = true;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $is_post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $postvars);
$headers = array();
$headers[] = "X-Naver-Client-Id: ".$client_id;
$headers[] = "X-Naver-Client-Secret: ".$client_secret;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// echo "status_code:".$status_code."<br>";
curl_close ($ch);
if($status_code == 200) {
// echo $response['langCode'];
$json = json_decode($response, true);
$langCode = $json['langCode'];
} else {
echo "점검 중";
// echo "Error 내용:".$response;
}
return $langCode;
}
public static function translation($papago,$langCode) {
$client_id = config('papago.client_language_translation_id');
$client_secret = config('papago.client_language_translation_secret');
$encText = urlencode($papago);
if($langCode == "ko"){
$postvars = "source=ko&target=ja&text=".$encText;
}else if($langCode == "ja"){
$postvars = "source=ja&target=ko&text=".$encText;
}else{
$postvars = "source=ko&target=ja&text=".$encText;
}
$url = "https://openapi.naver.com/v1/language/translate";
$is_post = true;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $is_post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $postvars);
$headers = array();
$headers[] = "X-Naver-Client-Id: ".$client_id;
$headers[] = "X-Naver-Client-Secret: ".$client_secret;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// echo "status_code:".$status_code."<br>";
curl_close ($ch);
if($status_code == 200) {
$json = json_decode($response, true); //json_decode는 디코딩 된 json문자열을 연관배열로 만든다.
$translation = $json['message']['result']['translatedText'];
} else {
$translation = '점검 중';
// echo "Error 내용:".$response;
}
return $translation;
}
}
我正在制作一个微型翻译器,并且效果很好。但这还没有解决。我不知道自己的方式出了什么问题。只是
参数1传递给 App \ Http \ Controller \ Home \ TranslationController :: translation()必须为 Illuminate \ Http \ Request的实例,给定的字符串,调用 C:xampp \ Helmer \ Helabor \ Projection 1传递给 App \ Http \ Controller \ Home \ TranslationController :: translation()必须 是Illuminate \ Http \ Request的实例,给定的字符串,称为 in \ xperms \ Project \ triler.php:14)
我不知道此错误。
答案 0 :(得分:1)
您在以下位置使用函数名称translation
Class TranslationController
和Trait Translation
。一个是static
,另一个是instance
。
调用instance
而不是static
方法。
最好使用其他名称,以免造成混淆。
您当前的代码正在执行递归,甚至不使用trait方法。
将特征方法重命名为其他内容,然后尝试。