Routing in the Zend Framework 2 - zend-framework

It's not long since I'm using Zend Framework 2 and I'm trying to write a route for a product that have the rule something like that:
/[slug]-test[ID][av|ai|cv|ci|svb|sib|svc|sic|svi|sii|tv|ti]
An exemple for this rule would look like this:
/freezer-in-good-shape-test12345cv
I try to this with REGEX:
'details' => array(
'type' => 'Zend\Mvc\Router\Http\Regex',
'options' => array(
'route' => '(?<slug>[a-zA-Z0-9_-]+)-test(?<id>[0-9]+)(?<sufix>(av|ai|cv|ci|svb|sib|svc|sic|svi|sii|tv|ti))',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'List',
'action' => 'details',
),
'spec' => '%slug%-test%id%%sufix%',
)
),
and with SEGMENT too:
'details' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:slug-test:id:sufix',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'List',
'action' => 'details',
)
'constraints' => array(
'slug' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]*',
'sufix' => '[av|ai|cv|ci|svb|sib|svc|sic|svi|sii|tv|ti]'
)
)
),
but it's not working. Can anyone tell what is that i'm doing wrong? Thanks!

I’d go with Segment here. The only issue I’ve noticed in your route config is the sufix constraint: instead of [av|ai|…] you should type (av|ai|…).

please try with this-
'details' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:slug[-test]:id:sufix',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'List',
'action' => 'details',
)
'constraints' => array(
'slug' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]*',
'sufix' => '[av|ai|cv|ci|svb|sib|svc|sic|svi|sii|tv|ti]'
)
)
),
on this line 'route' => '/:slug-test:id:sufix',
you had the issues, as it considers "slug-test" as single variable.
so for this you should separate "-test" with "slug" variable.
'route' => '/:slug[-test]:id:sufix',

Related

How to add a Restful controller in a Zend module

I'm actually working on a zend framework 2 application which should render two sorts of View :
a view model
a json model
I would include the restful controller in my module but I don't understand if it's possible or recommended.
If someone has been already confronted to a same case, does he could bring me some explanations because I'm searching an answer since 2 days fruitlessly.
In my module.config.php I have :
return array(
'controllers' => array(
'invokables' => array(
'Role\Controller\Role' => 'Role\Controller\RoleController',
'Role\Controller\RoleRest' => 'Role\Controller\RoleRestController',
),
),
'router' => array(
'routes' => array(
'role' => array(
'type' => 'segment',
'options' => array(
'route' => '/role[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Role\Controller\Role',
'action' => 'index',
),
),
),
'role-rest' => array(
'type' => 'segment',
'options' => array(
'route' => '/role-rest[/:id]',
'constraints' => array(
'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Role\Controller\RoleRest',
'action' => 'getList',
),
),
),
),
),
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy',
),
'template_path_stack' => array(
'role' => __DIR__ . '/../view',
),
),
);
and when i try to get myApplication.localhost/role-rest, the page displays the following message :
"The requested controller was unable to dispatch the request.
Controller:
Role\Controller\RoleRest"
I resolve the problem with a simple change in the id's regex constraint :
'constraints' => array(
'id' => '[a-zA-Z0-9_-]*',
),

How to set a variable in ObjectSelect criteria params in DoctrineModule

I'd like to know how to set a variable in ObjectSelect criteria params.
My code is as follow:
$this->add(
array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'shop',
'attributes' => array(
'class' => 'chosen-select form-control'
),
'options' => array(
'object_manager' => $this->objectManager,
'target_class' => '\Godana\Entity\Shop',
'property' => 'name',
'label' => 'Shop',
'label_attributes' => array(
'class' => 'col-sm-3 control-label',
),
'find_method' => array(
'name' => 'findBy',
'params' => array(
'criteria' => array('owner' => $this->shopOwner),
),
),
),
)
);
and it returns empty value but if I use a static value like 'criteria' => array('owner' => 1) it returns data from my db.
shouldn't it be $this->shopOwner->getId() ?

