File tree 1 file changed +16
-12
lines changed
1 file changed +16
-12
lines changed Original file line number Diff line number Diff line change 7
7
class SearchController
8
8
{
9
9
10
- function index ($ params )
10
+ function index (array $ params ): void // Just echo something
11
11
{
12
12
13
- $ data = json_decode (file_get_contents ("/home/max/absent/api/dict.json " ), true );
13
+ $ data = json_decode (file_get_contents (__DIR__ ."/../dict.json " ), true );
14
+
15
+ if (json_last_error () === JSON_ERROR_NONE && is_array ($ data )){
16
+
17
+ $ word = strtolower ($ params ["word " ]);
14
18
15
- $ word = strtolower ( $ params [ " word " ]);
19
+ foreach ( $ data as $ query => $ translate ){
16
20
17
- $ result = [];
21
+ if ($ query === $ word ){
22
+ $ result = ["status " => 200 , "data " => ["word " => $ word , "translate " => $ translate ]];
23
+ break ;
24
+ }
25
+ }
18
26
19
- foreach ( $ data as $ query => $ translate ){
27
+ if (! $ result ) $ result = [ " status " => 504 , " data " => [ " word " => $ word , " massage " => " Word not found " ]];
20
28
21
- if ($ query == $ word ){
22
- $ result = ["status " => 200 , "data " => ["word " => $ word , "translate " => $ translate ]];
23
- break ;
24
- }
29
+ } else {
30
+ $ result = ["status " => 501 , "data " => ["massage " => "Internal server error " ]];
25
31
}
26
32
27
- if (!$ result ) $ result = ["status " => 404 , "data " => ["massage " => "Word not found " ]];
28
-
29
- print_r (json_encode ($ result ));
33
+ echo json_encode ($ result );
30
34
}
31
35
}
You can’t perform that action at this time.
0 commit comments