星期五, 7月 26, 2013

Phalcon 何時要起飛呢...

前陣子朋友推薦個超殺的PHP Framework - Phalcon
這圖表真是太吸引人了,有夠殺~


最重要的是整體framework 也滿不錯的
還有Dependency Injector~

  • Template Engine - Volt
    之前滿不喜歡PHP相關的template engine
    畢竟自己能做出來,還得花時間學個template的expressive 語法
    而且也把view搞的也不是很好看

    不過看Phalcon的 template engine - Volt還挺不錯的
    有時在View還是會有些邏輯判斷,把view的程式搞得滿亂的
    而Volt語法簡潔,簡化PHP在view的一些code,
    語法也挺直覺,不會造成額外的困難,超想拿來試試!!!
    而且速度快!!!
    Volt is an ultra-fast and designer friendly templating language written in C for PHP
    • 簡化php語法

      Robots

      <ul> {% for robot in robots %}
    • {{ robot.name|e }}
    • {% endfor %} </ul>
      可以看到減少滿多php code
      變數直接用、php tag也不用、物件.的寫法、及filter |e
      view的code變得更少,更少就更好維護~ nice
      |e是指escape Html的意思9
      另外還有|capitalize|trim|striptags (接在一起就是都做的意思)
      smarty也是相同的用法
    • Loop Controls
      {# skip the even robots #}
      {% for index, robot in robots %}
          {% if index is even %}
              {% continue %}
          {% endif %}
          ...
      {% endfor %}
      
    • Html tag helper
      {{ javascript_include("js/jquery.js") }}
      
      {{ form('products/save', 'method': 'post') }}
      
          
          {{ text_field("name", "size": 32) }}
      
          
          {{ select("type", productTypes, 'using': ['id', 'name']) }}
      
          {{ submit_button('Send') }}
      </form>
      
  • Assets Management
    即在php裡管理css, javascript等resource,寫方法如下
    <?php
    class IndexController extends Phalcon\Mvc\Controller
    {
        public function index()
        {
    
            //Add some local CSS resources
            $this->assets
                ->addCss('css/style.css')
                ->addCss('css/index.css');
    
            //and some local javascript resources
            $this->assets
                ->addJs('js/jquery.js')
                ->addJs('js/bootstrap.min.js');
    
        }
    }
    在view裡就下...
    <html>
        <head>
            <title>Some amazing website</title>
            <?php $this->assets->outputCss() ?>
        </head>
        <body>
    
            
    
            <?php $this->assets->outputJs() ?>
        </body>
    <html>
    本想說這東西有需要嗎 自己寫不會比較好嗎
    其他framework也看有過這東西
    不過就覺得怎麼可能phalcon會推這東西,再給他看下去...
    原來是為了做全併到同一個檔案,並做最小化...
    果然代誌不是憨人所想的這麼簡單...
    <?php
    
    $manager
    
        //These Javascripts are located in the page's bottom
        ->collection('jsFooter')
    
        //The name of the final output
        ->setTargetPath('final.js')
    
        //The script tag is generated with this URI
        ->setTargetUri('production/final.js')
    
        //This is a remote resource that does not need filtering
        ->addJs('code.jquery.com/jquery-1.10.0.min.js', true, false)
    
        //These are local resources that must be filtered
        ->addJs('common-functions.js')
        ->addJs('page-functions.js')
    
        //Join all the resources in a single file
        ->join(true)
    
        //Use the built-in Jsmin filter
        ->addFilter(new Phalcon\Assets\Filters\Jsmin())
    
        //Use a custom filter
        ->addFilter(new MyApp\Assets\Filters\LicenseStamper());
    


研究phalcon好一陣子...
最近應該是時機點推了...

但是!!!
看到這張圖...
我實在不知道怎麼跟主管推phalcon....
可能要說這隻非常有淺力,將來一定漲.... (都貼平了...)
Reference Performance benchmark of popular PHP frameworks