-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.php
executable file
·107 lines (92 loc) · 2.83 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace SocialogSEO;
use Zend\EventManager\EventInterface;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\Mvc\MvcEvent;
use Zend\ServiceManager\ServiceLocatorInterface;
/**
* Socialog SEO Module
*/
class Module implements
BootstrapListenerInterface,
AutoloaderProviderInterface,
ConfigProviderInterface
{
/**
* @return ServiceLocatorInterface
*/
protected $sm;
public function onBootstrap(EventInterface $e)
{
$app = $e->getApplication();
$this->sm = $app->getServiceManager();
$app->getEventManager()->attach(MvcEvent::EVENT_ROUTE, array($this, 'onPreRoute'), -100);
$app->getEventManager()->attach(MvcEvent::EVENT_RENDER, array($this, 'onRender'), -100);
}
/**
* Add extra metadata when rendering a page
* @param \Zend\Mvc\MvcEvent $e
*/
public function onRender(MvcEvent $e)
{
$view = $this->sm->get('ViewRenderer');
$config = $this->sm->get('Config');
$view->headMeta()->appendName('robots', $config['socialog-seo']['content']);
$view->headLink(array(
'rel' => 'author',
'href' => 'humans.txt',
));
}
/**
* Listen to routing and redirect any seo links
* @param \Zend\Mvc\MvcEvent $e
*/
public function onPreRoute(MvcEvent $e)
{
$basePath = $e->getRequest()->getBaseUrl();
$relativePath = substr($e->getRequest()->getUri()->getPath(), strlen($basePath));
$config = $this->sm->get('Config');
$config = $config['socialog-seo']['redirect'];
if (isset($config[$relativePath])) {
$redirect = $config[$relativePath];
$code = 301;
$url = null;
if (is_string($redirect)) {
$url = $redirect;
} elseif (is_array($redirect)) {
if (isset($redirect['code'])) {
$code = $redirect['code'];
}
if (isset($redirect['url'])) {
$url = $redirect['url'];
}
}
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode($code);
return $response;
}
}
/**
* @return array
*/
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
/**
* @return array
*/
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}