Skip to content

Commit 49e7b8e

Browse files
committed
Update redirection doc
1 parent 93d3975 commit 49e7b8e

File tree

2 files changed

+98
-120
lines changed

2 files changed

+98
-120
lines changed

src/Resources/doc/index.md

Lines changed: 0 additions & 109 deletions
This file was deleted.

src/Resources/doc/redirection.md

Lines changed: 98 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,111 @@ Redirection
33

44
Set up automated mobile/desktop/tablet redirection
55

6-
In this example, we will make sure to activate the automatic redirection to a mobile site http://m.site.com when the user uses a mobile device and desktop http://site.com when the user uses a computer or desktop browser.
6+
In this example, we will make sure to activate the automatic redirection to a mobile site http://m.example.com when the user uses a mobile device and desktop http://example.com when the user uses a computer or desktop browser.
77

8-
If the user reaches the mobile site http://m.site.com, on his desktop browser he should be redirected to the full version at http://site.com.
8+
If the user reaches the mobile site http://m.example.com, on his desktop browser he should be redirected to the full version at http://example.com.
99

10-
If the user reaches the desktop site http://site.com, with his mobile he should be redirected to the full version at http://m.site.com.
10+
If the user reaches the desktop site http://example.com, with his mobile he should be redirected to the full version at http://m.example.com.
1111

12+
```env
13+
# .env
14+
REDIRECT_DESKTOP=http://example.com
15+
REDIRECT_MOBILE=http://m.example.com
16+
```
1217
```yaml
1318
# config/packages/mobile_detect.yaml
1419
mobile_detect:
1520
redirect:
1621
full:
17-
action: redirect # redirect, no_redirect, redirect_without_path
18-
host: http://localhost:8001 # with scheme (http|https), default null, url validate
19-
is_enabled: true # default false
20-
status_code: 301 # default 302
22+
action: redirect # redirect, no_redirect, redirect_without_path
23+
host: '%env(REDIRECT_DESKTOP)%' # with scheme (http|https), default null, url validate
24+
is_enabled: true # default false
25+
status_code: 301 # default 302
2126
mobile:
22-
action: redirect # redirect, no_redirect, redirect_without_path
23-
host: http://localhost:8002 # with scheme (http|https), default null, url validate
24-
is_enabled: true # default false
25-
status_code: 301 # default 302
27+
action: redirect # redirect, no_redirect, redirect_without_path
28+
host: '%env(REDIRECT_MOBILE)%' # with scheme (http|https), default null, url validate
29+
is_enabled: true # default false
30+
status_code: 301 # default 302
2631
```
32+
33+
Then you can create your Controllers and constrain your actions to match each host
34+
35+
```php
36+
// src/Controller/DesktopController.php
37+
<?php
38+
39+
namespace App\Controller;
40+
41+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
42+
use Symfony\Component\Routing\Annotation\Route;
43+
44+
#[Route(name: 'desktop_', host: '%env(REDIRECT_DESKTOP)%')]
45+
class DesktopController extends AbstractController
46+
{
47+
#[Route("/", name: "homepage")]
48+
public function homepage()
49+
{
50+
// dd('desktop');
51+
}
52+
}
53+
```
54+
55+
```php
56+
// src/Controller/MobileController.php
57+
<?php
58+
59+
namespace App\Controller;
60+
61+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
62+
use Symfony\Component\Routing\Annotation\Route;
63+
64+
#[Route(name: 'mobile_', host: '%env(REDIRECT_MOBILE)%')]
65+
class MobileController extends AbstractController
66+
{
67+
#[Route("/", name: "homepage")]
68+
public function homepage()
69+
{
70+
// dd('mobile');
71+
}
72+
}
73+
```
74+
75+
Or use it directly on your action
76+
```php
77+
// src/Controller/MainController.php
78+
<?php
79+
80+
namespace App\Controller;
81+
82+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
83+
use Symfony\Component\Routing\Annotation\Route;
84+
85+
class MainController extends Controller
86+
{
87+
#[Route("/myAction", name: 'my_action', host: '%env(REDIRECT_MOBILE)%')]
88+
public function myAction()
89+
{
90+
// dd('myAction');
91+
}
92+
}
93+
```
94+
95+
If your host contain a port (for local testing)
96+
```php
97+
// src/Controller/MobileController.php
98+
<?php
99+
100+
namespace App\Controller;
101+
102+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
103+
use Symfony\Component\Routing\Annotation\Route;
104+
105+
#[Route(name: 'mobile_', condition: "env('REDIRECT_MOBILE') === request.getSchemeAndHttpHost()")]
106+
class MobileController extends AbstractController
107+
{
108+
#[Route("/", name: "homepage")]
109+
public function homepage()
110+
{
111+
// dd('mobile');
112+
}
113+
}

0 commit comments

Comments
 (0)