Skip to content

Commit

Permalink
2023.03.05 modify readme
Browse files Browse the repository at this point in the history
  • Loading branch information
monkenWu committed Mar 5, 2023
1 parent 3acc862 commit 2fcee71
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 59 deletions.
76 changes: 44 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,58 +64,72 @@ Please be noticed, while constructing response for the users during developing,

### Use return to stop controller logic

Inside the Controller, try using return to stop the controller logic. No matter the response of view or API, reduce the `echo` output usage can avoid lets of errors, just like ths:W
Inside the Controller, try using return to stop the controller logic. No matter the response of view or API, reduce the `echo` output usage can avoid lets of errors, just like this:

```php
<?php namespace App\Controllers;
namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;

class Home extends BaseController
{
use ResponseTrait;

public function index()
{
// Don't use:
// echo view('welcome_message');
return view('welcome_message');
}

/**
* send header
*/
public function sendHeader()
{
$this->response->setHeader("X-Set-Auth-Token", uniqid());
return $this->respond(["status"=>true]);
}
use ResponseTrait;

public function index()
{
// Don't use:
// echo view('welcome_message');
return view('welcome_message');
}

/**
* send header
*/
public function sendHeader()
{
$this->response->setHeader("X-Set-Auth-Token", uniqid());
return $this->respond(["status" => true]);
}
}
```

### Use the built-in Session library

