Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion general/common_functions.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ <h2>remove_invisible_characters(<var>$str</var>)</h2>


<h2>html_escape(<var>$mixed</var>)</h2>
<p>This function provides short cut for htmlspecialchars() function. It accepts string and array. To prevent Cross Site Scripting (XSS), it is very useful.</p>
<p>此函數提供一個捷徑來呼叫 htmlspecialchars() 函數。它允許傳入字串或陣列。這對於避免跨網站腳本攻擊(XSS)很有用處。</p>

</div>
<!-- END CONTENT -->
Expand Down
88 changes: 32 additions & 56 deletions general/creating_libraries.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,19 @@ <h1>建立自己的程式庫</h1>

<p>當我們使用"程式庫(Libraries)"這個名稱的時候, 我們通常指的是位在 <kbd>libraries</kbd> 目錄底下的類別, 詳細可參考使用手冊中的類別參考的說明。

In this case, however, we will instead describe how you can create
your own libraries within your <dfn>application/libraries</dfn> directory in order to maintain separation between your local resources
and the global framework resources.</p>

<p>As an added bonus, CodeIgniter permits your libraries to <kbd>extend</kbd> native classes if you simply need to add some functionality
to an existing library. Or you can even replace native libraries just by placing identically named versions in your <dfn>application/libraries</dfn> folder.</p>
而在這邊,我們要教你的是如何在 <dfn>application/libraries</dfn> 建立你自己的程式庫。這是為了將你的程式庫與CodeIgniter內建的區分開來。</p>
<p>CodeIgniter允許你<kbd>繼承(extend)</kbd>原有的類別以擴充新功能。你也可以直接取代掉原本的類別,只要你使用與原有類別相同的名稱並放進 <dfn>application/libraries</dfn> 資料夾裡即可。</p>

<p>總結歸納:</p>

<ul>
<li>你可以建立(create)全新的程式庫。</li>
<li>你可以擴充(extend)原有的程式庫。</li>
<li>你可以繼承(extend)原有的程式庫。</li>
<li>你可以取代(replace)原有的程式庫。</li>
</ul>

<p>底下詳細解釋這三個概念。</p>

<p class="important"><strong>Note:</strong> The Database classes can not be extended or replaced with your own classes. All other classes are able to be replaced/extended.</p>

<p class="important"><strong>注意:</strong> 只有資料庫相關的類別無法被繼承,也無法用你自己的版本取代。其他的類別都可以。 </p>

<h2>存檔位置</h2>

Expand Down Expand Up @@ -145,98 +139,82 @@ <h2>於初使化自定類別時傳遞參數</h2>
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br /><br />
?&gt;</code>

<p class="important">You can also pass parameters stored in a config file. Simply create a config file named identically to the class <kbd>file name</kbd>
and store it in your <dfn>application/config/</dfn> folder. Note that if you dynamically pass parameters as described above,
the config file option will not be available.</p>






<p class="important">你也可以使用設定檔來存放設定值,只要在 <dfn>application/config/</dfn> 資料夾裡,建立一個與類別 <kbd>小寫檔案名稱</kbd> 相同的檔案即可。
但若是你使用上面所述的方式,由參數來傳入設定值,則設定檔就不會被使用。</p>

<h2>在您的程式裡面使用 CodeIgniter 資源</h2>


<p>To access CodeIgniter's native resources within your library use the <kbd>get_instance()</kbd> function.
This function returns the CodeIgniter super object.</p>

<p>Normally from within your controller functions you will call any of the available CodeIgniter functions using the <kbd>$this</kbd> construct:</p>
<p>使用函式 <kbd>get_instance()</kbd> 可以讓你在自己的程式庫中取得 CodeIgniter 的資源,這個函式將傳回 CodeIgniter 的 super object。</p>
<p>通常你可以在你的 controller 函式裡直接使用 <kbd>$this</kbd> 來呼叫 CodeIgniter 的函式:</p>

