
掌握器是 MVC 形式中的一部分, 是继续yii\base\Controller类的对象,负责处理请乞降生成相应。具体来说,掌握器从运用主体接受掌握后会剖析要求数据并传送到模子, 传送模子效果到视图,末了生成输出相应信息。 (引荐进修:yii框架)
行动
掌握器由 操纵 构成,它是实行终端用户要求的最基本的单位, 一个掌握器可有一个或多个操纵。
以下示例显现包括两个行动view and create 的掌握器post:
namespace app\controllers; use Yii; use app\models\Post; use yii\web\Controller; use yii\web\NotFoundHttpException; class PostController extends Controller { public function actionView($id) { $model = Post::findOne($id); if ($model === null) { throw new NotFoundHttpException; } return $this->render('view', [ 'model' => $model, ]); } public function actionCreate() { $model = new Post; if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', [ 'model' => $model, ]); } } }
建立掌握器
在Web applications网页运用中,掌握器应继续yii\web\Controller 或它的子类。 同理在console applications掌握台运用中,掌握器继续yii\console\Controller 或它的子类。
以下代码定义一个 site 掌握器:
namespace app\controllers; use yii\web\Controller; class SiteController extends Controller { }
以上就是yii掌握器怎样定义的细致内容,更多请关注ki4网别的相干文章!