>  기사  >  PHP 프레임워크  >  yii 컨트롤러를 정의하는 방법

yii 컨트롤러를 정의하는 방법

(*-*)浩
(*-*)浩원래의
2019-12-04 13:33:242580검색

yii 컨트롤러를 정의하는 방법

Controller은 MVC 패턴의 일부이며 yiibaseController 클래스를 상속하고 요청 처리 및 응답 생성을 담당하는 개체입니다. . 구체적으로, 컨트롤러는 Application Body로부터 제어권을 넘겨받은 후 요청 데이터를 분석하여 모델에 전달하고, 모델 결과를 뷰에 전달하여 최종적으로 출력 응답 정보를 생성합니다. (추천 학습: yii Framework)

action

컨트롤러는 연산으로 구성됩니다. 실행 터미널 사용자가 요청하는 가장 기본적인 단위로, 컨트롤러는 하나 이상의 작업을 가질 수 있습니다.

다음 예에서는 두 가지 작업 보기 및 만들기가 포함된 컨트롤러 게시물을 보여줍니다.

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,
            ]);
        }
    }
}

컨트롤러 만들기#🎜🎜 ## 🎜🎜#

웹 애플리케이션에서 컨트롤러는 yiiwebController 또는 해당 하위 클래스를 상속해야 합니다. 마찬가지로 콘솔 애플리케이션에서 컨트롤러는 yiiconsoleController 또는 해당 하위 클래스를 상속합니다.

다음 코드는 사이트 컨트롤러를 정의합니다:

namespace app\controllers;
use yii\web\Controller;
class SiteController extends Controller
{
}

위 내용은 yii 컨트롤러를 정의하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.