Answer the question
In order to leave comments, you need to log in
Symfony4 - How to connect several options for the design / design of the site, and organize switching between them?
For example, there are two options for website design - standard and for mobile applications. And accordingly, it is necessary to switch between these design options depending on the device from which the user entered.
Answer the question
In order to leave comments, you need to log in
how the switching effect is not clear
1) either make an adaptive layout and then everything happens by itself
2) either sculpt a subdomain for mobile devices,
and then either use multi-routes
/**
* @Route("/", name="mobile_homepage", host="m.example.com")
* @Route("/", name="homepage")
*/
public function homepage(Request $request)
{
$currentHost = $request->getHttpHost();
var_dump($currentHost);die;
}
/**
* @Route("/", name="mobile_homepage", host="m.example.com")
*/
public function mobileHomepage()
{
// ...
}
/**
* @Route("/", name="homepage")
*/
public function homepage()
{
// ...
}
Thanks to everyone who replied and for the Kernel idea. Once again, I "climbed" to the docks, and found another solution.
It turns out that Symfony4 compiles the configuration, controllers, extensions, templates, etc., and puts it all in the /var/cache folder, where the path to the templates is already hardcoded. And changing it after that in the application most likely will not work.
So we initially need to create 2 configurations, and in our case, depending on $_SERVER['SERVER_NAME'] choose one of them.
To do this, we make changes to config/packages/twig.php
<?php
if (isset($_SERVER['SERVER_NAME']) && preg_match("/m.example.com/i", $_SERVER['SERVER_NAME'])) {
$container->loadFromExtension('twig', [
'paths' => [
'%kernel.project_dir%/templates_mobile' => null
],
// ... другие опции конфига
]);
} else {
$container->loadFromExtension('twig', [
'paths' => [
'%kernel.project_dir%/templates' => null
],
// ... другие опции конфига
]);
}
<?php
// ...
public function getCacheDir(): string
{
if (isset($_SERVER['SERVER_NAME']) && preg_match("/m.example.com/i", $_SERVER['SERVER_NAME'])) {
return $this->getProjectDir()."/var/cache_mobile/".$this->environment;
} else {
return $this->getProjectDir()."/var/cache/".$this->environment;
}
}
// ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question