<code>
<strong>$this</strong>->load->helper('url');<br />
<strong>$this</strong>->load->library('session');<br />
<strong>$this</strong>->config->item('base_url');<br />
etc.
// 以此類推
</code>

<p><kbd>$this</kbd>, however, only works directly within your controllers, your models, or your views.
If you would like to use CodeIgniter's classes from within your own custom classes you can do so as follows:</p>


<p>First, assign the CodeIgniter object to a variable:</p>
<p>然而,這僅在 controllers, models 或是 views 能夠使用。若是你想在你自己製作的類別中使用 CodeIgniter 的類別,你可以這樣做:</p>
<p>首先,取得 CodeIgniter 物件並存放到變數中:</p>

<code>$CI =&amp; get_instance();</code>

<p>Once you've assigned the object to a variable, you'll use that variable <em>instead</em> of <kbd>$this</kbd>:</p>
<p>當你將物件放到變數中,你就可以使用這個變數來<em>取代</em> <kbd>$this</kbd></p>

<code>
$CI =&amp; get_instance();<br />
<br />
$CI->load->helper('url');<br />
$CI->load->library('session');<br />
$CI->config->item('base_url');<br />
etc.
// 以此類推
</code>

<p class="important"><strong>注意:</strong> You'll notice that the above get_instance() function is being passed by reference:
<p class="important"><strong>注意:</strong> 你會發現範例中 get_instance() 時使用了 &amp; 以取得物件的參考:
<br /><br />
<var>$CI =&amp; get_instance();</var>
<br />
<br />
<kbd>This is very important.</kbd> Assigning by reference allows you to use the original CodeIgniter object rather than creating a copy of it.</p>
<kbd>這非常非常重要!!</kbd> 取得物件的參考可以讓你使用同一個 CodeIgniter 物件,而不是複製一個副本。</p>

<h2>原生程式庫替換至個人版本(Replacing Native Libraries with Your Versions)</h2>

<p>Simply by naming your class files identically to a native library will cause CodeIgniter to use it instead of the native one. To use this
feature you must name the file and the class declaration exactly the same as the native library. For example, to replace the native <kbd>Email</kbd> library
you'll create a file named <dfn>application/libraries/Email.php</dfn>, and declare your class with:</p>
<h2>使用個人版本替換原生程式庫</h2>
<p>只要讓你的類別檔案使用與原生程式庫相同的檔案名稱,CodeIgniter 就會自動用它來代替原本的函式庫。
要這麼做,你必須讓檔案名稱與類別名稱都與要替換的原生程式庫完全相同才行。
例如,要取代掉原本的 <kbd>Email</kbd> 程式庫,你必須在 <dfn>application/libraries/Email.php</dfn> 建立一個檔案,並宣告你的類別為:</p>

<code>
class CI_Email {<br /><br />

}</code>

<p>Note that most native classes are prefixed with <kbd>CI_</kbd>.</p>

<p>To load your library you'll see the standard loading function:</p>
<p>注意,大多數的原生類別名稱都使用 <kbd>CI_</kbd> 做為前置字串。</p>
<p>這時只需要用標準的方式,就可以讀取你的版本:</p>

<code>$this->load->library('<kbd>email</kbd>');</code>

<p class="important"><strong>注意:</strong> At this time the Database 類別es can not be replaced with your own versions.</p>
<p class="important"><strong>注意:</strong> 目前還無法使用你自己的類別來替換掉資料庫的類別。</p>

<h2>繼承原生程式庫(Extending Native Libraries)</h2>

<h2>擴充原生程式庫(Extending Native Libraries)</h2>

<p>If all you need to do is add some functionality to an existing library - perhaps add a function or two - then
it's overkill to replace the entire library with your version. In this case it's better to simply extend the class.
Extending a class is nearly identical to replacing a class with a couple exceptions:</p>
<p>如果你想要做的只是增加一些(可能一或兩個)函式給原有的程式庫,那就沒有必要將整個類別替換掉。這個時候使用繼承來擴充類別是更容易的方法。
要繼承一個類別的作法,有點類似將其替換掉,但有以下的差別:</p>

