Mostly just reading through the framework code itself. An
IDE or
text editor with
good language support helps with navigating the codebase and piecing things together.
If it helps, the template
link()
function arguments correspond to the route callback arguments like so:
Code:
link('prefix/action', $data, $params)
The
prefix
is the route prefix, the
action
is the desired controller action,
$data
is an array (or an object implementing
\ArrayAccess
, like an entity) from which to automatically derive the route format parameters (meaning
:str<title>
will pull from
$data['title']
, and so on), and
$params
is an array of key-value pairs to add to the query string (
?key=value&key2=value2
).
Code:
link('base', {'type': 'HARDCODED TYPE', 'item': $r.name})
In the above call, your
$prefix
is
'base'
, your
$action
is
''
(blank), your
$data
is
['type' => 'HARDCODED TYPE', 'item' => $r->name]
, and your
$params
is
[]
(blank).
Your callback is just a static method which is called when the route is being built (when the
link()
function is called). It also receives the
$router
object as an argument if you want to call (public) methods on the router.
In your callback, you have a few options. You can return a string or a
\XF\Mvc\RouteBuiltLink
object, but I would personally reserve that for more advanced use-cases. Your other option is to simply manipulate the arguments as needed (they are passed by reference) and return
null
. The router will proceed with building the link as if the manipulated arguments were passed from the
link()
function directly. In other words, the callback simply sits between the
link()
function and the router, modifying the arguments as needed.
The example in my post above normalizes the
$data['item']
string, which corresponds to the
:str<item>
format parameter.
Though, all of this may be moot if you're always building the
$data
array manually. You could probably just do:
Code:
link('base', {'type': 'HARDCODED TYPE', 'item': $r.name|to_lower})