yii 2 相比yii 1.1.x 有什麼變化?

來源:互聯網
上載者:User
yii 2 相比yii 1.1.x ,有什麼優點和變化 ?選擇時應該注意什嗎?

回複內容:

更新:重新寫了一個,改正了一些書寫錯誤,並進行排版,更新的版本在
《深入理解Yii2.0》附錄1:Yii2.0 對比 Yii1.1 的重大改進
---------------------------
我自己翻的官方升級文檔,供參考:

In this chapter, we list the major changes introduced in Yii 2.0 since version 1.1. We hope this list will make it easier for you to upgrade from Yii 1.1 and quickly master Yii 2.0 based on your existing Yii knowledge.


本章將列出自1.1版本以來Yii 2.0的主要變化。


命名空間(Namespace)

The most obvious change in Yii 2.0 is the use of namespaces. Almost every core class is namespaced, e.g., yii\web\Request. The “C” prefix is no longer used in class names. The naming of the namespaces follows the directory structure. For example, yii\web\Request indicates the corresponding class file is web/Request.php under the Yii framework folder. You can use any core class without explicitly including that class file, thanks to the Yii class loader.


Yii 2.0最明顯的改變是對命名空間的使用。幾乎所有的核心類都使用了命名空間,比如yii\web\Request。同時,類名前不再使用“C”首碼。命名空間的命名遵循目錄結構,如yii\web\Request代表的相應類檔案是位於Yii 架構的目錄下的 web/Request.php。由於Yii的類裝載機制,可以在未顯示包含類檔案的情況下使用任意的核心類。


組件(Component)和對象(Object)

Yii 2.0 breaks the CComponent class in 1.1 into two classes: [[yii\base\Object]] and [[yii\base\Component]]. The [[yii\base\Object|Object]] class is a lightweight base class that allows defining class properties via getters and setters. The [[yii\base\Component|Component]] class extends from [[yii\base\Object|Object]] and supports the event feature and the behavior feature.


Yii 2.0將1.1版本中的CComponent類拆分成兩個類:[[yii\base\Object]] 和 [[yii\base\Component]]。其中,[[yii\base\Object|Object]]類是一個輕量級的基類,它通過getter和setter提供了定義類屬性(property)的方法。[[yii\base\Component|Component]]繼承自[[yii\base\Object|Object]],並提供對事件(event)和行為(behavior)的支援。


If your class does not need the event or behavior feature, you should consider using Object as the base class. This is usually the case for classes that represent basic data structures.


如果自訂類不需要事件或行為特性,可考慮使用Object 作為基類。這通常用於表示基礎的資料結構。


對象配置Object Configuration

The [[yii\base\Object|Object]] class introduces a uniform way of configuring objects. Any descendant class of [[yii\base\Object|Object]] should declare its constructor (if needed) in the following way so that it can be properly configured:


[[yii\base\Object|Object]]對設定物件引入了一個規範的方法。其後代類可以在需要的情況下通過如下的方式聲明一個建構函式,那麼該類就可以被正確的配置:

class MyClass extends \yii\base\Object{    function __construct($param1, $param2, $config = [])    {        // ... initialization before configuration is applied        parent::__construct($config);    }    public function init ()    {        parent::init();        // ... initialization after configuration is applied    }}
門外漢開個頭(用過一小段時間1以及看2發布了看了點核碼)

變化及優點:
大致思路不會變,開發流程變化也不是很大。
有變化的是
1、yii2帶入的PHP5.4的特性,引入了namespace解決命名衝突,因此基類不會再C字開頭了
2、不再所有類都繼承自組件Component,而是選擇繼承object和component(也繼承自object,但帶有事件功能、以及用於擴充的Behavior功能)
3、更加的MVC,原先的view層其實基本算是controller直接include進來的,現在有了view的類用來控制,因此View層在使用Controller帶來的參數的方式稍有不同。
4、yii2分基本版(看上去和1差不多)和進階版(分前台和後台),進階版更加適合開發大型項目。
5、其他的都是細節方面的變化,比如塊賦值(基本用於收集表單資料)更加方便了。安裝完畢介面就內建bootstrap風格了,等等等等。
當然還有許多其他的特性,可以直接參看yii在git上更新的文檔:
yii2/docs/guide/upgrade-from-v1.md at master 路 yiisoft/yii2 路 GitHub

--update--
1、加入了Dependency Injection(依賴注入,理論可以看經典翻譯文IoC容器和依賴注入模式(轉)-redcoffee-ChinaUnix部落格 ,yii的具體實現看/vendor/yiisoft/yii2/di下的container和instance就行),以後建立對象時基本都可以用容器的get方法替代new了,很不錯很贊喲~

選擇:
如果你急於馬上開啟一個項目的話 用1.1.X,因為2的正式版估計還有好多個月吧。
如果你是帶著學習研究的態度的,直接從2的beta版開始看吧,和正式版不會變化很大。

純門外漢瞎搭,大神路過發現有不對之處隨意拍磚:P推薦一個部落格地址,關於教程的:http://www.fancyecommerce.com/
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.