6

コントローラーとアクションごとに(頭の中で)異なるタイトルを付けたいです。コントローラーからこれを行う方法は?

4

5 に答える 5

11

あなたのコントローラー

    class SiteController {
        public function actionIndex() {
            $this->pageTitle = 'Home page';
            //...
        }

        //..
    } 

レイアウトファイル

    <title><?php echo $this->pageTitle; ?></title>

html に参照を追加するのを忘れていませんか?

于 2012-04-30T08:32:03.370 に答える
8

アクションごとに異なるタイトルを付けたい場合

CController.pageTitle次に、アクション内の値を設定するだけです。

class MyController extends CController {
    public function actionIndex() {
        $this->pageTitle = "my title";
        // other code here
    }
}

複数のアクション間で特定のタイトルを共有したい場合

1 つの方法は、おそらくページ タイトルとしてクラス定数を使用して、上記のアプローチに従うことです。

class MyController extends CController {
    const SHARED_TITLE = "my title";

    public function actionIndex() {
        $this->pageTitle = self::SHARED_TITLE;
        // other code here
    }

    public function actionFoo() {
        $this->pageTitle = self::SHARED_TITLE;
        // other code here
    }
}

ただし、これには、「タイトル共有」スキームに含めたり除外したりするたびに、各アクションに個別にアクセスする必要があります. この欠点がない解決策は、フィルターを使用することです。例えば:

class MyController extends CController {
    public function filters() {
        // set the title when running methods index and foo
        return array('setPageTitle + index, foo');

        // alternatively: set the title when running any method except foo
        return array('setPageTitle - foo');
    }

    public function filterSetPageTitle($filterChain) {
        $filterChain->controller->pageTitle = "my title";
        $filterChain->run();
    }

    public function actionIndex() {
        // $this->pageTitle is now set automatically!
    }

    public function actionFoo() {
        // $this->pageTitle is now set automatically!
    }
}

すべてのアクションで同じタイトルにしたい場合

これは明らかですが、完全を期すために言及します。

class MyController extends CController {
    public $pageTitle = "my title";

    public function actionIndex() {
        // $this->pageTitle is already set
    }

    public function actionFoo() {
        // $this->pageTitle is already set
    }
}
于 2012-04-30T14:34:46.393 に答える
1

関数 init または before action または run which 呼び出しを実際のアクション呼び出しの前に使用できます。したがって、その関数では、コントローラーのパブリック pageTitle 変数を設定できます。

次のように使用します。

public function init()
{
  parent::init();
  $this->pageTitle = "My Page Title";
}
于 2012-04-30T05:03:37.627 に答える
0

VIEW ページ (index.php、view.php、create.php など)

$this->setPageTitle('custom page title');
于 2014-04-09T08:52:29.690 に答える
0

ヨンは次のように与えることができます:-

$this->set("title", "Enrollment page");

別の名前またはタイトルを指定して、この $title を ur ctp ファイルで使用します。

これを試して..

于 2012-04-30T05:32:21.160 に答える