1.urlManager中指定errorHandler。但源码中没有对应的action。导致报错。建议加一个默认的。
'errorHandler' => [
'errorAction' => 'site/error',
],
2.appapi使用yii\rest\UrlRule指定采用restful规范访问接口。代码如下:
//#################
//# Customer Api ##
//#################
[
'class' => 'yii\rest\UrlRule',
'controller' => 'v1/customer',
// 默认开启复数,需要在url后面加一个s,譬如v1/article,默认访问为v1/articles
// 如果为false,则url后面不需要加s,譬如v1/article,默认访问为v1/article ,
//'pluralize' => false,
],
对应关系如下:
public $patterns = [
'PUT,PATCH {id}' => 'update',
'DELETE {id}' => 'delete',
'GET,HEAD {id}' => 'view',
'POST' => 'create',
'GET,HEAD' => 'index',
'{id}' => 'options',
'' => 'options',
];
然后我直接修改vender里的CustomerController(路径:vendor/fancyecommerce/fecshop/app/appapi/modules/V1/controllers/CustomerController.php,这里直接修改源文件是因为我配置的controllerMap不生效)。
示例如下:
<?php
namespace fecshop\app\appapi\modules\V1\controllers;
use fecshop\app\appapi\modules\AppapiController;
use Yii;
class CustomerController extends AppapiController
{
public $modelClass;
public function init()
{
//echo
// 得到当前service相应的model
$this->modelClass = Yii::$service->customer->getModelName();
parent::init();
}
public function actionIndex(){
echo 11;exit;
}
}
这里修改init()是有效的。但我添加actionIndex方法之后,按理说是应该覆盖原有GET请求映射的方法的。但是不起效。访问url:http://appapi.fecshoptest.com/v1/customers 请教一下是什么原因。
另外controllerMap我参照手册示例配的。也不起效。。
controllerMap配置如下:
(1)appapi/local/local_modules/V1/controllers/CustomerController.php
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2017/9/26
* Time: 下午2:47
*/
namespace appapi\local\local_modules\V1\controllers;
use Yii;
Class CustomerController extends \fecshop\app\appapi\modules\V1\controllers\CustomerController{
public function behaviors()
{
$behaviors = parent::behaviors();
unset($behaviors['authenticator']);
return $behaviors;
}
public function actionIndex(){
echo 1;exit;
}
}
(2)新建appapi/config/fecshop_local_modules/V1.php
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2017/9/26
* Time: 下午2:59
*/
return [
'v1' => [
'controllerMap' =>[
'customer'=> 'appapi\local\local_modules\V1\controllers\CustomerController'
]
]
];
也一并看看 感谢!