Can I have an action that doesn't load a template? - php

I got an answer to this question here, but now I have another concern\problem.
I have a subnav bar in my module created from a view partial via a helper.
Here is the config in module.config.php:
'navigation' => array(
'default' => array(
array(
'label' => 'Search',
'route' => 'mymodule\search',
),
array(
'label' => 'Log Off',
'route' => 'logout',
),
),
),
I have a LoginController with 2 actions: login and logout. After logging in I want the user to click the logout button, be redirected to the login page and have their session cleared.
Now if I have a login and logout action I need a template for each. This seems unnecessary for the logout action- I don't want another template to load for this action.
Here's my routing config:
'login' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/login',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Application\Controller\Login',
'action' => 'login',
),
),
),
'logout' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/login',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Application\Controller\Login',
'action' => 'logout',
),
),
),
Is there a correct way to have an action get called without loading a corresponding template?

You can return a response object instead:
public function logoutAction()
{
// do stuff
return $this->redirect()->toRoute('login');
}
then you don't need a template.

On zf2
public function Action(){
$vm = new ViewModel();
$vm->setTerminal(true);
return $vm;
}
EDIT:::
In case that you want to return a custom response you can do:
public function Action(){
return $this->getResponse()->setContent("Hello world!");
}

Related

ZF2 Paginator issue with child routes

In my application I want to add some simple pagination. Normal first level routes function without any problems but when I try to implement a paginator in a child route I get error messages. Don't know how to address my child route correctly.
I have following routes:
'queues' => array(
'type' => 'Literal',
'options' => array(
'route' => '/queues',
'defaults' => array(
'controller' => 'Mail\Controller\MailQueue',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'show' => array(
'type' => 'Segment',
'options' => array(
'route' => '/show/:queue_id',
'constraints' => array(
'queue_id' => '[0-9]+',
),
'defaults' => array(
'action' => 'show',
),
),
),
)
)
And here the paginationControl from the .phtml file to output the pagination html:
echo $this->paginationControl(
// the paginator object
$this->paginator,
// the scrolling style
'sliding',
// the partial to use to render the control
'partial/paginator.phtml',
// the route to link to when a user clicks a control link
array(
'route' => 'queues/show/' . $this->queue->getId()
)
);
Unfortunately, I get this error:
File:
/home/norbert/dev/holding/efeedback_mail/vendor/zendframework/zend-mvc/src/Router/Http/TreeRouteStack.php:322
Message:
Route with name "show" does not have child routes
When I try to give it the route without the slash:
array(
'route' => 'queues/show', $this->queue->getId()
)
I get this segment error:
File:
/home/norbert/dev/holding/efeedback_mail/vendor/zendframework/zend-mvc/src/Router/Http/Segment.php:298
Message:
Missing parameter "queue_id"

Zend 2 redirect with toRoute not working

Why is Zend 2 such a !##(#(!##??
OK, so I'm trying to get a simple redirect working. I have a controller called 'listitems' with an action called 'editlistitem'. After hours of banging on it with a hand sledge, I've finally got the form to work and the validation to work and the hydration to Doctrine to work so I can save the result.
The last step is to redirect the user to the 'showlistitem' action which includes the id trailing it. (full route sub path is 'listitem/showlistitem/2' where 2 is the id I want to see)
I have tried:
$this->redirect()->toRoute('/listitem/showlistitem/2');
$this->redirect()->toRoute('listitem/showlistitem/2');
$this->redirect()->toRoute('showlistitem/2');
$this->redirect()->toRoute('listitem/showlistitem', array('id' => 2));
$this->redirect()->toRoute('listitem-showlistitem', array('id' => 2));
None of them flippin work! (they all return route not found)
A route to the controller is in modules.config.php with a child route to the action. I can go directly to the url by typing it in manually and it works fine. How in the bleep do I get Zend to redirect the user to that route from an action?
The toRoute method provided by the The Redirect plugin needs the route name to be passed as parameter. This is its desciption :
toRoute(string $route = null, array $params = array(), array $options = array(), boolean $reuseMatchedParams = false)
Redirects to a named route, using the provided $params and $options to assembled the URL.
Given this simple route configuration example :
//module.config.php
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Segment',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'index',
'action' => 'index',
),
),
),
'app' => array(
'type' => 'Literal',
'options' => array(
'route' => '/app',
'defaults' => array(
'controller' => 'index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id'=>'[0-9]+',
),
),
),
),
),
),
),
This redirection works :
return $this->redirect()->toRoute('app/default',
array('controller'=>'controller-name', 'action'=>'action-name', 'id'=>$id));
In your case, this would work :
return $this->redirect()->toRoute('app/default',
array('controller'=>'listitem', 'action'=>'showlistitem', 'id'=>2));

Zend 2 determinate routed / used Navigation in View

