ZF2- Dynamic base route - php

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.

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 Restful hierarchical routes

I'm trying to use a hierarchical resource in ZF2 for a Restful API. The resource should looks like clients/1/addresses. What I've tried was this
'clients' => array(
'type' => 'segment',
'options' => array(
'route' => '/clients[/:id]',
'constraints' => array(
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Api\Controller\ClientController',
),
),
'may_terminate' => true,
'child_routes' => array(
'addresses' => array(
'type' => 'segment',
'options' => array(
'route' => '/addresses[/:address_id]',
'constraints' => array(
'address_id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Api\Controller\AddressController',
),
),
),
),
),
There is this conflict of both id's, but I don't know if I rename the route identifier id of the resource addresses like I did will solve it. Anyway, the real problem is that the route clients/1/addresses calls the get method of the AddressController, not the getList, and I think that's because Zend understands that the id of the client belongs to addresses, so its calls the get method.
Do you know how to deal with this?
You are probably right that get is called instead of getList because of the id being present in your route match parameters and the controller by default uses 'id' for matching the route identifier.
The way to deal with this is that you give the route identifiers names that fit the resource. So for client you make client_id and for address you use address_id (like you already did).
And then you configure your AbstractRestfulController instance to "look" for the correct route identifier using the setIdentifierName method:
$clientController->setIdentifierName( 'client_id' );
$addressController->setIdentifierName( 'address_id' );
This is just an example, the best way to do this is (of course) by using a controller factory...

Zend Framework 2 navigation active class with params

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

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.
),
),
),

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