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

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

Render partial in other module ZF2

In my project I have to modules
Module1
Module2
In Module1 I have a view that needs to render a view that I have in Module2, so what I'm doing is:
$this->partial('partials/hello/title.phtml','Module2',array('data' => $data))
and seems I'm calling correctly the view, but inside of the view title.phtml I'm unable to use data
Undefined variable: data in /site/src/module/Module2/view/partials/hello/title.phtml
Do I need add something related to the configuration?
Thanks!
You aren't calling it correctly, try:
$this->partial('partials/hello/title.phtml', array('data' => $data));
See: http://framework.zend.com/manual/2.3/en/modules/zend.view.helpers.partial.html
The fact that the partial is in a different module doesn't matter. Having to specify the module name as the second parameter was a thing in ZF1 only.
Another solution is add your own view-path resolver in module-bootstrap:
class Module
{
/* #var \Zend\ServiceManager\ServiceManager $SM */
public static $SM;
/* #var \Zend\EventManager\EventManager $EM */
public static $EM;
public function onBootstrap(MvcEvent $e)
{
self::$SM = $e->getApplication()->getServiceManager();
self::$EM = $e->getApplication()->getEventManager();
//change view resolver to resolve views from {Module}/view/{Controller}/{Action}.phtml path
self::$EM->attach('dispatch', function($e) {
self::$SM->get('ViewRenderer')->resolver()->attach(
new \Engine\View\Resolver\MCA() , 10
);
});
}
....
Resolver gets a string, and must to return path or false (if not resolved), just write resolver to understand other-modules paths:
<?php
namespace Engine\View\Resolver;
use Zend\View\Renderer\RendererInterface as Renderer;
/**
* Resolves view scripts based on a stack of paths
*/
class MCA implements \Zend\View\Resolver\ResolverInterface
{
public function resolve($name, Renderer $renderer = null)
{
$path = explode('/', $name);
if (count($path)<3){
return false;
}
$module = array_shift($path);
$resolvedPath = ROOT_PATH . '/module/'. ucfirst($module) . '/view/' . implode('/', $path). '.phtml';
if (!file_exists($resolvedPath)){
return false;
}
return $resolvedPath;
}
}
There can be collisions, but you can regulate priority of resolvers (10 in example).

How to end the execution of an action via an event

in my ZF2 application I am adding the following event listener, however I want to make the execution of the action actually stop, however this doesnt happen.
public function setEventManager(EventManagerInterface $events)
{
parent::setEventManager($events);
$controller = $this;
$events->attach('dispatch', function ($e) use ($controller) {
$request = $e->getRequest();
$method = $request->getMethod();
$headers = $request->getHeaders();
// If we get here, based on some conditions, the original intended action must return a new JsonViewModel()...
return new JsonViewModel([]); // However, this doesn't do anything.
}, 100); // execute before executing action logic
}
Based on your comments, I am assuming you are doing some sort of authentication. You can perfecty use the event manager for this. However, I would not tie the listener to a single controller. If your API increases, you might want to split the API over several controllers and you get into trouble with your authentication.
My solution is to create a listener which listens to the dispatch event of the Zend\Mvc\Application. This is an event which is triggered before the event in the controllers itself.
use Zend\Mvc\MvcEvent;
public function onBootstrap(MvcEvent $e)
{
$app = $e->getApplication();
$em = $app->getEventManager();
$sm = $app->getServiceManager()->getSharedManager();
$listener = new Listener\Authentication();
$identifier = 'MyModule\Controller\ApiController';
$em->attach($identifier, MvcEvent::EVENT_DISPATCH, $listener, 1000);
}
This way, the listener is attached to all controllers which are identified with MyModule\Controller\ApiController. The listener will be triggered on every dispatch call of those controllers. Your listener can short-circuit the complete dispatch loop in case you need it:
use Zend\Http\Request as HttpRequest;
use Zend\Mvc\MvcEvent;
use Zend\Json\Json;
use Zend\View\Model\JsonModel;
class Authentication
{
public function __invoke(MvcEvent $e)
{
$request = $e->getRequest();
if (!$request instanceof HttpRequest) {
// Don't run on CLI requests
return;
}
if ($result->isValid()) {
// Say you get auth result and all is OK
return;
}
// Case 1: short-circuit and return response, this is the fast way
// The response I use here is the HTTP problem API
$message = array(
'httpStatus' => 401,
'title' => 'Unauthorized',
'detail' => 'You are unauthorized to perform this request',
);
$response = $e->getResponse();
$response->setStatusCode(401);
$response->getHeaders()->addHeaderLine('Content-Type', 'application/json');
$response->setContent(Json::encode($message);
return $response;
// Case 2: don't short circuit and stop propagation, you're using the JSON renderer
$e->getResponse()->setStatusCode(401);
$message = array(
'httpStatus' => 401,
'title' => 'Unauthorized',
'detail' => 'You are unauthorized to perform this request',
);
$model = new JsonModel($message);
return $model;
}
}
I would advice you to use the first method (returning the response yourself) because you'll short circuit the complete dispatch process and skip the complete finishing of the request. If you really rely on the view and response senders, use the second case.
Now if you need a controller which is authenticated via this system, add the identifier to this controller:
namespace MyModule\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class MyFooBarApiController extends AbstractActionController
{
protected $eventIdentifer = 'MyModule\Controller\ApiController';
// your code here
}
If you need to allow certain requests without validation (I would always use a whitelist!), you can do this in your listener:
use Zend\Mvc\Route\RouteMatch;
$routematch = $e->getRouteMatch();
if (!$routematch instance of RouteMatch) {
// It's a 404, don't perform auth
return;
}
$route = $routematch->getMatchedRouteName();
if (
($request->isPost() && 'foo/bar' === $route)
|| ($request->isGet() && 'baz/bat' === $route)
) {
// We allow these requests to pass on without auth
return;
}
In your code you can explicitly check request method and route name. If you need parameters of the route, you can access it with $routematch->getParam('id').
Use the following in your event:
$e->stopPropagation();

Get local value in layout or view in Zend Framework 2

How can we get local value (i.e: 'en' or 'en_US', 'de' etc) in layout.phtml or views in Zend Framework 2?
My local setting are exactly same as explained here
<?php
namespace FileManager;
use Zend\Mvc\ModuleRouteListener;
class Module
{
public function onBootstrap($e)
{
$translator = $e->getApplication()->getServiceManager()->get('translator');
$translator
->setLocale(\Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']))
->setFallbackLocale('en_US');
}
//...
}
I want to get local value something like this:
$locale = $this->translate()->getLocale(); // <-- It's not working anyway
I need to use '$locale' it while calling google map api url to get matched locale/language. I'm calling it throughtout the application in layout.phtml
$this->headScript()->appendFile('http://maps.googleapis.com/maps/api/js?language=' . $locale);
So I want to make language option dynamic while calling api.
PS: I don't have any query string parameter such as 'language', It's a google api thing which I need to set in script url (if you don't know) Please don't get confused.
Not answered here
Depends on where you want to get the Locale value from. In any case, you can do it in your controller, e.g.:
$locale = $this->request->getQuery('language');
$this->layout()->locale = $locale;
or
return new ViewModel(array('locale' => $locale));
Edit if you just want to get the locale from the translator, you can try this in view script:
$this->plugin('translate')->getTranslator()->getLocale();
My version is like that
<?php
namespace FileManager;
use Zend\Mvc\ModuleRouteListener;
use Zend\Session\Container;
class Module
{
public function onBootstrap($e)
{
$application = $e->getTarget();
$serviceManager = $application->getServiceManager();
$eventManager = $application->getEventManager();
$events = $eventManager->getSharedManager();
// session container
$sessionContainer = new Container('locale');
// test if the language in session exists
if(!$sessionContainer->offsetExists('mylocale')){
// doesn't so the browser lan
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
$sessionContainer->offsetSet('mylocale', Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']));
}else{
$sessionContainer->offsetSet('mylocale', 'en_US');
}
}
// translation
$translator = $serviceManager->get('translator');
$translator ->setLocale($sessionContainer->mylocale)
->setFallbackLocale('en_US');
$mylocale = $sessionContainer->mylocale;
$events->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) use ($mylocale) {
$controller = $e->getTarget();
$controller->layout()->mylocale = $mylocale;
}, 100);
}
//...
}
in your layout
$this->headScript()->appendFile('http://maps.googleapis.com/maps/api/js?language=' . $this->mylocale);

Set layout variables for use by the (404) error pages in ZF2

At present I set a couple of variables to be used by the app's overall layout.phtml, using the onDispatch method of a BaseController, which all my other controllers extend:
public function onDispatch(MvcEvent $e)
{
$config = $this->getServiceLocator()->get('config');
$this->layout()->setVariable('platformName', $config['platform']['name']);
$this->layout()->setVariable('platformYear', $config['platform']['year']);
}
This works fine, until I test some error pages and find that these pages do not get provided with the variables, as it's not using the base controller.
How can I get around this problem and provide the error pages with the same variables?
Change the event you're listening for.
In this case, I'd move this logic to the application bootstrap event or the application render event (I haven't tested this, but it would probably work fine).
One example, in your Module.php
public function onBootstrap($e)
{
$config = $e->getApplication()->getServiceManager()->get('config');
//$e->getViewModel()->setVariable();
}
Haven't tested that commented out line, but it should get you headed in the right direction.
EDIT: Found an example of using the render event
public function onBootstrap($e)
{
$event = $e->getApplication()->getEventManager();
$event->attach('render', function($e) {
$config = $e->getApplication()->getServiceManager()->get('config');
$e->getViewModel()->setVariable('test', 'test');
});
}
(Necro)
When using onDispatch in a Controller, remember to return the parent with the event and all:
public function onDispatch(MvcEvent $e)
{
// Your code
return parent::onDispatch($e);
}
Otherwise, the logic on your Actions in that Controller will be ignored.

where is the autoload? trying to unit test

i am following a getting started tutorial on zend framework 2, in one of the topics it suggests using tests, the code it suggests is:
namespace ApplicationTest\Controller;
use ApplicationTest\Bootstrap;
use Zend\Mvc\Router\Http\TreeRouteStack as HttpRouter;
use Application\Controller\IndexController;
use Zend\Http\Request;
use Zend\Http\Response;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
use PHPUnit_Framework_TestCase;
class IndexControllerTest extends PHPUnit_Framework_TestCase
{
protected $controller;
protected $request;
protected $response;
protected $routeMatch;
protected $event;
protected function setUp()
{
$serviceManager = Bootstrap::getServiceManager();
$this->controller = new IndexController();
$this->request = new Request();
$this->routeMatch = new RouteMatch(array('controller' => 'index'));
$this->event = new MvcEvent();
$config = $serviceManager->get('Config');
$routerConfig = isset($config['router']) ? $config['router'] : array();
$router = HttpRouter::factory($routerConfig);
$this->event->setRouter($router);
$this->event->setRouteMatch($this->routeMatch);
$this->controller->setEvent($this->event);
$this->controller->setServiceLocator($serviceManager);
}
public function testIndexActionCanBeAccessed()
{
$this->routeMatch->setParam('action', 'index');
$result = $this->controller->dispatch($this->request);
$response = $this->controller->getResponse();
$this->assertEquals(200, $response->getStatusCode());
}
}
as you can see there is no __autoload($class).
to manually make it work i added include "../../Bootstrap.php"; it did solve the problem but i remember once i could get this code to work, and the tutorial doesn't seem to forget something conceptually obvious and there is no feedback about it in the Topic comments , there may be something I am missing, how would the code above probably work?
I managed to get it working but noticed you couldn't use phpUnit's extended Request and Response objects. These are the instructions for early 2.0 release. At least after 2.0.7, the instructions are much different and the code is cleaner:
http://zf2.readthedocs.org/en/latest/user-guide/unit-testing.html
<?php
namespace ApplicationTest\Controller;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
class IndexControllerTest extends AbstractHttpControllerTestCase
{
public function setUp()
{
$this->setApplicationConfig(
include '/path/to/application/config/test/application.config.php'
);
parent::setUp();
}
public function testIndexActionCanBeAccessed()
{
$this->dispatch('/');
$this->assertResponseStatusCode(200);
$this->assertModuleName('application');
$this->assertControllerName('application_index');
$this->assertControllerClass('IndexController');
$this->assertMatchedRouteName('home');
}
}
In this example testing is carried out by extending Zend's controller test case, which was the way controller tests were carried out with zf1.

Resources