Breadcrumbs are not shown on zend2 - php

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

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"

How to get params in action in ZendFramework 2?

I have configured a route in my module.config.php to handle two parameters ,
here is the content of the file module.config.php , i put just the definition of the routes ( same routes ) :
updated :
'router' => array(
'routes' => array(
'Products' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/application/admin/products[/:id]',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Admin',
'action' => 'index',
),
),
),
'productsList' => array(
'type' => 'segment',
'options' => array(
'route' => '/application/products/productsList[/:type][/:id]',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Products',
'action' => 'productsList'
),
),
),
'Emplacement' => array(
'type' => 'segment',
'options' => array(
'route' => '/application/support/listeEmplacementsSupport[/:pkSupport]',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Support',
'action' => 'listeEmplacementsSupport',
),
),
),
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id][/:dr]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
And I build a link in the view using the following
<a href="/application/products/productsList/mandan/<?php echo $coreg['idProduct']; ?>" >
<img height='20' src="/img/mail-recevoir-32.png" alt='products' />
</a>
To get one parameter I use
$param = $this->params('id');
Routes are matched in the 'Last in first out' LIFO order.
The relevant section of the documentation states:
Typically, routes should be queried in a LIFO order, and hence the reason behind the name RouteStack. [...] you will need to ensure that routes that potentially overlap are registered such that the most specific match will match first (i.e., register later)
Therefore based on you current configuration, as all the routes are register at the top level, the last route (application/default) is being checked and then subsequently matched first, before the productList is given a chance.
Also because application/default defines an optional id parameter as the first argument this is the reason that you get the value of the type param when using $this->params('id').
Soultion
The easiest solution is just to reorder the routes; moving the application route right to the top of the definition should work.
The order should be the following
'router' => array(
'routes' => array(
'application' => array(
// .. same
'child_routes' => array(
'default' => array(
// .. Same
),
),
),
'Products' => array(
// ..same
),
'productsList' => array(
// ..same
),
'Emplacement' => array(
// ..config
),
),
),
You could alternatively give each route a priority, regardless of the order, however I think It would make more sense to keep them in a natural order to save confusion in the future.
While I'm here
I mentioned in my comment you incorrectly used a Literal route rather than a Segment. You have this same issue with the the Products route definition. If a route is literal it will only match on the exact value (so you would need /[:id] in the URL).
/application/admin/products[/:id]
Therefore you will also need to update this route to a segment in order for the product id to be regarded as a parameter.
Lastly, there is a easier way of constructing URL's in the view, using the URL view helper. Your view script code would be changed to
<?php
$type = 'mandan';
$id = $coreg['idProduct'];
?>
<a href="<?php echo $this->url('productsList', compact('type', 'id')); ?>">
<img height='20' src="/img/mail-recevoir-32.png" alt='products' />
</a>

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)
}

Render header with /abc/abc/abc php

I have experience with php but i'm new to the zend frameworks. Could some one please give me some leads as to how I can get a site to work ie- www.facebook.com/nathandoe/photos
.
Using controllers how can i create a function that accepts different names and direct it that page ?
if i have a site www.hello.com/profile
instead of using GET www.hello.com/profile?is=342342?photos
how to handle it as www.hello.com/profile/342342/photos
i have controllers that handle the index and i know how to create different functions .. im stuck with www.hello.com/profile/xxxxxxxx/photos that part
In your module/[Module Name]/config/module.config.php, you can set the routes.
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
Something like
'route' => '/profile/[/:id]/photos',
Reference http://framework.zend.com/manual/2.0/en/user-guide/routing-and-controllers.html

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