LoginSignup
5
6

More than 5 years have passed since last update.

cakephpの2系でデフォルトでfetch出来るmetaとかtitleとかscriptはどこで定義されているのか

Last updated at Posted at 2015-02-14

調べました。

きっかけ

cakephp2の勉強やってて、default.ctpに書かれている$this->fetchメソッドがよく分からず調べていたものの、assignで書き換えられるよ!って話しか無くて、どこにあるんだ!って思ってげきおこになったので調べた。

使用したCakePHPのバージョン

2.6.1です。

記述されているファイル

該当の記述があるのは、lib\Cake\View\View.phpです。

このファイルの500行目くらいにあるrenderLayoutメソッドです。

public function renderLayout($content, $layout = null) {
    $layoutFileName = $this->_getLayoutFileName($layout);
    if (empty($layoutFileName)) {
        return $this->Blocks->get('content');
    }

    if (empty($content)) {
        $content = $this->Blocks->get('content');
    } else {
        $this->Blocks->set('content', $content);
    }
    $this->getEventManager()->dispatch(new CakeEvent('View.beforeLayout', $this, array($layoutFileName)));

    $scripts = implode("\n\t", $this->_scripts);
    $scripts .= $this->Blocks->get('meta') . $this->Blocks->get('css') . $this->Blocks->get('script');

    $this->viewVars = array_merge($this->viewVars, array(
        'content_for_layout' => $content,
        'scripts_for_layout' => $scripts,
    ));

    $title = $this->Blocks->get('title');
    if ($title === '') {
        if (isset($this->viewVars['title_for_layout'])) {
            $title = $this->viewVars['title_for_layout'];
        } else {
            $title = Inflector::humanize($this->viewPath);
        }
    }
    $this->viewVars['title_for_layout'] = $title;
    $this->Blocks->set('title', $title);

    $this->_currentType = self::TYPE_LAYOUT;
    $this->Blocks->set('content', $this->_render($layoutFileName));

    $this->getEventManager()->dispatch(new CakeEvent('View.afterLayout', $this, array($layoutFileName)));
    return $this->Blocks->get('content');
}

上記にある、$this->Blocks->setというメソッドがassignの役割を果たしています。
そもそもfetchとかassignはViewBlockの機能なのです。

このように定義されていることが分かったので良かったです。

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6