2015-09-17 13 views
6

के लिए एक आरईएसटी एपीआई कैसे बनाएं I yii2 मूल टेम्पलेट के लिए एक आरईएसटी एपीआई बनाना चाहता था। मैंने निम्नलिखित link का पालन किया।वाईआई 2-मूल-टेम्पलेट

मैं 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', 
    ], 
     ], 

restapi मेरी फ़ाइल का नाम है तो मैं इस यूआरएल की कोशिश की http://localhost/~user/restapi/web/ सब मैं प्राप्त एक है एक नियंत्रक बनाया 404 पेज त्रुटि नहीं मिली। किसी भी मदद की सराहना की जाएगी

उत्तर

4
उन विन्यास के साथ

:

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

अपने संसाधनों उन यूआरएल के भीतर उपलब्ध होना चाहिए:

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

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

  • नोट: Yii स्वचालित रूप से pluralize जाएगा के लिए नियंत्रक नाम जब तक आप कॉन्फ़िगर नहीं करते हैं तब तक एंडपॉइंट्स में उपयोग करें yii\rest\UrlRule::$pluralize संपत्ति ऐसा करने के लिए नहीं है।

इसके अलावा, आप अगर अपाचे का उपयोग कर सर्वर आपके web फ़ोल्डर में इस सामग्री के साथ एक .htaccess फ़ाइल जोड़कर सुंदर Urls सक्षम करने से पहले अपने सर्वर विन्यस्त करने की जरूरत (कृपया नीचे अगर लिंक करने के लिए उपयोग करते हुए उल्लेख 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

शेष एपीआई वाईआई 2 मूल ऐप में लागू करना बहुत आसान है। बस नीचे दिए गए चरणों का पालन करें। यह कोड मेरे लिए काम कर रहा है।

आवेदन संरचना

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 

एपीआई/config/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

+4

आप नियंत्रक परियोजना कैसे पहुंचेंगे? मेरा मतलब है कि यूआरएल क्या होगा? – Bloodhound