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.
Related
I typically format my routes in ZF2 like so: /name/to/route
Now I have been doing the same thing with my api routes however I am finding that I am struggling to include data such as encoded urls or arrays.
Here is an example of such a route:
http://example.com/api/register/access/code/c102dea422fa4bb6958d77a29d9873d2/http%3A%2F%2Frouter-local.example.com%2Fapi%2Fdirectory
The following represents forward slashes and thus causes the route not to work: %3A%2F%2
I am thinking I should encode my route as such:
http://example.com/api/register/access/code/?access_code=c102dea422fa4bb6958d77a29d9873d2&route=http%3A%2F%example.com%2Fapi%2Fdirectory
How do you configure the module.config file to deal with this?
Currently it is set as such in apigility:
'api.rpc.register-access-code' => array(
'type' => 'Segment',
'options' => array(
'route' => '/api/register/access/code/:access_code/:route',
'defaults' => array(
'controller' => 'Api\\V1\\Rpc\\RegisterAccessCode\\Controller',
'action' => 'registerAccessCode',
),
),
),
EDIT
I have encoded my routes to include GET parameters by doing the following:
$url = "http://example.com/api/register/access/code/";
$params = [
'access_code' => 'c102dea422fa4bb6958d77a29d9873d2',
'route' => 'http://example.com/api/directory'
];
$final = $url . "?" . http_build_query($params);
Which gives this:
http://example.com/api/register/access/code/?access_code=c102dea422fa4bb6958d77a29d9873d2&route=http%3A%2F%2Fexample.com%2Fapi%2Fdirectory
However this breaks due to a "The requested URL could not be matched by routing." error.
The route is unidentified due to the interpretation of the slashes in the included URL.
Perhaps the issue is to do with how the URL is formatted and included as a parameter?
You don't define query variables in the segment route option; only the path.
You may append ?query=vars to any url, regardless of route configuration. ZF2's url helpers should encode the query vars for you, you just have to create an array of query vars and give it to the helper function when creating a url.
<?php echo $this->url('api.rpc.register-access-code', array(), array('query' => array(
'access_code' => 'c102dea422fa4bb6958d77a29d9873d2',
'route' => 'http://router-local.example.com/api/directory',
))); ?>`
In this case, it would seem the problem is to do with htaccess or apache. The simplest solution has been to encode the url using: base64_encode($url) which can be de-coded at the other end.
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
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.
I'm a bit confused about params in ZF2 routing. Here's the literal route:
'route-test1' => array(
'type' => 'literal',
'options' => array(
'route' => '/my/route',
'defaults' => array(
'controller' => 'IndexController',
'action' => 'myRoute',
),
),
),
Should that route catch only:
example.com/my/route
Or also:
example.com/my/route?test1=aaa&test2=bbb
example.com/my/route?test=aaa
RESOLUTION:
Tested id (dunno why I haven't done it int he first place) - and yes - it will match all of these urls - with or without query line.
Yes. Then you can access the arguments (from a controller, for example) with:
$this->getRequest()->getQuery()
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.