ユーザ用ツール

サイト用ツール


alcgi:汎用メソッド

汎用メソッド

コントローラから汎用的に使えるメソッドを紹介します。

Alone.add_http_header( header )

httpヘッダの追加

ユーザーコードで出力が行われる前に呼ばれる必要がある。

Alone.add_http_header("Content-Type: text/plain; charset=UTF-8")    # PlainTextで出力する
Alone.add_http_header("Status: 404 Not Found")                      # エラー404を通知する

Alone.delete_http_header( header )

httpヘッダの削除

ユーザーコードで出力が行われる前に呼ばれる必要がある。

Alone.delete_http_header("Cache-Control:")

Alone.redirect_to( uri )

リダイレクト (ステータスコード 302) 指示

ユーザーコードで出力が行われる前に呼ばれる必要がある。

Alone.redirect_to( make_uri(:action=>"XXX") )

Alone.encode_uri_component( s )

URIエンコード

引数で与えた文字列を、パーセントエンコードして返す。

Alone.encode_uri_component("abc <あい>+123")    # => "abc%20%3c%e3%81%82%e3%81%84%3e%2b123"

Alone.decode_uri_component( s )

URIデコード

パーセントエンコードされた文字列を、デコードして返す。

Alone.decode_uri_component("abc%20%3c%e3%81%82%e3%81%84%3e%2b123") # => "abc <あい>+123"

Alone.request_uri()

リクエストされたURIを返す

Alone.request_uri()    # => "http://localhost:10080/index.rb?ctrl=hello"

Alone.make_uri( arg )

リンク用のURIを生成する

Alone.make_uri( :ctrl=>"ctrl1", :action=>"act1", :any1=>"Any1" )    # => "/index.rb?ctrl=ctrl1&action=act1&any1=Any1"

テンプレート中で使用する例

<a href="<%=h Alone.make_uri() %>">

Alone.escape_html( s )

html特殊文字のエスケープ

Alone.escape_html("<tag>")      # => "&lt;tag&gt;"

Alone.escape_html_br( s )

html特殊文字のエスケープに加え、改行文字を<br>タグへ変更する。

Alone.escape_html_br("<tag>\nTEXT\n")    # => "&lt;tag&gt;<br>TEXT<br>"

Alone.escape_backslash( s )

クォート文字をバックスラッシュでエスケープする

対象は、シングルクォート、ダブルクォート、バックスラッシュ、NULL

Alone.escape_backslash(%!ab'C'd"E"fg!)    # => "ab\\'C\\'d\\\"E\\\"fg"
alcgi/汎用メソッド.txt · 最終更新: 2019/02/18 11:11 by hirohito