Routing with Zend Framework 2 Restful Webservice

I want to implement a RESTful webservice by using Zend Framework 2, more precisely 2.1.5. I got a 404 if I visit http://ehcserver.localhost/rest, the corresponding message is 'rest(resolves to invalid controller class or alias: rest)'. What went wrong?
You can see my source code in my github-repository:
https://github.com/Jochen1980/EhcServer/blob/master/module/Application/config/module.config.php
The route is defined like this:
return array(
'router' => array(
'routes' => array(
'rest' => array(
'type' => 'ZendMvcRouterHttpSegment',
'options' => array(
'route' => '/:controller[.:formatter][/:id]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'formatter' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]*'
),
),
),
'home' => array(
...
Your route doesn't define a namespace to which the controller belongs, you need to add a __NAMESPACE__ to route defaults
'rest' => array(
'type' => 'ZendMvcRouterHttpSegment',
'options' => array(
'route' => '/:controller[.:formatter][/:id]',
'defaults' => array(
// tell the router which namespace :controller belongs to
'__NAMESPACE__' => 'Application\Controller',
),
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'formatter' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]*'
),
),
),
Are you sure the type is valid?
type' => 'ZendMvcRouterHttpSegment',
to this
type' => 'Segment',

How to redirect with GET query in Zend 2?

I can redirect to matchable routes like $this->redirect()->toRoute('controller', array('action' => 'something', 'foo' => 'bar')), but how can I append a get query? I'm using it for a filter for a table. The filter can either be submitted on the page (form get method), or linked from another page (need to add get query).
try this :
'course' => array(
'type' => 'Segment',
'options' => array(
'route' => '/course[/:action[/:id]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '\d+'
),
'defaults' => array(
'__NAMESPACE__' => 'Home\Controller',
'controller' => 'course',
'action' => 'index',
'id' => 0
),
),
'may_terminate' => true,
'child_routes' => array(
'query' => array(
'type' => 'Query',
'options' => array(
'route' => '?[:filterByDepartment]'
),
),
),
),
make sure the route is met your need in the view :
echo $this->url('course/query', array('controller'=>'course', 'action'=>'index', 'id'=>0, 'filterByDepartment'=>'depA'));
if this is right, let's try the following in the controller action :
$this->redirect()->toRoute('course/query', array('controller'=>'course', 'action'=>'index', 'id'=>0, 'filterByDepartment'=>'depA'));

ZF2 routing configuration

I've just built a zf2 project and i'm having a configuration problem.
When i go to mydomain.com this routs as specified in configuration files to Application module, index controller, index action.
But If i type mydomain.com/otheraction this is not routed to Application module, index controller, otheraction action. Of course, since it isn't configured to do this.
So my question would be how do i configure the app to do that? I've added my Application/config/module.config.php file and the main config file. Thank you!
module.config.php
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* #link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* #copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* #license http://framework.zend.com/license/new-bsd New BSD License
*/
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
// The following is a route to simplify getting started creating
// new controllers and actions without needing to create a new
// module. Simply drop new controllers in, and you can access them
// using the path /application/:controller/:action
'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]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController'
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
application.config.php
<?php
return array(
'modules' => array(
'Application',
'ZfcBase',
'ZfcUser',
),
'module_listener_options' => array(
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
),
'module_paths' => array(
'./module',
'./vendor',
),
),
);
The default route configured is to accept any url of this format, this is the standard format
mydomain.com/
mydomain.com/applicatiom/controller/action (standard format)
in your case mydomain.com/otheraction the router expect a module otheraction, but its not present.
if you need to have a route as specified above have a entry under route
'otheraction' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/otheraction',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'otheraction',
),
),
),
In the routes subarray, add a next entry similar to the 'home' entry, for example:
'user' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/user',
'defaults' => array(
'controller' => 'Application\Controller\User',
'action' => 'some-action',
),
),
),
That would route yourpage.com/user to someActionAction in your UserController.