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
Related
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"
I'm trying to get the breadcrumbs on a Zend Framework 2 project but unfortunately they didn't apper on the page.
I've configured the navigation on the module.config.php as follows:
'navigation' => array(
'default' => array(
array(
'label' => 'Home',
'route' => 'home',
'pages' => array(
array(
'label' => 'Support',
'route' => 'home',
'action' => 'support',
),
),
),
array(
'label' => 'Blog',
'route' => 'post',
'pages' => array(
array(
'label' => 'Admin',
'route' => 'post',
'action' => 'admin',
),
),
),
),
),
And on the layout.phtml I've tried to show the breadcrumbs with each of the following methods, but without any success:
echo $this->navigation('navigation')->breadcrumbs()->setMinDepth(0)->setPartial('partial/breadcrumb.phtml'); (please note that I've also created the necessary breadcrumb.phtml)
echo $this->navigation('navigation')->breadcrumbs()->setMinDepth(0);
echo $this->navigation('navigation')->breadcrumbs()->render();
Can you help me please with a hint please? Please note that the routes from the navigation are also valid.
This should work.
echo $this->navigation()->breadcrumbs()->setPartial('partial/breadcrumb.phtml');
echo $this->navigation()->breadcrumbs()->setPartial('MODULENAME/partial/breadcrumb.phtml');
I am using following code in my application
<?php echo $this->navigation()->breadcrumbs('navigation')->setMinDepth(0)->setMaxDepth(3)->setLinkLast(false)->render();?>
This should work
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));
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!");
}
Build navigation from config:
'navigation' => array(
'default' => array(
'admin' => array(
'label' => 'Administration',
'controller' => 'index',
'action' => 'index',
'route' => 'admin/default',
),
'album' => array(
'label' => 'Album',
'controller' => 'index',
'action' => 'index',
'route' => 'album/default',
),
/* ... */
Routing is configured like it is true. Navigation in the menu works. Links menu lead to the desired controller/action of the desired module. But while introducing menu and a transition to one or another menuitem, active marked both points simultaneously and 'Administration' and 'Album'. As I understand it, for the reason that match the names of controllers and actions with them, but there's still the 'route' and it's different... not for nothing that the generated different url for each item... but somehow, despite this, they both are marked as active.
Routing config:
'router' => array(
'routes' => array(
'admin' => array(
'type' => 'Literal',
'options' => array(
'route' => '/admin',
'defaults' => array(
'__NAMESPACE__' => 'Admin\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller][/:action[/id:id]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
),
),
),
Album routing config similar...
Why this is happening? Thanks.
Looks like it's how ZF2 works (read isActive() function in Zend\Navigation\Page\Mvc.php). Initially it checks matching of route/controller/action, but if it fails, ZF2 again check for just controller/action pair. So there are three possible ways:
Open a ticket at https://github.com/zendframework/zf2/issues and wait for response.
Override \Zend\Navigation\Page\Mvc.
Choose different names for controllers (and don't use index name because it's default name for controller in Mvc.php).
If you make your controller names include the the namespace then they will be unique and won't clash:
Admin\Controller\IndexController
Album\Controller\IndexController
Rather than
Index
Index