2016-12-07 4 views
-1

私のコードの構造:私の安らかなapi YII2に何が問題なのですか?

  • config/
  • controllers/
  • models/
  • api-data/
    • config/
      • api.php
    • modules/
      • v1/
        • controllers/
          • ProgramStudiController.php
        • module.php
        • ここでは
    • index.php
    • .htaccess

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/programstudi'], 
      ], 
     ], 
     'db' => $db, 
    ], 
    'modules' => [ 
     'v1' => [ 

      'basePath' => '@app/modules/v1', 
      'class' => 'api-data\modules\v1\Module', // here is our v1 modules 
      'controllerNamespace' => 'app\modules\v1\controllers', 
     ], 
    ], 
    'params' => $params, 
]; 

return $config; 

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(); 

.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 

module.php

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

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

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

ProgramStudiController.php:誰もが私がやっているのか分からない私はhttp://localhost/sbmptn/api-data/v1/programstudiと呼んでますが、応答は404である

<?php 
namespace \api-data\modules\v1\controllers; 

use yii\rest\ActiveController; 

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

違う?

+0

質問を基本的に再フォーマットしました。ネストされた箇条書きは、ディレクトリツリーを表示するのに適しています。これは、各項目の先頭に '*'をつけて、各レベルに4つのスペースをインデントすることで可能です。 – Chris

+0

おかげさまであなたの情報、私はここで初心者 –

答えて

0

conrollerの名前をProgramStudiController、次にProjectControllerとします。コントローラ名にcamelcaseを使用したため、名前を変更して/api-data/v1/program-studiのようなURLを-と組み合わせて使用​​してください。

Btwの場合、controllers/フォルダにメインコントローラを作成してからapi-data/modules/v1/controllersに拡張する必要があります。

+0

あなたの答えをありがとう、メインコントローラのいくつかの例を与えるために私を助けることができます、私はyii2初心者ですか? –

+0

メインコントローラーは通常のコントローラーで、すべてのアクションを入れ、 'api-data/v1/controllers'でこのコントローラーを拡張するだけです。メインコントローラーからすべてのメソッドを継承し、必要に応じてオーバーライドできますあなたがこれを必要としない、ちょうどappロジック) – Yupik

関連する問題