<ul>
<li>The class declaration must extend the parent class.</li>
<li>Your new class name and filename must be prefixed with <kbd>MY_</kbd> (this item is configurable. See below.).</li>
<li>宣告類別時必須要繼承父類別。</li>
<li>你的新類別名稱與檔案名稱必須使用 <kbd>MY_</kbd> 做為前置字串。(這是可以設定的,讓我們繼續往下看。)</li>
</ul>

<p>For example, to extend the native <kbd>Email</kbd> class you'll create a file named <dfn>application/libraries/</dfn><kbd>MY_Email.php</kbd>, and declare your class with:</p>
<p>例如,繼承原生的 <kbd>Email</kbd> 類別,你必須建立一個檔案 <dfn>application/libraries/</dfn><kbd>MY_Email.php</kbd>,
並這樣宣告你的類別:</p>

<code>
class MY_Email extends CI_Email {<br /><br />

}</code>

<p>注意: If you need to use a constructor in your class make sure you extend the parent constructor:</p>

<p>注意: 如果你需要在你的類別中使用建構子,那你也必須執行父類別的建構子:</p>

<code>
class MY_Email extends CI_Email {<br />
Expand All @@ -250,13 +228,11 @@ <h2>擴充原生程式庫(Extending Native Libraries)</h2>

<h3>載入子類別</h3>

<p>To load your sub-class you'll use the standard syntax normally used. DO NOT include your prefix. For example,
to load the example above, which extends the Email 類別, you will use:</p>
<p>要讀取你的子類別,使用標準的語法載入即可。注意不要包含前置字串,例如你要讀取上面範例中 Email 的子類別,你可以這樣做:</p>

<code>$this->load->library('<kbd>email</kbd>');</code>

<p>Once loaded you will use the class variable as you normally would for the class you are extending. In the case of
the email class all calls will use:</p>
<p>載入成功後,你就可以像平常使用原生類別那樣使用這個子類別。在本例中,你可以這樣使用 email 類別:</p>


<code>$this-><kbd>email</kbd>->some_function();</code>
Expand Down
2 changes: 1 addition & 1 deletion general/libraries.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ <h1>使用 CodeIgniter 程式庫</h1>

<p>只要載入後,你就可以依照教學手冊的相關章節來使用它:</p>

<p>Additionally, multiple libraries can be loaded at the same time by passing an array of libraries to the load function.</p>
<p>你還可以一次載入多個程式庫,只要傳入一個包含多個程式庫的陣列即可,例如下面這樣:</p>

<code>$this->load->library(array('<var>email</var>', '<var>table</var>'));</code>

Expand Down
61 changes: 28 additions & 33 deletions libraries/output.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,61 +58,57 @@

<h1>Output 類別</h1>

<p>The Output class is a small class with one main function: To send the finalized web page to the requesting browser. It is
also responsible for <a href="../general/caching.html">caching</a> your web pages,if you use that feature.</p>
<p>Output 類別是一個小類別,其主要的功能是:將最終產生的網頁送交給客戶端瀏覽器。
若你使用了<a href="../general/caching.html">快取</a>的功能,它也負責快取你的網頁。</p>

<p class="important"><strong>注意:</strong> This class is initialized automatically by the system so there is no need to do it manually.</p>
<p class="important"><strong>注意:</strong>系統會自動初始這個類別, 所以不需要手動載入。</p>

<p>Under normal circumstances you won't even notice the Output class since it works transparently without your intervention.
For example,when you use the <a href="../libraries/loader.html">Loader</a> class to load a view file,it's automatically
passed to the Output class,which will be called automatically by CodeIgniter at the end of system execution.
It is possible,however,for you to manually intervene with the output if you need to,using either of the two following functions:</p>
<p>在一般的情況下你不會發現 Output 類別,因為它不需要你的介入便默默的在工作。
例如,當你使用 <a href="../libraries/loader.html">Loader</a> 類別去讀取一個 view 檔案,這個 view 檔案會被自動的傳給 Output 類別,
然後在系統執行的最後階段 CodeIgniter 會自動處理這些輸出步驟。當然,必要的話你也可以手動的去處理,只要使用下列兩個函式:</p>

<h2>$this->output->set_output();</h2>

<p>Permits you to manually set the final output string. 使用範例:</p>
<p>允許你手動設定最後輸出的字串。使用範例:</p>

<code>$this->output->set_output($data);</code>

<p><strong>非常重要:</strong> If you do set your output manually,it must be the last thing done in the function you call it from.
For example,if you build a page in one of your controller functions,don't set the output until the end.</p>
<p><strong>非常重要:</strong>如果你要自己操作輸出的功能,則必須放在最後一個步驟才做。
例如,如果你正在你的一個 controller 函式中建立頁面,你只能在函式最後結束前才能使用output。</p>

<h2>$this->output->set_content_type();</h2>

<p>Permits you to set the mime-type of your page so you can serve JSON data, JPEG's, XML, etc easily.</p>
<p>允許你設定頁面的 mime-type,如此一來你可以更輕鬆的提供 JSONJPEGXML 等等資料。</p>

<code>$this->output<br />
&nbsp;&nbsp;&nbsp;&nbsp;->set_content_type('application/json')<br />
&nbsp;&nbsp;&nbsp;&nbsp;->set_output(json_encode(array('foo' => 'bar')));<br />
<br />
$this->output<br />
&nbsp;&nbsp;&nbsp;&nbsp;->set_content_type('jpeg') // You could also use ".jpeg" which will have the full stop removed before looking in config/mimes.php<br/>
&nbsp;&nbsp;&nbsp;&nbsp;->set_content_type('jpeg') // 你也可以使用 ".jpeg","點號"會先被移掉才去查找 config/mimes.php<br/>
&nbsp;&nbsp;&nbsp;&nbsp;->set_output(file_get_contents('files/something.jpg'));</code>

<p><strong>Important:</strong> Make sure any non-mime string you pass to this method exists in config/mimes.php or it will have no effect.</p>

<p><strong>非常重要:</strong>確認你傳入的所有非mine的字串都有在 config/mimes.php 中設定好,否則不會運作。</p>


<h2>$this->output->get_output();</h2>

<p>Permits you to manually retrieve any output that has been sent for storage in the output class. 使用範例:</p>
<p>允許你手動取回 Output 類別所存放的輸出資料。使用範例:</p>
<code>$string = $this->output->get_output();</code>

<p>Note that data will only be retrievable from this function if it has been previously sent to the output class by one of the
CodeIgniter functions like <var>$this->load->view()</var>.</p>

<p>注意,只有在使用了像是 <var>$this->load->view()</var> 這樣的函式來將資料傳送給 Output 類別以後,你才能夠從 Output 類別取得輸出資料。</p>

<h2>$this->output->append_output();</h2>

<p>Appends data onto the output string. Usage example:</p>
<p>增加資料到輸出字串中。使用範例:</p>

<code>$this->output->append_output($data);</code>



<h2>$this->output->set_header();</h2>

<p>Permits you to manually set server headers,which the output class will send for you when outputting the final rendered display. 參考範例:</p>
<p>允許你手動設定 HTTP header,這將在 Output 類別準備好頁面要輸出時一併送出。參考範例:</p>

<code>
$this->output->set_header("HTTP/1.0 200 OK");<br />
Expand All @@ -125,37 +121,36 @@ <h2>$this->output->set_header();</h2>

<h2>$this->output->set_status_header();</h2>

<p>Permits you to manually set a server status header. 參考範例:</p>
<p>允許你手動設定 HTTP status參考範例:</p>

<code>$this->output->set_status_header('401');<br />
// Sets the header as: Unauthorized</code>
// 設定 header: Unauthorized</code>

<p><a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html">See here</a> for a full list of headers.</p>
<p>請參考:<a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html">完整的 header 列表</a></p>

<h2>$this->output->enable_profiler();</h2>

<p>Permits you to enable/disable the <a href="../general/profiling.html">Profiler</a>,which will display benchmark and other data
at the bottom of your pages for debugging and optimization purposes.</p>
<p>允許你啟用或停用 <a href="../general/profiling.html">Profiler</a>,這會在你的頁面底部顯示一些資料與數據,可以用來除錯或是最佳化。</p>

<p>To enable the profiler place the following function anywhere within your <a href="../general/controllers.html">Controller</a> functions:</p>
<p>若要啟用 Profiler,將下面的函式放在你的 <a href="../general/controllers.html">Controller</a> 當中任意地方。</p>
<code>$this->output->enable_profiler(TRUE);</code>

<p>When enabled a report will be generated and inserted at the bottom of your pages.</p>
<p>當啟用之後,在你的網頁底部將會插入一些報表。</p>

<p>To disable the profiler you will use:</p>
<p>若要停用 Profiler 你可以這樣做:</p>
<code>$this->output->enable_profiler(FALSE);</code>

<h2>$this->output->set_profiler_sections();</h2>

<p>Permits you to enable/disable specific sections of the Profiler when enabled. Please refer to the <a href="../general/profiling.html">Profiler</a> documentation for further information.</p>
<p>允許你啟用或停用 Profiler 特定的部份,請參考 <a href="../general/profiling.html">Profiler 的文件</a> 以取得更多資訊。</p>

<h2>$this->output->cache();</h2>
<p>The CodeIgniter output library also controls caching. For more information,please see the <a href="../general/caching.html">caching documentation</a>.</p>

<h2>Parsing Execution Variables</h2>
<p>CodeIgniter 輸出函式庫也可以控制快取,請參考<a href="../general/caching.html">caching 的文件</a>。</p>

<p>CodeIgniter will parse the pseudo-variables <var>{elapsed_time}</var> and <var>{memory_usage}</var> in your output by default. To disable this, set the <var>$parse_exec_vars</var> class property to <var>FALSE</var> in your controller.
<h2>解析變數</h2>

<p>CodeIgniter 預設會在輸出中解析這些虛擬變數:<var>{elapsed_time}</var> 及 <var>{memory_usage}</var>。
要停用這項功能,將 controller 的類別屬性<var>$parse_exec_vars</var> 設定為 <var>FALSE</var>。</p>
<code>$this->output->parse_exec_vars = FALSE;</code>

</div>
Expand Down
2 changes: 1 addition & 1 deletion libraries/pagination.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ <h4>$config['num_links'] = 2;</h4>
<p>放在您目前所在頁數前面跟後面所顯示的分頁數量。舉例來說,參數設定2,就會在前面跟後面兩邊多加兩個頁數,如同此頁最頂端的例子所顯示</p>

<h4>$config['use_page_numbers'] = TRUE;</h4>
<p>By default, the URI segment will use the starting index for the items you are paginating. If you prefer to show the the actual page number, set this to TRUE.</p>
<p>預設會在 URI 顯示你要分頁項目的索引編號,而不是頁數。如果你比較喜歡使用頁數,將這個值設定為 TRUE</p>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

官網文件沒有這段,請問是自己增加的嘛?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

對~我自己看完文章還不懂的部份, 會加上自己的實驗結果, 你看有沒有需要

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

這邊自己新增的部份,就麻煩您拿掉了,謝謝

<h4>$config['page_query_string'] = TRUE;</h4>
<p>在預設情況下,分頁類會假設您預設使用 <a href="../general/urls.html">URI Segments</a>,並且建構您的連結就如同底下</p>
Expand Down