2015-09-17 2 views
6

yii2基本テンプレートのREST APIを作成したかったのですが、私は次のようにlinkに従った。Yii2-basic-template用のREST APIを作成する方法

私はusersという名前のテーブル、UserController

<?php 
namespace app\controllers; 

use yii\rest\ActiveController; 

class UserController extends ActiveController 
{ 
    public $modelClass = 'app\models\User'; 
} 
?> 

という名前のコントローラを作成し、ウェブ

'urlManager' => [ 
    'enablePrettyUrl' => true, 
    'enableStrictParsing' => true, 
    'showScriptName' => false, 
    'rules' => [ 
     ['class' => 'yii\rest\UrlRule', 'controller' => 'user'], 
    ], 
], 

     'request' => [ 
      // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation 
      'cookieValidationKey' => '4534', 
      'parsers' => [ 
     'application/json' => 'yii\web\JsonParser', 
    ], 
     ], 

で私のファイル名はので、私は私が得るすべてはこのURL http://localhost/~user/restapi/web/ を試してみましたrestapiです404ページが見つかりませんでした。任意の助けをいただければ幸いです

答えて

4

'rules' => [ 
    ['class' => 'yii\rest\UrlRule', 'controller' => 'user'], 
], 

あなたのリソースがそれらのURL内で使用可能でなければなりません:

http://localhost/~user/restapi/web/users

http://localhost/~user/restapi/web/users/1

  • 注:Yiiには、自動的用コントローラ名を複数形になります構成しない限りエンドポイントで使用するyii\rest\UrlRule::$pluralizeプロパティはそうしません。

また、あなたは(PLSが使用している場合、以下のリンクを参照してくださいのapacheサーバーを使用している場合は、あなたのwebフォルダにこの内容で.htaccessファイルを追加することにより、プリティURLを有効にする前にサーバーを設定する必要がありますnginxの):

# Set document root to be "basic/web" 
DocumentRoot "path/to/basic/web" 

<Directory "path/to/basic/web"> 
    # use mod_rewrite for pretty URL support 
    RewriteEngine on 
    # If a directory or a file exists, use the request directly 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    # Otherwise forward the request to index.php 
    RewriteRule . index.php 

    # ...other settings... 
</Directory> 

この部分は、リンクあなたのマニュアルに記載されていませんでしたあなたは、コントローラのプロジェクトへのアクセス方法

http://www.yiiframework.com/doc-2.0/guide-start-installation.html#configuring-web-servers

6

残りApiはYii2基本アプリケーションで実装するのは非常に簡単です。以下の手順に従ってください。このコードは私のために働いています。

アプリケーション構造

yourapp 
+ web 
+ config 
+ controllers 
... 
+ api 
    + config 
    + modules 
    + v1 
     + controllers 
    .htaccess 
    index.php 

API/index.phpを

<?php 

// comment out the following two lines when deployed to production 
defined('YII_DEBUG') or define('YII_DEBUG', true); 
defined('YII_ENV') or define('YII_ENV', 'dev'); 

require(__DIR__ . '/../vendor/autoload.php'); 
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); 

// Use a distinct configuration for the API 
$config = require(__DIR__ . '/config/api.php'); 

(new yii\web\Application($config))->run(); 

API/.htaccessの

Options +FollowSymLinks 
IndexIgnore */* 

RewriteEngine on 

# if a directory or a file exists, use it directly 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# otherwise forward it to index.php 
RewriteRule . index.php 

API /設定/ api.php

<?php 

$db  = require(__DIR__ . '/../../config/db.php'); 
$params = require(__DIR__ . '/params.php'); 

$config = [ 
    'id' => 'basic', 
    'name' => 'TimeTracker', 
    // Need to get one level up: 
    'basePath' => dirname(__DIR__).'/..', 
    'bootstrap' => ['log'], 
    'components' => [ 
     'request' => [ 
      // Enable JSON Input: 
      'parsers' => [ 
       'application/json' => 'yii\web\JsonParser', 
      ] 
     ], 
     'log' => [ 
      'traceLevel' => YII_DEBUG ? 3 : 0, 
      'targets' => [ 
       [ 
        'class' => 'yii\log\FileTarget', 
        'levels' => ['error', 'warning'], 
        // Create API log in the standard log dir 
        // But in file 'api.log': 
        'logFile' => '@app/runtime/logs/api.log', 
       ], 
      ], 
     ], 
     'urlManager' => [ 
      'enablePrettyUrl' => true, 
      'enableStrictParsing' => true, 
      'showScriptName' => false, 
      'rules' => [ 
       ['class' => 'yii\rest\UrlRule', 'controller' => ['v1/project','v1/time']], 
      ], 
     ], 
     'db' => $db, 
    ], 
    'modules' => [ 
     'v1' => [ 
      'class' => 'app\api\modules\v1\Module', 
     ], 
    ], 
    'params' => $params, 
]; 

return $config; 

API /モジュール/ V1/Module.php

<?php 
// Check this namespace: 
namespace app\api\modules\v1; 

class Module extends \yii\base\Module 
{ 
    public function init() 
    { 
     parent::init(); 

     // ... other initialization code ... 
    } 
} 

API /モジュール/ V1 /コントローラ/ ProjectController.php

<?php 
namespace app\api\modules\v1\controllers; 

use yii\rest\ActiveController; 

class ProjectController extends ActiveController 
{ 
    // We are using the regular web app modules: 
    public $modelClass = 'app\models\Project'; 
} 

reference

これらの構成で210
+4

:それはあなたがインストール&サーバーの構成セクションに従わなかったことを期待していたとして提供?私は何がURLになるのでしょうか? – Bloodhound

関連する問題