Zend framework 2, pagination routing issue - zend-framework2

I have issue on zend pagination and routing in zf2 . I would like to display details of feedback item , and list of its sub items ( actions ) on the same page . My route code is given below
$routes['dashboard_inbox_actions'] = array(
'type' => 'segment',
'options' => array(
'route' => '/dashboard/inbox/detail[/:feedback[/actions/page/:page]]',
'constraints' => array(
'feedback' => '[0-9]+',
'page' => '[0-9]+',
),
'defaults' => array(
'__NAMESPACE__' => 'Dashboard\Controller',
'controller' => 'inbox' ,
'action' => 'detail',
'feedback' => 0 ,
'page' => 1
),
),
);
I pass url like
/dashboard/inbox/detail/4
in listing page , for rendering the provided pages of subitems .
<?php echo $this->paginationControl($this->paginator, 'Sliding' ); ?>
which creates paging urls , with feedback id as 0 ( it my issue )
/dashboard/inbox/detail/0/actions/page/2
/dashboard/inbox/detail/0/actions/page/3
I manually paste url
/dashboard/inbox/detail/4/actions/page/2
Its shows page 2 as active item . My controller code works fine and gives me result , but still paginationControl creates url with feedback id 0.

You need to use the fourth parameter of the paginationControl view helper:
<?php
echo $this->paginationControl($this->paginator, 'Sliding',
'my_pagination_control', array('route' => 'paginator_route'));
?>
You can pass through parameters to the view partial, for example pass through your route name so you can generate yoru links using the correct route.
then inside your view partial you can use this in the url helper:
<?php echo $this->url($this->route, array('page' => $this->first), FALSE, TRUE) ?>
see: http://framework.zend.com/manual/2.0/en/modules/zend.view.helpers.html#url-helper
where you can see the url helper can use currently matches params:
url($name, $urlParams, $routeOptions, $reuseMatchedParams)
Setting $reuseMatchedParams to true will allow the use of the current matched params as default values.

Related

Where can I get the route action in zend framework

This is my module.config.php
return [
'router' => [
'routes' => [
'home' => [
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => [
'route' => '/',
'defaults' => [
'controller' => 'rotation',
'action' => 'add',
],
],
],
And this is my add.phtml.
<?php
$form = $this->form;
$form->setAttribute('action',
$this->url('home/default', //your route name ...
array('controller'=>'rotation', 'action' => 'add')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formRow($form->get('profilename'));
echo $this->form()->closeTag();
In line $this->url('home/default', //your route name ...
array('controller'=>'rotation', 'action' => 'add'))); I get a error. It doesn't display anything but when I erased that line it displays the textboxes.
My question is the url I put inside the code is correct or wrong? Thanks
For one thing to generate that route url with url helper you don't need pass in any parameters, because you've specified them in the defaults under options. Also since it's a literal I don't think you could even change the action (it's not variable for this type of route).
Sidenote: you don't need to pass in the whole route class name, because ZF2 already has it registered under Literal (case-insensitive), so you can shorten the value of type key.
$this->url('home') should return the result you're expecting.

how to define which module to fire first in Zend Framework 2

How to define which module to fire first in Zend Framework 2, I need this as I have two modules created 1>front and another is 2>admin and my main folder is mysite , so my requirement is when I fire:
http://example.com
it should fire front module.
I'm unable to figure how to call the front modules controller.
I'm using Zend Framework 2 for first time.
In any module.config.php -
set the route as -
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/', // due to '/' - http://mysite.net will call the below module's controller - action
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller', //Set the front module name here
'controller' => 'Index', //set the front module's controller name
'action' => 'index', //here the action name
),
),
),
//..... some other routes
)
)
It will work fine. Let us know the result to help further.

Adding parameters to URL in ZF2

I am trying to construct url which looks like this:
abc.com/folder?user_id=1&category=v
Followed the suggestion given in this link:
How can you add query parameters in the ZF2 url view helper
Initially, it throws up this error
Query route deprecated as of ZF 2.1.4; use the "query" option of the HTTP router\'s assembling method instead
Following suggestion, I used similar to
$name = 'index/article';
$params = ['article_id' => $articleId];
$options = [
'query' => ['param' => 'value'],
];
$this->url($name, $params, $options);
Now, I am getting syntax error saying,
Parse error: syntax error, unexpected '[' in /var/www/test/module/Dashboard/view/dashboard/dashboard/product.phtml on line 3
My module.config.php is configured like this:
'browse_test_case' => array(
'type' => 'Literal',
'options' => array(
'route' => '/browse-case[/:productId]',
'defaults' => array(
'controller' => 'Test\Controller\Browse',
'action' => 'browse-test-case',
),
),
'may_terminate' => true,
'child_routes' => array(
'query' => array(
'type' => 'Query',
),
),
),
Any Idea, please help!
Your url view helper call is wrong. First parameter is the name of a defined route, second parameter is an array with your route parameters. Your array syntax should also be correct. e.g.
array("param1" => "value1", "param2" => "value2")
In your example the correct url view helper call should be something like this:
$this->url('browse_test_case', array('productId' => '1'));
...in which "1" could be an database table row identifier, for example.
The Query child-route in your route definition, allows you to use also other url paramaters, than specified within the route. But each of these child-routes start with "/browse-case[/:productId]".
There you find the reference example of ZF2:
http://framework.zend.com/manual/2.3/en/modules/zend.view.helpers.url.html#zend-view-helpers-initial-url

