限时免费试用:欢迎注册 api.bigmodel.org ,快速体验大模型 API 接入服务。
当前位置:首页 >开发者 >网站框架 >后端框架 >YII

yii初体验(5)控制器 操作的简单介绍

分类:YII时间:2019-08-23浏览:2524
控制器
ControllerID/ActionID
ModuleID/ControllerID/ActionID(属于模块下的控制器)
控制器ID应仅包含英文小写字母、数字、下划线、中横杠和正斜杠
  • article 对应 app\controllers\ArticleController;
  • post-comment 对应 app\controllers\PostCommentController;
  • admin/post-comment 对应app\controllers\admin\PostCommentController;
  • adminPanels/post-comment 对应app\controllers\adminPanels\PostCommentController
方法(操作)
index 和 hello-world
public functionactionIndex() public functionactionHelloWorld()
操作ID应仅包含英文小写字母、数字、下划线和中横杠,操作ID中的中横杠用来分隔单词。
独立操作
public function actions() { return [ // 用类来申明"error" 操作 'error' => 'yii\web\ErrorAction', // 用配置数组申明 "view" 操作 'view' => [ 'class' => 'yii\web\ViewAction', 'viewPrefix' => '', ], ]; }
// 用户浏览器跳转到 http://example.com return $this->redirect('http://example.com');
默认操作,默认index,可以自定义
public $defaultAction ='home'; public functionactionHome(){return $this->render('home');}
控制器的生命周期
beforeAction()
afterAction()
本站文章如未注明出处均为原创,转载请注明出处,如有侵权请邮件联系站长。
0/500
Share your thoughts respectfully.