LoginSignup
7
3

More than 5 years have passed since last update.

#CakePHP 2.1 以降でパラメータにスラッシュ(/ or %2F)を含める

Posted at

次の場合に処理できるようにした時のメモ

  • %2Fをパラメータに含む (例: http://---domain---/pages/a%2Fb%2Fc)
  • /をパラメータに含む (例: http://---domain---/pages/a/b/c)
  • a/b/c という値を action で受け取る

Apacheの*.confと、CakePHPのルーティングを設定

CakePHP2.3.9を利用しています。

Apache

ApacheはデフォルトでURLに%2Fを含むと404を返すとのことでAllowEncodedSlashesを有効にする。

AllowEncodedSlashes On

→ これでURLに%2Fを含んでいても404を返さず、処理がCakePHPまで行われる。

参考: http://blog.flatlabs.net/20110307_230227/

CakePHP 2.1以降

/を含むパラメータは**で利用できる。

routes.php
Router::connect(
    '/pages/**',
    array(
        'controller' => 'pages',
        'action' => 'index',
    )
);
PagesController.php
public function index($param) {

}

http://---domain---/pages/a%2Fb%2Fc
http://---domain---/pages/a/b/c

a/b/c という一塊の値が pages コントローラ index アクション $param に渡ってくる。

※ルーティングを設定しないと pages コントローラの a アクションに bc という2つの値が渡ってくる。

参考: http://book.cakephp.org/2.0/ja/development/routing.html#connecting-routes

7
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
3