PHP 中的路由条件
Conditions for Routing in PHP
如果国家/地区是美国,setViewFileName 将使用 Which_Online_Shopper_Are_You_US/default。html.twig
,我该如何设定条件
public function routeWhich_Online_Shopper_Are_You(Get $request, Twig $response): array
{
global $CONFIG;
if (I18l::getCountryByDomain() !== "GB") {
$response->redirect('/');
}
$siteName = $CONFIG['site_name'];
$siteUrl = $CONFIG['site_url'];
$context = [
'siteName' => $siteName,
'siteUrl' => $siteUrl
];
$page = $this->project->config->getPage();
$response->setViewFilename('Which_Online_Shopper_Are_You/default.html.twig');
$page->setPageTitle('Which Online Shopper Are You');
$page->updateMeta();
return $context;
}
您可以将视图设置为可以根据当前国家覆盖的变量。
我还确保美国没有被重定向。
更多细节在下面的代码中添加为注释:
public function routeWhich_Online_Shopper_Are_You(Get $request, Twig $response): array
{
global $CONFIG;
// Add all the allowed countries
$allowed = ['GB', 'US'];
// Get the current country
$currentCountry = I18l::getCountryByDomain();
// Check if the current country is in the allowed array. If not, redirect
if (in_array($currentCountry, $allowed) === false) {
$response->redirect('/');
}
// Set the default view
$viewFile = 'Which_Online_Shopper_Are_You/default.html.twig';
// Now override the view if the country is US
if ($currentCountry === 'US') {
$viewFile = 'Which_Online_Shopper_Are_You_US/default.html.twig';
}
$siteName = $CONFIG['site_name'];
$siteUrl = $CONFIG['site_url'];
$context = [
'siteName' => $siteName,
'siteUrl' => $siteUrl
];
$page = $this->project->config->getPage();
// Let's use our dynamic viewFile variable to set the view
$response->setViewFilename($viewFile);
$page->setPageTitle('Which Online Shopper Are You');
$page->updateMeta();
return $context;
}
如果国家/地区是美国,setViewFileName 将使用 Which_Online_Shopper_Are_You_US/default。html.twig
,我该如何设定条件public function routeWhich_Online_Shopper_Are_You(Get $request, Twig $response): array
{
global $CONFIG;
if (I18l::getCountryByDomain() !== "GB") {
$response->redirect('/');
}
$siteName = $CONFIG['site_name'];
$siteUrl = $CONFIG['site_url'];
$context = [
'siteName' => $siteName,
'siteUrl' => $siteUrl
];
$page = $this->project->config->getPage();
$response->setViewFilename('Which_Online_Shopper_Are_You/default.html.twig');
$page->setPageTitle('Which Online Shopper Are You');
$page->updateMeta();
return $context;
}
您可以将视图设置为可以根据当前国家覆盖的变量。
我还确保美国没有被重定向。
更多细节在下面的代码中添加为注释:
public function routeWhich_Online_Shopper_Are_You(Get $request, Twig $response): array
{
global $CONFIG;
// Add all the allowed countries
$allowed = ['GB', 'US'];
// Get the current country
$currentCountry = I18l::getCountryByDomain();
// Check if the current country is in the allowed array. If not, redirect
if (in_array($currentCountry, $allowed) === false) {
$response->redirect('/');
}
// Set the default view
$viewFile = 'Which_Online_Shopper_Are_You/default.html.twig';
// Now override the view if the country is US
if ($currentCountry === 'US') {
$viewFile = 'Which_Online_Shopper_Are_You_US/default.html.twig';
}
$siteName = $CONFIG['site_name'];
$siteUrl = $CONFIG['site_url'];
$context = [
'siteName' => $siteName,
'siteUrl' => $siteUrl
];
$page = $this->project->config->getPage();
// Let's use our dynamic viewFile variable to set the view
$response->setViewFilename($viewFile);
$page->setPageTitle('Which Online Shopper Are You');
$page->updateMeta();
return $context;
}