action名にlistは使えない listを使う方法

CakePHPではアクション名にlistを使うことができません。
通常、listアクションを使用するとコントローラーに紐付くテーブル情報一覧が表示されます。

また次のようなエラーが発生します。

syntax error, unexpected 'array', expecting 'identifier'

Parse error: syntax error, unexpected T_LIST, expecting T_STRING in /app/controllers/test_controller.php on line xx

アクション名にlistが使えないということは、つまりは次のようなURLは使用できません。
http://www.example.com/item/list

このような場合には、beforeFilter()を使用して読み込むアクションを内部的に変更することで回避できます。

class TestController extends AppController {

  function beforeFilter(){
    parent::beforeFilter();
    if($this->action == "list"){
      $this->action = "_list";
    }
  }

// listアクションの代替
  function _list() {

    // ここに処理を書く

    $this->render('list');
  }
}

またはルーティングで変更することもできます。

/app/config/routes.php

Router::connect('/:controller/list', array('action' => 'lists'));

関連記事

スポンサーリンク

strip_tags修飾子 マークアップタグ(HTMLタグ)を取り除く

ホームページ製作・web系アプリ系の製作案件募集中です。

上に戻る