0

我正在关注本教程

http://learncooltech.com/yii2-angular-how-to-create-single-page-application-in-minutes/

我得到了这个教程,现在我可以看到 GET /books:逐页列出所有书籍;

但是,现在我想创建一些端点,并在我的控制器中添加了一个方法测试,但我得到了 404。这是我的控制器

namespace app\controllers;

use yii\rest\ActiveController;

class BookController extends ActiveController
{
    public $modelClass = 'app\models\Book';

    public function behaviors()
    {
        return 
        \yii\helpers\ArrayHelper::merge(parent::behaviors(), [
            'corsFilter' => [
                'class' => \yii\filters\Cors::className(),
            ],
        ]);
    }


    public function actionTest(){
        return json_encode(array("name"=>"John Doe"));
    }

}

我有这个作为我的 urlRule

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

我正在做

http://localhost/events/test

到达终点

我明白了

<head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="csrf-param" content="_csrf">
        <meta name="csrf-token" content="cWdHTlZnWE8uNn5jEDQVHwZXJDcUIykKADUjAgM/Ei4dXn4mOAwNCg==">
        <title>Not Found (#404)</title>
        <link href="/assets/a7503887/css/bootstrap.css" rel="stylesheet">
        <link href="/css/site.css" rel="stylesheet">
    </head>
    <body>
4

1 回答 1

0

通过将规则更改为此使其工作

 'rules' => [
                ['class' => 'yii\rest\UrlRule', 'controller' =>
                    ['book'],
                    'extraPatterns' => [
                          'GET test' => 'test',
                ],
                ],
于 2016-06-02T01:07:36.547 回答