Multiple URI patterns under one rule?
Closed this issue · 2 comments
siebird commented
Hey Rob,
Is it possible to have multiple URI patterns under one rule by piping? I have a site in development that checks an entry to see if that members site is turned on/off. Then, routes the correct template. The first pattern matches, however the second pattern throws an error.
'fa/:any/:any|fa/:any' => function($router, $wildcard_1, $wildcard_2) {
$agent = ee()->db->select('entry_id')
->where('channel_id', 1)
->where('url_title', $wildcard_1->value)
->get('channel_titles');
$entry_id = $agent->row('entry_id');
$router->setGlobal('gv_agent_id', $entry_id);
$agentSite = ee()->db->select('field_id_4')
->where('channel_id', 1)
->where('entry_id', $entry_id)
->get('channel_data');
$isAgentSite = $agentSite->row('field_id_4');
// is the current agent's homepage on?
if (empty($wildcard_2) && $isAgentSite == 'y') {
$router->setTemplate('agent-site/index');
// is the current agent's interior pages on?
} elseif (isset($wildcard_2) && $isAgentSite == 'y') {
$router->setTemplate('agent-site/_page');
} else {
$router->setTemplate('agent-site/_404');
}
}
rsanchez commented
Try this:
'fa/:any/:all' => function($router, $wildcard1, $wildcard2 = null) {
...
if ($wildcard2) {
...
siebird commented
Rob, thanks for the direction–it's working properly now. This is what I ended up with:
'fa/:any/:all' => function($router, $wildcard_1, $wildcard_2 = null) {
$where = array (
'channel' => 'zoo_visitor'
);
// Validate a member exists
if ($wildcard_1->isValidUrlTitle($where)) {
$entry_id = $wildcard_1->getMeta('entry_id');
$router->setGlobal('gv_agent_id', $entry_id);
$agentSite = ee()->db->select('field_id_4')
->where('channel_id', 1)
->where('entry_id', $entry_id)
->get('channel_data');
$isAgentSite = $agentSite->row('field_id_4');
// is the current agent site on and interior page?
if ($wildcard_2 != '' && $isAgentSite == 'y') {
$router->setTemplate('agent-site/_page');
// is the current agent site on and home page?
} elseif ($isAgentSite == 'y') {
$router->setTemplate('agent-site/index');
} else {
$router->set404();
}
} else {
$router->set404();
}
},