0
0

More than 1 year has passed since last update.

CakePHP4.3 Deprecated ルーティング内のプレースホルダーの記載方法の変更

Posted at

CakePHP4.2 以前から CakePHP4.3 にあげるとこんな警告が表示される。

Deprecated Error: Colon prefixed route placeholders like `:foo` are deprecated. Use braced placeholders like `{foo}` instead.
/var/www/html/api/vendor/cakephp/cakephp/src/Routing/Route/Route.php, line: 301
You can disable all deprecation warnings by setting `Error.errorLevel` to `E_ALL & ~E_USER_DEPRECATED`. Adding `vendor/cakephp/cakephp/src/Routing/Route/Route.php` to `Error.ignoredDeprecationPaths` in your `config/app.php` config will mute deprecations from that file only.
In [/var/www/html/api/vendor/cakephp/cakephp/src/Core/functions.php, line 321]

基本的にはCakePHPのバージョンを上げる際には公式ドキュメントの「移行ガイド」とにらめっこしながら進めていく。

まぁメッセージどおりなのですが、

Colon prefixed route placeholders like `:foo` are deprecated. Use braced placeholders like `{foo}` instead.
適当和訳
`:foo` のようなコロンプレフィックスでのプレースホルダーは非推奨になりました。`{foo}` のようなカッコでプレースホルダーを定義してください。

実際に移行ガイドにも同様の記載があります。

Routing
:controller のようなコロンの付いたルートプレースホルダーは非推奨です。 代わりに {controller} のような波括弧付きのプレースホルダーを使用してください。

リンク: https://book.cakephp.org/4/ja/appendices/4-3-migration-guide.html#routing

実際に書く場合はこのように置き換えます。

CakePHP4.2以前

$builder->connect('/:id', ['controller' => 'Articles', 'action' => 'view'])
    ->setMethods(['GET'])
    ->setPatterns(['id' => '\d+'])
    ->setPass(['id']);
CakePHP4.3以降

$builder->connect('/{id}', ['controller' => 'Articles', 'action' => 'view'])
    ->setMethods(['GET'])
    ->setPatterns(['id' => '\d+'])
    ->setPass(['id']);
0
0
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
0
0