Combining routes
Created by: agorlov
Options:
// option1:
new AppStd(
[
'/' => action1
'/admin/*' => new ActRouted(
new RqSERVER(),
[
'/admin/' => new ActAdmin(),
'/admin/something' => new ActAdminSomething(),
]
)
]
);
//option2 nested ActRouted
new AppStd(
new ActRouted(
new RqSERVER(),
[
'/' => action1
'/contacts' => action2
],
new ActAuthorized(
new ActRouted( // if not found in array above - action
new RqSERVER(),
[
'/admin' => new ActAdmin(),
'/admin/something' => new ActAdminSomething(),
]
)
)
);
// option3:
new AppStd(
[
'/' => action1
'/admin/*' => new ActAdminPart() // one option
''~ ^/news' => new ActNews(), // or like in nginx
]
);
class ActAdminPart implements Action {
public function __construct(Request $rqSERVER = null) {
new ActRouted(
[
'/admin' => new ActAdmin(),
'/admin/something' => new ActAdminSomething(),
]
)
}
}
PS: Sample code to handle patterns ~ regexp:
* @param $urls array
* [
* '/pa' => ...
* '/partner/' => ...
* '~ ^/partner' => ...
* '/partner/si' => ...
* ]
* @return string if match '~ ^/partner'
*/
function pmatchUrl(string $url, array $urls)
{
$match = '';
foreach ($urls as $u => $val) {
if ($u[0] !== '~') { // handle only strings starting with ~
continue;
}
$pattern = ltrim($u, '~ ');
$res = preg_match('#' . $pattern . '#', $url);
if ($res === false) {
throw new Exception(": #$u# ");
}
if ($res === 1) { //нашли совпадение
return $u;
}
}
return false;
}