Bundling Custom Components


Nov '14

Dec '14

1

951

0


Thien
31.3k
edited Dec '14
Nov '14

Here is a nice way to bundle your stuff up. I like it because it makes it easy to share methods between controllers and get rid of code duplication.I namespaced all my custom components so I don't get them confused with a Model name in the future.

  $di->setShared('component', function() {    
        $obj = new stdClass();     
        $obj->menu = new \Component\Menu(); 
        $obj->helper = new \Component\Helper();    
        $obj->user = new \Component\User();  
        return $obj;   
    });

Now you can access anywhere:

      $this->component->helper->method(123);   //or    {{ component.helper.method(123) }}

kenjis
163

Something wrong with code display. There are &#039and a too long line. Why don't you fix?