Philip Lafeber
Philip Lafeber

Reputation: 36

Cakephp 4 Naming conventions policies singular/plural

I have a few basic applications in Cake now. I am adding authorizations to define who can do that. I have installed the Authenticate and Autorize modules. Now I am setting up the "policies". I got some error messages and noticed a bit of a discrepancy. Let's say we are baking a model for cookies. I would have a table "cookies". I can then bake the code with the following command line;

cake bake all cookies

This does not bake the policy, so I would do this separately as well;

cake bake policy cookies

However, this does not work. If I tell the CookiesController that authorization has to be checked on the current model, it tries to find the "CookiePolicy". However, the bake command has createed the "CookiesPolicy". I would have to bake "policy cookie". That seems a little inconistent to me. Did I miss something? Any thoughts?

Upvotes: 0

Views: 215

Answers (1)

Andy Hoffner
Andy Hoffner

Reputation: 3327

It's not inconsistent pluralization - the two commands take different arguments.

The bake all command expects a database table name - by convention Tables should be plural.

The bake policy command policy can take either an Entity name, Table name, or generic object name -- but defaults to an Entity, per the help, it has a --type argument:

$ cake bake policy --help
Bake policy classes for various supported object types.

Usage:
cake bake policy [options] [<name>]

Options:
 ...
--type            The object type to bake a policy for. If only one
                  argument is used, type will be object.
                  (default: entity)
                  (choices: table|entity|object)
                  (required)
 ...

Your command is baking an Entity policy, those are singular. If you want to bake a Table policy, you'll have to specify the type manually and use the plural Table name, per conventions:

$ cake bake policy cookies --type=table

Creating file /var/www/html/src/Policy/CookiesTablePolicy.php
Wrote `/var/www/html/src/Policy/CookiesTablePolicy.php`

Upvotes: 2

Related Questions