We only focus on supporting the Codeigniter4 built-in [Session library](https://codeigniter.com/user_guide/libraries/sessions.html), and do not guarantee if using `session_start()` and `$_SEEEION` can work as normal. So, you should avoid using the PHP built-in Session method, change to the Codeigniter4 framework built-in library.

### Developing and debugging in a environment with only one Worker
### External Connections

Since the RoadRunner, OpenSwoole and Workerman has fundamentally difference with other server software(i.e. Nginx, Apache), every Codeigniter4 will persist inside RAMs as the form of Worker, HTTP requests will reuse these Workers to process. Hence, we have better develop and test stability under the circumstance with only one Worker to prove it can also work properly under serveral Workers in the formal environment.
We only focus on supporting the Codeigniter4 built-in [Database Library](https://codeigniter.com/user_guide/database/index.html) and [Cache Library](https://codeigniter.com/user_guide/libraries/caching.html), hence we do not guarantee if using the PHP
built-in method should work as normal. Therefore, you should avoid using the PHP built-in method but
pick the Codeigniter4 framework built-in library.

### Database Connection
By default, Worker's DB and Cache should be persistent and try to reconnect once the connection fails.
Every request into the Worker is using the same connection instance. If you don't want this default setting but want every request to use the reconnected connection instance.
You can adjust these settings in `Config/Burner.php`.

We only focus on supporting the Codeigniter4 built-in [Database Library](https://codeigniter.com/user_guide/database/index.html), hence we do not guarantee if using the PHP
built-in method should work as normal. Therefore, you should avoid using the PHP built-in database connection method but
pick the Codeigniter4 framework built-in library.
```php
public $dbAutoClose = true;
public $cacheAutoClose = true;
```

Under the default situation, DB of the Worker should be lasting, and will try to reconnect once the connection is failed.
Every Request that goes into Worker is using a same DB connection instance. If you don't want this default setting but expecting
every Request to use the reconnect DB connection instance. You can add the configuration down below into the `.env` file under the root directory.
### CodeIgniter Services

```env
CIROAD_DB_AUTOCLOSE = true
CodeIgniter4 allows you to write any class to be managed as a Services Class, and your class will remain as a single instance in the Service Class.

After the HTTP response, Burner automatically initializes all instances in the service in case the already used singleton affects the next request. If your service does not need to be initialized, then declaring the service name in the `skipInitServices` string array in `Config/Burner.php` will make the service persistent and reusable in the Worker.

```php
public $skipInitServices = [
'your',
'service',
'name'
];
```

### Developing and debugging in a environment with only one Worker

Since the RoadRunner, OpenSwoole and Workerman has fundamentally difference with other server software(i.e. Nginx, Apache), every Codeigniter4 will persist inside RAMs as the form of Worker, HTTP requests will reuse these Workers to process. Hence, we have better develop and test stability under the circumstance with only one Worker to prove it can also work properly under serveral Workers in the formal environment.

# Global Methods

We offer some Global methods to help you develop your projects more smoothly.
Expand All @@ -127,8 +141,6 @@ Since the RoadRunner and Workerman Worker can not transfer the correct `$_FILES`
You can fetch the uploaded files by means of `SDPMlab\Ci4Roadrunner\UploadedFileBridge::getPsr7UploadedFiles()` in the controller (or any other places). This method will return an array, consist of Uploaded File objects. The available methods of this object is identical as the regulation of [PSR-7 Uploaded File Interface](https://www.php-fig.org/psr/psr-7/#36-psrhttpmessageuploadedfileinterface).

```php
<?php

namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;
Expand Down
67 changes: 40 additions & 27 deletions README_zh-TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,51 +66,66 @@ Codeigniter4 並沒有實作完整的 [HTTP message 介面](https://www.php-fig.
在 Controller 中,盡量使用 return 結束程式邏輯,不論是視圖的響應或是 API 響應,減少使用 `echo` 輸出內容可以避免很多錯誤,就像這個樣子。

```php
<?php namespace App\Controllers;
namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;

class Home extends BaseController
{
use ResponseTrait;

public function index()
{
//Don't use :
//echo view('welcome_message');
return view('welcome_message');
}

/**
* send header
*/
public function sendHeader()
{
$this->response->setHeader("X-Set-Auth-Token",uniqid());
return $this->respond(["status"=>true]);
}
use ResponseTrait;

public function index()
{
// Don't use:
// echo view('welcome_message');
return view('welcome_message');
}

/**
* send header
*/
public function sendHeader()
{
$this->response->setHeader("X-Set-Auth-Token", uniqid());
return $this->respond(["status" => true]);
}
}
```

### 使用內建 Session 程式庫

我們只針對 Codeigniter4 內建 [Session 程式庫](https://codeigniter.tw/user_guide/libraries/sessions.html) 進行支援,並不保證使用 `session_start()``$_SESSION` 是否能照常運作。所以,你應該避免使用 PHP 內建的 Session 方法,而是以 Codeigniter4 框架內建的程式庫為主。

### 在只有一個 Worker 的環境中開發與除錯

因為 RoadRunner、OpenSwoole 與 Workerman 與其他伺服器軟體(Nginx、Apache)有著根本上的不同,每個 Codeigniter4 將會以 Worker 的形式持久化於記憶體中,HTTP 的請求會重複利用到這些 Worker 進行處裡。所以,我們最好在只有單個 Worker 的情況下開發軟體並測試是否穩定,以證明在多個 Woker 的實際環境中能正常運作。
### 外部連線

### 資料庫連線
我們只針對 Codeigniter4 內建的 [Database 程式庫](https://codeigniter.tw/user_guide/database/index.html)[快取程式庫](https://codeigniter.tw/user_guide/libraries/caching.html) 進行支援,並不保證 PHP 內建的方法是否能照常運作。所以,你應該避免使用內建的 PHP 方法,而是以 Codeigniter4 框架內建的程式庫為主。

我們只針對 Codeigniter4 內建 [Database 程式庫](https://codeigniter.tw/user_guide/database/index.html) 進行支援,並不保證 PHP 內建的方法是否能照常運作。所以,你應該避免使用內建的 PHP 資料庫連線方法,而是以 Codeigniter4 框架內建的程式庫為主。
預設的情況下,在 Worker 中的連線是持久的,並會在連線失效時自動重新連線。所有進入 Worker 的 Request 都使用同一個連線實體。如果你不想要這個預設設定,希望每個進入 Worker 的 Request 都使用重新連線的 DB 或 Cache 連線實體。你調整 `Config/Burner.php` 中的下列設定:

預設的情況下,在 Worker 中的 DB 連線是持久的,並會在連線失效時自動重新連線。所有進入 Worker 的 Request 都使用同一個 DB 連線實體。如果你不想要這個預設設定,希望每個進入 Worker 的 Request 都使用重新連線的 DB 連線實體。你可以在專案根目錄下的 `.env` 檔案加入以下設定。
```php
public $dbAutoClose = true;
public $cacheAutoClose = true;
```

### CodeIgniter Services

```env
CIROAD_DB_AUTOCLOSE = true
CodeIgniter4 允許你使用 Services 來管理你所撰寫的類別,你的類別將以單例的方式留存在 Services 類別中。

在 HTTP 響應後,Burner 會自動初始化 Services 類別中的所有實體,防止已經使用過的單例實體影響到下一個請求。如果你所撰寫的 Service 不需要被初始化,那麼請在 `Config/Burner.php` 中的 `skipInitServices` 字串陣列宣告這些 Service 的名字。這將使你所宣告的 Service 在 Worker 中持久化,並被所有請求重複使用。

```php
public $skipInitServices = [
'your',
'service',
'name'
];
```

### 在只有一個 Worker 的環境中開發與除錯

因為 RoadRunner、OpenSwoole 與 Workerman 與其他伺服器軟體(Nginx、Apache)有著根本上的不同,每個 Codeigniter4 將會以 Worker 的形式持久化於記憶體中,HTTP 的請求會重複利用到這些 Worker 進行處裡。所以,我們最好在只有單個 Worker 的情況下開發軟體並測試是否穩定,以證明在多個 Woker 的實際環境中能正常運作。

## 全域方法

我們提供了一些全域方法,幫助你更加順利地開發你的專案。
Expand All @@ -122,8 +137,6 @@ CIROAD_DB_AUTOCLOSE = true
你可以在控制器(或任何地方),以 `SDPMlab\Ci4Roadrunner\UploadedFileBridge::getPsr7UploadedFiles()` 取得使用者上傳的檔案。這個方法將回傳以 Uploaded File 物件組成的陣列。此物件可用的方法與 [PSR-7 Uploaded File Interface](https://www.php-fig.org/psr/psr-7/#36-psrhttpmessageuploadedfileinterface) 中規範的一樣。

```php
<?php

namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;
Expand Down

0 comments on commit 2fcee71

Please sign in to comment.