zend-framework2


Zend framework 2 session exists or not, in Module.php


I am trying to call the session in public function onBootstrap(MvcEvent $e) function in Module.php
public function onBootstrap(MvcEvent $e)
{
if( $user_session->offsetExists('user_email_id')){
//code here
}
else {
header("Location: ". $this->serverUrl() . "/register");
}
}
How can i achieve this?
i am not getting the echo $this->serverUrl(); inside the OnBootstrap function
There a number of problems with this code.
You need to create a new session container (Zend\Session\Container) to set/get your session data.
You are trying to set headers manually, although this would work, there are better ways to do so in ZF2.
Redirection in the onBootstrap method is probably not the best 'time' to do so.
You attempt to use a view helper in Module.php (\Zend\View\Helper\ServiceUrl) to redirect. View helpers can should only be called in the view. You can use them, however you would need to fetch it via the ViewPluginManager, rather than using $this->.
With these points in mind I would consider adding a event listener either late onRoute or early onDispatch.
For example:
namespace FooModule;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
use Zend\EventManager\EventInterface;
use Zend\Session\Container;
use Zend\Mvc\MvcEvent;
class Module implements BootstrapListenerInterface
{
public function onBootstrap(EventInterface $event)
{
$application = $event->getApplication();
$eventManager = $application->getEventManager();
$eventManager->attach(MvcEvent::EVENT_DISPATCH, [$this, 'isLoggedIn'], 100);
}
public function isLoggedIn(MvcEvent $event)
{
$data = new Container('user');
if (! isset($data['user_email_id'])) {
$serviceManager = $event->getApplication()->getServiceManager();
$controllerPluginManager = $serviceManager->get('ControllerPluginManager');
// Get the \Zend\Mvc\Controller\Plugin\Redirect
$redirect = $controllerPluginManager->get('redirect');
return $redirect->toRoute('some/route/path', ['foo' => 'bar']);
}
// use $data here
}
}

Related Links

save path in database / or \
I cannot generate phpunit test for albumcontroller
zf2 JSON-RPC server how to return custom error
Independent blocks on page
Append to Zend\Navigation
how to use security features in Zend framework 2?
ZF2 - Retain query from form using url helper in pagination
Zend Framework 2 Form Element Label No Escape
SharedEventManger and __NAMESPACE__
Zend Framework 2 instaliation without console or composer?
ServiceNotCreatedException in Zend Framework 2 while attempting multiple navigations
How to make ZfcUser use an abstract db_adapter
zf2 Form class issue
PDF is upload with application/octet-stream
Zend Framework 2 - Multi Page Form
Add a fragment id to a Zend redirect

Categories

HOME
batch-processing
erlang
wmic
voip
antivirus
agile
otrs
thunderbird-addon
mapbox-gl-js
travis-ci
httpclient
sudo
actionscript-2
desktop
aruco
constructor
data-synchronization
mongodb-query
future
cosmicmind
react-leaflet
event-log
pdo
apache-karaf
thumbnails
mousewheel
marathon
piwik
extractor
jquery-waypoints
http-method
data-cleansing
opencart2.3
frp
aurelia-binding
xor
twitter-bootstrap-2
svnkit
data-extraction
body-parser
nsurlconnection
npm-publish
es-shell
initializer
mongoexport
cgo
constants
encapsulation
gauss
seamless-immutable
dwarf
tofixed
pycurl
search-box
redux-router
autoresize
barcode-printing
hspec
backstop.js
metalsmith
jackson-databind
kendo-combobox
faraday
bluesnap
web-essentials
textkit
base32
twython
prezto
robocode
low-level
sqoop2
pl-i
line-numbers
omnifocus
fragment-tab-host
grunt-express
iostream
spdy
dvcs
unison
help-viewer
reserved-words
cling
opendata
cdata
cilk-plus
sql-server-2012-web
sttwitter
windows-update
soa-suite
fraud-prevention
device-emulation
vt100
transitive-closure-table
mvs
bulbs
feof
getstring
z-machine
jquery-selectbox
hinstance
smooth
pantheios
graniteds
plinq
nt4
msf
project-lifecycle
dotproject

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App