2015-09-17 18 views
6

Chciałem stworzyć interfejs REST API dla podstawowego szablonu Yii2. Postępowałem zgodnie z następującym link.Jak utworzyć interfejs REST API dla Yii2-basic-template

Stworzyłem tabelę o nazwie users, kontroler o nazwie UserController

<?php 
namespace app\controllers; 

use yii\rest\ActiveController; 

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

iw internecie

'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', 
    ], 
     ], 

moja nazwa pliku jest restapi więc próbowałem ten link http://localhost/~user/restapi/web/ wszystko ja dostać jest Błąd 404 strona nie znaleziono. Każda pomoc zostanie doceniona:

Odpowiedz

4

Z tych konfiguracjach:

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

twoje zasoby powinny być dostępne w ciągu tych adresów URL:

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

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

  • UWAGA: Yii automatycznie pluralize nazwy kontrolera dla używać w punktach końcowych, chyba że skonfigurujesz własność yii\rest\UrlRule::$pluralize, aby tego nie robić.

Również trzeba skonfigurować serwer przed włączeniem Ładne adresu URL dodając plik .htaccess ta zawartość do folderu web przypadku korzystania serwera Apache (pls odnoszą się do łączenia poniżej w przypadku korzystania 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> 

Ta część nie została opisana w dokumentacji linku pod warunkiem, jak to było przypuszczać, że zrobiłeś śledzić instalacji sekcję konfiguracji serwera &:

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

6

Rest Api jest bardzo prosty w implementacji w podstawowej aplikacji Yii2. Wystarczy wykonać poniższe czynności. Ten kod działa dla mnie.

struktury aplikacji

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/konfiguracji/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/moduły/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/moduły/v1/regulatory/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

jak będzie dostęp projekt kontrolera? Mam na myśli jaki będzie adres URL? – Bloodhound

Powiązane problemy