When strings are used as array keys there is no difference between the key used to define the
identifier of a route and that of a record. It may become quite troublesome when you need to search
for common keys such as id
, name
, or title
; for instance when you need to rename records
'id'
as 'article_id'
. Also, using strings as array keys may introduce errors such as
misspelling and use of deprecated/extraneous keys.
The following code is an example of strings used as array keys:
<?php
$route = [ 'id' => 'articles:edit', … ];
$record = Record::from([ 'id' => 123 ]);
For all these reasons I recommend the use of constants such as ROUTE_ID
and ARTICLE_ID
. Your
lovely IDE will help you avoid misspells, and will even warn you when a constant is marked as
deprecated. But instead of defining constants in some namespace, I prefer to define them in classes
or interfaces with a clear purpose. For instance, I'd use constants such as RouteDefinition::ID
and Article::ID
.
Consider the following route definition, using strings to define keys and some special values.
<?php
$definition = [
'pattern' => '/articles/<id:\d+>/edit',
'controller' => ArticleController::class,
'action' => 'edit',
'via' => 'GET',
'id' => 'articles:edit'
];
And now consider the following route definition, using constants:
<?php
use ICanBoogie\HTTP\Request;
use ICanBoogie\RouteDefinition;
$definition = [
RouteDefinition::PATTERN => '/articles/<id:\d+>/edit',
RouteDefinition::CONTROLLER => ArticleController::class,
RouteDefinition::ACTION => RouteDefinition::ACTION_EDIT,
RouteDefinition::VIA => Request::METHOD_GET,
RouteDefinition::ID => 'articles:edit'
];
That's a little more work, but I'm confident that these keys exists and now it would be child's play to search for route definition identifiers.
tl;dr
Define constants to use as array keys, and group them in classes/interfaces relevant to their purpose.