路由传参

接上一节,修改api/main.php文件

'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                [
                'class' => 'yii\rest\UrlRule', 
                'controller' => ['user','test'],
                'except' => ['delete'], 
                'extraPatterns'=>[
                    'GET auth/sign-in' => 'login',
                    'POST query' => 'querydb',
                    'POST update' => 'updatedb',
                    'POST del' => 'deldb',
                    'GET sendm' => 'send1',
                    'POST add' => 'adddb',
                    'POST auth/sign-in/<name>'=>'loginp',
                ],
                ]
            ],
        ],

两种形式,一种直接写在link上,控制器的函数里面作为参数接收对应值,参考TestController.php里面的loginp的函数实现;另外一种,参考TestController.php里面的adddb函数实现;

<?php
namespace api\controllers;
use yii\rest\ActiveController;
use yii\data\ActiveDataProvider;
use api\models\test;
use Yii;
use yii\web\Controller;


class TestController extends ActiveController
{
    public $modelClass = 'api\models\Test';
    public function actionLogin()
    {
        $response = \Yii::$app->response;
       // $response->statusCode = '404';
        $headers = \Yii::$app->response->headers;

        // 增加一个 Pragma 头,已存在的Pragma 头不会被覆盖。
        $headers->add('Pragma', 'no-cache');
        $headers->add('authCode', '12312324567890');

        return ['statusCode'=>'S001','message'=>'success'];
    }

    public function actions(){
        $actions=parent::actions();
        unset($actions['index']);
        return $actions;
    }

    public function actionIndex(){
        $modelClass = $this->modelClass;
        return new ActiveDataProvider(['query'=>$modelClass::find()->asArray(),
        'pagination'=>['pageSize'=>20], //每页二十条记录,翻页实现http://t2.com/tests?page=2
        ]);
    }

    public function actionLoginp($name)
    {
        return 'postp'.$name;
    }

    public function actionQuerydb(){
        $test = Test::find()->one(); //取出一条记录
        $a = $this->modelClass;
        //return $a::find()->where(['like','title',$_POST['keyword']])->all();
        return $test;

    }

    public function actionUpdatedb(){
        $test = Test::findOne(8); //更新id=8的记录
        $test->name = "new12345";
        $test->save();
       return $test;
    }

    public function actionDeldb(){
        $test = Test::findOne(8); //del
        $test->delete();
        return $test;

    }

    public function actionAdddb(){  //add

        $tt=Yii::$app->request->post();  //获取post数据

        $t1=$tt['name'];//$t1=$_POST['name'];//这样也行,不过推荐上面的做法,框架进行了字符串过滤
        $t2=$tt['t2'];
        //  return $t1;
        $test = new Test(); //add
        $test->name = "code".$t1;
        $test->t2="c2".$t2;
        $test->save();
        return $test;

    }
//要在common/main-local.php里面添加mailer设置
    public function actionSend1(){
       // return "sendmail";
        $mail= Yii::$app->mailer->compose();
        $mail->setFrom('7138784@qq.com');   
        $mail->setTo('nbllq@qq.com'); 
        $mail->setSubject("mail-test");  
        //$mail->setTextBody('txtxtxtx');   //发布纯文字文本
        $mail->setHtmlBody("<br>askme");    //发布可以带html标签的文本
        if($mail->send())  
            return "success";  
        else  
            return "fail";   
        //die(); 
    }
}
?>

发送邮件,yii-advanced-app-2.0.13这个版本需要php7.0

在common/params.php配置mail对应参数:

<?php
return [
    'adminEmail' => '7138784@qq.com',
    'supportEmail' => '7138784@qq.com',
    'senderEmail' => '7138784@qq.com',
    'senderName' => 'qblog',
    'user.passwordResetTokenExpire' => 3600,
];

在common/main-local.php里面添加mailer设置:

'mailer' => [  
            'class' => 'yii\swiftmailer\Mailer',  
             'useFileTransport' =>false,//这句一定有,false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件
            'transport' => [  
                'class' => 'Swift_SmtpTransport',  
                'host' => 'smtp.qq.com',  //每种邮箱的host配置不一样
                'username' => '7138784@qq.com',  
                'password' => 'your code',  
                'port' => '465',  
                'encryption' => 'ssl',

                ],   
         ],