2015-09-17 8 views
6

Ich wollte ein REST-API für eine yii2 grundlegende Vorlage erstellen. Ich folgte dem folgenden link.Wie für Yii2-basic-Vorlage ein REST-API erstellen

habe ich eine Tabelle mit dem Namen users, einen Controller namens UserController

<?php 
namespace app\controllers; 

use yii\rest\ActiveController; 

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

und im Web

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

meine Dateinamen restapi ist so habe ich versucht, diese URL http://localhost/~user/restapi/web/ alles, was ich eine bekommen ist 404 Seite nicht gefunden Fehler. Jede mögliche Hilfe würde

Antwort

4

Mit diesen Konfigurationen:

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

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

  • Hinweis:

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

    Ihre Ressourcen sollten in dieser URLs zur Verfügung Yü wird Controller-Name automatisch pluralize für Verwenden Sie in Endpunkten, wenn Sie nicht konfigurieren die yii\rest\UrlRule::$pluralize Eigenschaft nicht zu tun.


Auch müssen Sie Ihren Server konfigurieren, bevor ermöglicht Recht Urls durch eine .htaccess Datei mit diesem Inhalt zu Ihrem web Ordner hinzufügen, wenn mit Apache Server (pls beziehen unten verlinkt werden bei Verwendung von 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> 

Dieser Teil wurde nicht in der Dokumentation des Link beschrieben Sie zur Verfügung gestellt, wie es erwartet wurde, dass Sie die Installation & Serverkonfiguration Abschnitt folgen tat:

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

6

Erholung Api ist sehr einfach geschätzt werden, um in Yii2 grundlegende app zu implementieren. Befolgen Sie einfach die folgenden Schritte. Dieser Code funktioniert für mich.

Anwendungsstruktur

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/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/modules/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/modules/v1/Controller/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

wie werden Sie den Controller Projekt zugreifen? Ich meine, was wird die URL sein? – Bloodhound

Verwandte Themen