1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Zend Framework (http://framework.zend.com/) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) |
7
|
|
|
* @license http://framework.zend.com/license/new-bsd New BSD License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace PlaygroundCore\Controller\Frontend; |
11
|
|
|
|
12
|
|
|
use Zend\ServiceManager\ServiceLocatorAwareInterface; |
13
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
14
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
15
|
|
|
|
16
|
|
|
class SwitchLocaleController extends AbstractActionController implements ServiceLocatorAwareInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var $localeService : Service des locales |
20
|
|
|
*/ |
21
|
|
|
protected $localeService; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* switchAction : permet de switcher de langue en fonction d'un context (back/front) |
25
|
|
|
* locale : locale pour switch |
26
|
|
|
* context : (back/front) |
27
|
|
|
* referer : retour à la page |
28
|
|
|
* |
29
|
|
|
* @return Redirect $redirect redirect to referer |
30
|
|
|
*/ |
31
|
|
|
public function switchAction() |
32
|
|
|
{ |
33
|
|
|
$locale = $this->getEvent()->getRouteMatch()->getParam('locale'); |
34
|
|
|
$context = $this->getEvent()->getRouteMatch()->getParam('area'); |
35
|
|
|
$redirect = (!empty($this->getEvent()->getRouteMatch()->getParam('redirect')))? urldecode($this->getEvent()->getRouteMatch()->getParam('redirect')) : '/'; |
36
|
|
|
|
37
|
|
|
$cookie = new \Zend\Http\Header\SetCookie('pg_locale_'.$context, $locale, time() + 60*60*24*365, '/'); |
38
|
|
|
$this->getResponse()->getHeaders()->addHeader($cookie); |
|
|
|
|
39
|
|
|
|
40
|
|
|
return $this->redirect()->toUrl($redirect); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* getServiceLocator : Recuperer le service locator |
46
|
|
|
* @return ServiceLocator $serviceLocator |
47
|
|
|
*/ |
48
|
|
|
public function getServiceLocator() |
49
|
|
|
{ |
50
|
|
|
return $this->serviceLocator; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* setServiceLocator : set le service locator |
55
|
|
|
*/ |
56
|
|
|
public function setServiceLocator(ServiceLocatorInterface $serviceLocator) |
57
|
|
|
{ |
58
|
|
|
$this->serviceLocator = $serviceLocator; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* getLocaleService : Recuperer le service des locales |
63
|
|
|
* |
64
|
|
|
* @return Service/Locale $localeService |
|
|
|
|
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function getLocaleService() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
if ($this->localeService === null) { |
69
|
|
|
$this->localeService = $this->getServiceLocator()->get('playgroundcore_locale_service'); |
70
|
|
|
} |
71
|
|
|
return $this->localeService; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: