2019-01-29

Laravelのtorann/cellsの使い方メモ

完全に順番が前後しちゃってるけど。

呼び出し方

@cell(name, template, attributes, action)

内部の動作の抜粋

class Cells
{
  ..
  public function get($className,
                      $template = 'display',
                      $attributes = array() ,
                      $action = 'display' )
  {
    ..
    $instance = $this->app->make($className);
    $instance->setDisableCache($this->caching_disabled);

    array_set($cells, $className, $instance);
    ..
    $instance->setAttributes($attributes);
    $instance->initCell( $template , $action );

    return $instance->displayView();
  }
}
  • Cellxxx のインスタンスを作り
  • initCell() を呼ぶ
class CellBaseController
{
  public function initCell( $template = 'display' , $action = 'display' )
  {
    $this->template = $template;

    $this->init();

    $data = (array) $this->$action();
    $this->data = array_merge($this->attributes, $data);
  }

  public function displayView()
  {
    $path = "$this->name.$this->template";
    ..
    return $this->renderView( $path );
  }

  public function renderView( $path )
  {
    return View::make('cells' . DIRECTORY_SEPARATOR . $path,$this->data);
  }
}

initCell() の中では

  • init()
  • action() を呼んで、そこでセットされた結果を View に渡すデータとして保存
  • displayView() を呼ぶ

displayView() の中では

  • template の path をセットして
  • renderView() に渡して renderView() が View::make する

実装時の注意点

  • View で利用できる attributes に特定の値をセットするのは CellBaseController#setAttibute()
  • init() に渡ってくる前に @cell() を呼ぶ際に渡した attributes には getAttribute / getAttributes でアクセスできる
  • 個々の action は最終的に render の際に利用できる attributes に追加すべきデータの array を返す
    • action 固有のものはこの部分だけを見ることでテストできそう
  • 最終的に renderView() から View::make する際に template の存在チェックはしていないので、必ず対応する template を用意する1

コードで示すとこう

class CellFoo extends CellBaseController
{
  function init()
  {
    // 共通の前処理はここ
    // abstractなので実装しないといけない

    // View から利用できる attribute のセットは原則こう
    $this->setAttribute('key', 'value')
  }

  function action()
  {
    // attributes に追加あるいは attributes を上書きしたい値を返す
    return array('action_key' => 'action_value')

    // setAttribute してもよいが、自動で追加されるし、
    // return する方がテストしやすいはず
  }
}

Action に対応する View は絶対に必要。

views/cells/foo/action.blade.php
  1. どうしてもイヤなら CellBaseController を override しておいて挙動を変えちゃった方がよさそう 

About

例によって個人のなんちゃらです

Recent Posts

Categories

Tool 日々 Web Biz Net Apple MS ことば News Unix howto Food PHP Movie Edu Community Book Security Text TV Perl Ruby Music Pdoc 生き方 RDoc ViewCVS CVS Rsync Disk Mail FreeBSD Cygwin PDF Photo Zebedee Debian OSX Comic Cron Sysadmin Font Analog iCal Sunbird DNS Linux Wiki Emacs Thunderbird Sitecopy Terminal Drawing tDiary AppleScript Life Money Omni PukiWiki Xen XREA Zsh Screen CASL Firefox Fink zsh haXe Ecmascript PATH_INFO SQLite PEAR Lighttpd FastCGI Subversion au prototype.js jsUnit Apache Trac Template Java Rhino Mochikit Feed Bloglines CSS del.icio.us SBS qwikWeb gettext Ajax JSDoc Rails HTML CHM EPWING NDTP EB IE CLI ck ThinkPad Toy WSH RFC readline rlwrap ImageMagick epeg Frenzy sysprep Ubuntu MeCab DTP ERD DBMS eclipse Eclipse Awk RD Diigo XAMPP RubyGems PHPDoc iCab DOM YAML Camino Geekmonkey w3m Scheme Gauche Lisp JSAN Google VMware DSL SLAX Safari Markdown Textile IRC Jabber Fastladder MacPorts LLSpirit CPAN Mozilla Twitter OpenFL Rswatch ITS NTP GUI Pragger Yapra XML Mobile Git Study JSON VirtualBox Samba Pear Growl Mercurial Rack Capistrano Rake Win RSS Mechanize Sitemaps Android JavaScript Python RTM OOo iPod Yahoo Unicode Github iTunes God SBM friendfeed Friendfeed HokuUn Sinatra TDD Test Project Evernote iPad Geohash Location Map Search Simplenote Image WebKit RSpec Phone CSV WiMAX USB Chrome RubyKaigi RubyKaigi2011 Space CoffeeScript Nokogiri Hpricot Rubygems jQuery Node GTD CI UX Design VCS Kanazawa.rb Kindle Amazon Agile Vagrant Chef Windows Composer Dotenv PaaS Itamae SaaS Docker Swagger Grape WebAPI Microservices OmniAuth HTTP 分析基盤 CDN Terraform IaaS HCL Webpack Vue.js BigQuery Middleman CMS AWS PNG Laravel Selenium OAuth OpenAPI GitHub UML GCP TypeScript SQL Hanami Document SVG AsciiDoc Pandoc DocBook Develop Jekyll macOS Node.js Vite Heroku Transformer AI Data Cloud Wasm