2016-06-28 27 views
0

を返す別のファイルに渡す:スリムルート私はこのような<strong>フォルダ構造</strong>を持っている価値

|/app 
|-/db/users.php 
|/Slim 
|/vendor 
|index.php 

index.php内側:

require 'vendor/autoload.php'; 

$app = new Slim\App(); 

$app->get('/{action}/{type}/{props}', function ($request, $response, $args) { 
    $jenis_user = strtolower($args['type']); 
    $aksi = strtolower($args['action']); 
    $prop = strtolower($args['props']); 

    $target_loc = ""; 

    if($aksi == 'get'){ 

     // the parameter is using the following format 
     // action=XX&type=YY&props=ZZ; 
     $target_loc = "./app/db/users.php?action=". $aksi . "&type=" . $jenis_user . "&props=" . $prop ; 

     // #Tips :: data extracted are in Array Format :: 
     $file = file_get_contents($target_loc); 
     $response->write(json_encode($file)); 

    } 

    return $response; 
}); 

私の目的は私場合に返される整数値を取得することですブラウザに次のパスを入力します。

http://api.myweb.com/get/seller/totalNumber 

しかし、私は、パラメータを渡すのルーティングを書いた方法が間違っているようだ:?私は

警告の警告だ:のfile_get_contents(./アプリ/ DB/users.phpアクションを= &タイプ=売り手&小道具を取得=総数):ストリームを開くことに失敗しました

どうすれば修正できますか?

+0

あなたは 'app/db/users.php'の内容を私達に与える必要があります...これはひどいデザインです。あなたの依存関係はクラスでなければなりません。クラスからの出力を求めて、それをjsonifyすることができます。 – geggleto

答えて

0

あなたは本当にあなたが余分なこのようなhttpまたはhttpsプロトコルを指定する必要があり、この何をするときには、file://プロトコルでは、存在しない名前users.php?action=get&type=seller&props=totalnumberでファイルを取得しようとしている。

$content = file_get_contents('http://example.com/users.php?x=y'); 

となり、成功します。

お知らせは:あなたは、このための関数やクラスを定義するとき、これは悪いstructurで、それが良いだろう、それはこのようなことができます:

のfunctions.php

function getUsers($type, $props) { 
    // do your stuff 
    return $yourUsers; 
} 

index.phpのまたはルートファイル

include 'functions.php'; // include the functions 

$app = new Slim\App(); 
$app->get('/{action}/{type}/{props}', function ($request, $response, $args) { 
    $jenis_user = strtolower($args['type']); 
    $aksi = strtolower($args['action']); 
    $prop = strtolower($args['props']); 

    if($aksi == 'get'){ 

     $users = getUsers($jenis_user, $prop); // execute the function to collect the user 
     $response->write(json_encode($users)); 
    } 

    return $response; 
}); 

次に、file_get_contents()は必要ありません。

関連する問題