Zend Framework 2 navigation active class with params - php

Hi I have create a route like this
'webb' => array(
'type' => 'Segment',
'options' => array(
'route' => '/oferta/[:url1[/:url2][/:url3][/:url4]]',
'defaults' => array(
'controller' => 'webb',
'action' => 'index',
),
),
So my route is looking for paremeters url1 url2 etc
I have created navigation like this (just one item to show)
'page-3' => array(
'label' => 'example',
'route' => 'webb',
'controller' => 'index',
'params' => array('url1' => aa', 'url2' => bb', 'url3' => 'cc'),
)
and when I am rendering it using zend navigation helper everything is working fine despite that the current class of item is not changing to active? Any ideas? could it be done this way or when I am passing params zend navigation will not change class?
Bur url is bulid fine i get something like this /oferta/aa/bb/cc
Please help

Remove controller key from navigation array and it should work ('controller' => 'index' is wrong anyway).
From zend.navigation.pages.mvc
Note that when using the route property in a page, you do not need to specify the default params that the route defines (controller, action, etc.).

Related

how to change the controller name in zend with under score or dash?

how to change the controller name in URL in zend-framework ?.
if my URL is
http://www.diamondhedge.com/dev_new/public/diamondeducation/carat but
i want to change to like this
http://www.diamondhedge.com/dev_new/public/diamond-education/carat.
you don't need to change the controller name to change the url structure, you can change the route configuration though here is an example
in your module.config.php under the routes put something like this
'myroute' => array(
'type' => 'Segment',
'options' => array(
'route' => '/dev_new/public/diamond-education/carat.',
'defaults' => array(
'controller' => 'Module\Controller\Index',
'action' => 'myaction',
)
)
),

ZF2- Dynamic base route

I'm trying to create a dynamic route in a ZF2 project. It will be something like "domain.com/companyurl/products". The company url is dynamic. I did it:
'company' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:company]',
'defaults' => array(
'controller' => 'IndexController',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
...
),
),
But I always have to pass the company parameter in a route.
$this->url('company/products', array('company' => 'companyurl'));
Is there some way to specify a base route at the runtime, like a base url, then all route will follow it? Something like this:
$this->url('products');
or
$this->url('company/products');
In the both cases I already specified the base route value.
I hope you understand what I mean. Thanks.
There is a $reuseRouteParams option that you can use in the URL helper:
$this->url($name, $params, $options,$reuseMatchedParameters);
If you set this to true it will reuse the previous route match value of companyUrl.
You can read more on this in the docs here.

Zend Framework 2, redirecting to route and passing a variable to new controller

I implemented a form for placing new orders in Zend Framework 2 and after submitting the form I should redirect to another route and take the orders.id variable in another controller.
I tried using $this->redirect()->toRoute('confirm', array('param'=>$orderId)); but it is not working at all.
Maybe I do not know how to get that parameter in another confirmAction controller.
Please give me some examples. Thank you very much.
1) Since this is a routing issue, show what you have for the route in the module.config.php file. You might not have the "param" constraint configured properly in your config if I had to guess.
It should look something like this:
'confirm' => array(
'type' => 'segment',
'options' => array(
'route' => '/controller_name/confirm[/][:param][/]',
'constraints' => array(
'param' => '[0-9]*'
),
'defaults' => array(
'__NAMESPACE__' => 'your_namespace', // ex. Application\Controller
'action' => 'confirm', // or whatever action you're calling
'controller' => 'controller_name' // ex.
),
),
),

Zend 2 Accessing different url from controller

Is there a way to access a url that is different to the controller name?
Say, for my real life example, that I want to access this url http://www.example.com/men with men being a category. My controller would be CategoryController and it would display information from a database that would be appropriate to whatever category had been selected.
The reason I need it like this is because I need to support multiple sites so I can't have a controller for each category. Is there a way to do this? I'm fairly new to the 2 Framework but have decent knowledge of 1.12. Thanks.
If I undestand you correctly, you may resolve your task by routing configuration.
Module's module.config.php file:
return array(
'router' => array(
'routes' => array(
'<ROUTE_NAME>' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/:category',
'constraints' => array(
'category' => '<CATEGORY_PATTERN>', // usually [a-zA-Z0-9-_]+
),
'defaults' => array(
'__NAMESPACE__' => '<CategoryController Namespace>',
'controller' => 'Category',
'action' => '<Controller method without "action" postfix>',
),
),
),
),
),
);
Inside controller category accessed by:
$category = $this->params()->fromRoute('category');

ZF2 Friendly URL + SEO

I have two questions for you:
1) I'm developing my website in ZF2, and I want to make a friendly URL like:
domain.com/ADFE4
domain.com/RURUR
domain.com/UYRRTG
with a variable second part. The problem is that ZF2 interpret this second url like a controller. How could I send this url parameter to index action in the index controller?
2) This way to make the site, could be bad for SEO? Not for the friendly url,
but the same controller for manage the route.
try to use router like this:
'friendlyPage' => array(
'type' => 'segment',
'options' => array(
'route' => 'sometext-[:param]',
'constraints' => array(
'param' => '[0-9]+',
),
'defaults' => array(
'controller' => 'ControllerName',
'action' => 'ActionName',
),
),
),

Categories