How can i determinate in which navigation the request is routed in View (/ Template)?
I have two Navigations:
1 - User Navigation
2 - Admin Navigation
If User is logged in system as Admin, both navbars should be accessible (switched via triggerdiv)
So if the Admin triggers a link on the Usernavigation i would like to show the Usernavigation after the new request - if he triggers an Adminlink i would like to show the AdminNavbar.
Here the navigations array from module.config.php:
'navigation' => array(
'default' => array(
array(
'label' => 'Home',
'route' => 'user',
'resource' => 'navigation',
'privilege' => 'home',
),
array(
'label' => 'Login',
'route' => 'auth',
'resource' => 'navigation',
'privilege' => 'auth',
),
array(
'label' => 'Logout',
'route' => 'logout',
'resource' => 'navigation',
'privilege' => 'logout',
),
),
'admin' => array(
array(
'label' => 'Home',
'route' => 'admin',
'resource' => 'navigation',
'privilege' => 'home',
),
array(
'label' => 'Users',
'route' => 'users',
'resource' => 'navigation',
'privilege' => 'users',
),
array(
'label' => 'Logout',
'route' => 'logout',
'resource' => 'navigation',
'privilege' => 'logout',
),
),
),
if possible i would like to know and proof for the navigation keys from the navigation array.
i have no clue how i could access them, neither i found some helping links till now..
I came now up with following solution:
every controller is extended from Guest / User or Admin controller
in construct of extended controller i set the navigation key":
protected $_sNavigationKey = 'guest';
public function __construct()
{
$this->getEventManager()->attach('dispatch', function ($e)
{
$this->layout()->setVariable('sNavigationKey', $this->_sNavigationKey);
});
}
now i can check / show the right navigation in the layout via proof of $sNavigationKey

Grant ACL permission by URLs in Zend Framework 2

I follow this tutorial: http://ivangospodinow.com/zend-framework-2-acl-setup-in-5-minutes-tutorial/
But, I want to grant permission by custom URLs, so I had some changes in my code.
In module.acl.roles.php
return array(
'guest'=> array(
'/home.html',
'/login.html',
'/register.html'
),
'admin'=> array(
'/user/add.html',
'/user/edit.html',
'/user/list.html',
),
);
In module.config.php
return array(
'router' => array(
'routes' => array(
'/home.html' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
'/user/add.html' => array(
'type' => 'Zend\Mvc\Router\Http\Regex',
'options' => array(
'regex' => '/user/add.html',
'defaults' => array(
'controller' => 'Application\Controller\User',
'action' => 'add',
'format' => 'html',
),
'spec' => '/user/add.%format%',
),
),
...
),
),
);
But I received this error: Route with name "" not found. Please give me some advices and solutions to grant permission by URLs
Thank you!
I really really recommend the BjyAuthorize module (https://packagist.org/packages/bjyoungblood/bjy-authorize).
But if you really want to do this by yourselft you need to add a listener to the \Zend\Mvc\MvcEvent::EVENT_ROUTE.
You can attach your listener with
$events->attach(MvcEvent::EVENT_ROUTE, array($this, 'myOnRoute'), -1000);
and in your myOnRoute method you can handle the route
public function myOnRoute(MvcEvent $event) {
$match = $event->getRouteMatch();
$routeName = $match->getMatchedRouteName();
// do stuff here (compare to config or whatever)
}

Zend redirect not working

I have following configuration in module.config.php
'router'=>array(
'routes'=>array(
'test'=>array(
'type'=>'Zend\Mvc\Router\Http\Literal',
'options'=>array(
'route'=>'/test',
'defaults'=>array(
'controller'=>'Test\Controller\Index',
'action'=>'index'
)
),
'may_terminate' => true,
'child_routes' => array(
'edit' => array(
'type' => 'segment',
'options' => array(
'route' =>'/edit[/:id]',
'constraints' =>array('id' => '[\d\w\-_]*'),
'defaults' => array(
'controller' =>'Test\Controller\Index',
'action' => 'edit',
),
),
),
'add' => array(
'type' => 'segment',
'options' => array(
'route' =>'/add',
'defaults' => array(
'controller' =>'Test\Controller\Index',
'action' => 'add',
),
),
),
),
)
)
)
now I am using following redirect
return $this->redirect()->toRoute('test');
and it is not working. I am not getting what is wrong with this
Your Config file is not much helpful here, you should have posted your controller's code, nevertheless ,
instead of return $this->redirect()->toRoute('test');,
use the following format
return $this->redirect()->toRoute('ModuleName',
array('controller'=>controllerName,
'action' => actionName,
'params' =>params));
Where ModuleName is the name of your module,
Where ControllerName is the name of your controller,
Where Action Name is the name of your action,
Note:- params are optional.
If you module name is test, then you try with:
return $this->redirect()->toUrl('/test');

Categories