Zend Framework 2 - Segment Route

I'm building an app using Zend Framework v2.2.0 and I'm creating different modules for each section.
In a module, called Company, there is this route:
'company_wines' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/:slug_company/:action/',
'constraints'=>array(
':slug_company'=>'[a-zA-Z0-9\-_]+',
':action'=>'(wines|red\-wines|white\-wines|sparkling\-wines|dessert\-wines|rose\-wines){1}',
),
'defaults' => array(
'controller' => 'Company\Controller\Company',
),
),
),
In another module, called Vineyard, I have this route:
'vineyard_page' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/vineyard/:slug_vineyard/',
'constraints'=>array(
':slug_vineyard'=>'[a-zA-Z0-9\-_]+',
),
'defaults' => array(
'controller' => 'Vineyard\Controller\Vineyard',
'action' => 'vineyard',
),
),
),
When I test with url domain.ext/Company-name/red-wines/ or domain.ext/Company-name/white-wines etc, the Company controller is invoked.
If I test with domain.ext/vineyard/Vineyard-name/, the Vineyard controller is not invoked, is still invoked the Company one and the error message say that the controller cannot dispatch the request. Off course there is no method called VineyardnameAction() in CompanyController class.
I was expecting that the route match against the list of values specified on regex for :action, also if the :slug_company regex match the "flag" vineyard, then there is no action that match the Vineyard-name part...
If I test the :action regex with preg_match_all, nothing is found in a string like domain.ext/vineyard/Vineyard-name/.
If I disable Company module or delete the the company_wines route, vineyard route is working.
I've solved creating different routes for each wines types, but I would like to understand whath I'm doing wrong :)
Your syntax is wrong:
'constraints' => array(
'slug_vineyard'=>'[a-zA-Z0-9\-_]+',
),
'constraints'=>array(
'slug_company'=>'[a-zA-Z0-9\-_]+',
'action'=>'(wines|red\-wines|white\-wines|sparkling\-wines|dessert\-wines|rose\-wines){1}',
),
remove the colon from the default / constraints section and it should work fine.
As you have put the colon in there the constraints aren't being forced so default constraints will be used, which ever route comes first will match.

Zend Framework 2 (ZF2) dynamic locale

I am looking for a way to add translations to my ZF2 application, using globals in my URL.
Is there anyway to do this for the whole application at once?
A typpical URL would look like this: http://domain.com/en_GB/user/index
The first part (en_GB) should be used to show the correct translation.
Besides that, it would be nice, if it is possible to set this router part optional.
So, if I should go to http://domain.com/user/index (without the locale part) to my application, it should automatically take the browser locale.
I hope I am clear enough, if any additions are needed to this question, feel free to ask.
Thanx in advance
#DrBeza,
Thank you for your answer. I don't know if this is the correct way, but I created the next solution:
in /config/global.php I added this part
'translator' => array(
'locale' => 'nl_NL',
'translation_file_patterns' => array(
array(
'type' => 'phpArray',
'base_dir' => __DIR__ . '/../../language',
'pattern' => '%s.php',
),
),
),
in /module/[modulename]/config/module.config.php I added this part to set the first part of the URL containing the locale
'router' => array(
'routes' => array(
'user' => array(
'options' => array(
'route' => '[/:lang]/user[/:action][/:id]',
'constraints' => array(
'lang' => '([a-z]{2})+(_)+([A-Z]{2})',
),
),
),
),
),
in /config/local.php I added this code to fetch the locale from the URL:
http://domain.com/[locale][module][controller]
$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segment = explode('/', $_SERVER['REQUEST_URI_PATH']);
And I added this part to load the locale dynamicly:
return array(
'translator' => array(
'locale' => $segment[1],
);
I would suggest extending the Segment route class and adding in the optional locale constraint and segment part if missing. Call the optional variable something application wide such as 'locale'.
Then create a 'route' event in the main module bootstrap, this event will fire once a route has been matched. The callback function that is fired will have access to the RouteMatch object through the passed event, letting you gain access to the 'locale' value. Then you can do some checks and set the application locale.

Resources