ZF2 Routing regexp - regex

I try to validate a route in zend framework 2.
Example:
valid - /foo/overview/1-2015
valid - /foo/overview/9999-2015
invalid /foo/overview/sd-2015
invalid - /foo/overview/10001-2015
I tried this code, but I received 404 error:
'overview' => array(
'type' => 'segment',
'options' => array(
'route' => '/foo/overview/:nr',
'constraints' => array('nr' => '^[0-9]{1,4}-[0-9]{4}$',),
'defaults' => array(
'controller' => 'Foo\Controller\Foo',
'action' => 'overview',
),
),
),
thanks

It is enough to use [0-9]{1,4}-[0-9]{4} for your constraint value, since the starting ^ and the closing $ are automatically added by the framework (check the match function in the Zend\Mvc\Router\Http\Segment class).

Related

Regex Input Filter in zend framework 2

i add input filter by this parameters :
and the question is why zend framework 2 have problem with utf8 pattern
$formInputFilter->add($inputFactory->createInput(array(
'name' => $field->attribute->id,
'required' => true,
'validators' => array(
array(
'name' => 'Regex',
'options' => array(
'pattern' => '/^[0-9,\x{0600}-\x{06FF}]*/u',
'messages' => array(
'regexNotMatch'=>'number is not correct'
),
),
),
),
)
)
);
the error is :
preg_match(): Compilation failed: character value in \x{} or \o{} is too large at offset 15
I use this in my urls. It works with any digits and characters
'\/^\d{4},(?:\s|\w)+\/u*'
https://regex101.com/r/mW4xJ4/2
\/^\d(?:\s|\w)+\/u*
You can see the explanations of each character here: https://regex101.com/r/mW4xJ4/1

ZF2 Segment Route doesn't match parent's constraint when using children

I created the following routes:
'relatorios' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/relatorios/:tipo',
'defaults' => array(
'controller' => 'Relatorios',
'action' => 'index',
'tipo' => 'normais',
),
'constraints' => array('tipo' => '(normais|administrativos)$',
),
'may_terminate' => true,
'child_routes' => array(
'view' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/view/:id_relatorio',
'defaults' => array('action' => 'view'),
'constraints' => array('id_relatorio' => '[0-9]+'),
),
),
),
);
This is supposed to match the following routes:
/relatorios/normais //matches
/relatorios/administrativos //matches
/relatorios/normais/view/1 //doesn't match
/relatorios/administrativos/view/1 //doesn't match
So basically the :tipo parameter must be either normais or administrativos and there is a child /view/any_digits.
When the parent route is called it matches, when the child view is called it doesn't match because of my tipo constraint. Why is that?
As per my comment, the $ on the constraint means 'end of the string' (which in this case is the URL path), so it shouldn't be there.

Doctrine- unknown database type enum requested

I am using doctrine 2 within zend framework 2. To generate entities using database table, the console command used is:
php doctrine-module orm:convert-mapping --force --from-database annotation ./export
When i run above command, it throws an error:
Unknown database type enum requested
How to solve this issue?
You can add:
'doctrine_type_mappings' => array(
'enum' => 'string'
)
in your global configuration file located in /config/autoload/global.php.
Example code:
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' =>'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => 'username',
'password' => 'password',
'dbname' => 'DevBrew',
),
// To automatically convert enum to string
'doctrine_type_mappings' => array(
'enum' => 'string'
),
)
)
)
);

ZendFramework: Simple Router_Regex problem

I have never been any good at regular expressions. I am trying to use them in building a simple site. I construct a URL just fine like /some-course/some-vtm-1, but when it tries to lookup the defined controller, it fails. Here is the route I have defined:
chapter' => array(
'type' => 'Zend_Controller_Router_Route_Regex',
'route' => '/:course/:vtm\-(\d+)',
'defaults' => array(
'module' => 'learn',
'controller' => 'chapter',
'action' => 'index'
),
'map' => array(
1 => 'course',
2 => 'vtm',
3 => 'id'
),
'reverse' => '%s/%s-%d/'
),
How should I correct this Regex so it finds the correct module/controller/action when I a link like /some-course/some-vtm-1 is clicked
Your problem is that you're trying to mix the syntax of Zend_Controller_Router_Route (named variables in the route starting with :) and Zend_Controller_Router_Route_Regex (bracketed regular expression patterns in the route). You want to drop the former and just use the regexp syntax, leaving you with something like this:
array(
'type' => 'Zend_Controller_Router_Route_Regex',
'route' => '([\w]+)/(vtm)-([\d]+)',
'defaults' => array(
'module' => 'learn',
'controller' => 'chapter',
'action' => 'index'
),
'map' => array(
1 => 'course',
2 => 'vtm',
3 => 'id'
),
'reverse' => '%s/%s-%d'
),

Zend Framework - Router Rewrite with Regex

I have been trying to shortern this route:
http://abc.localhost/user/view/index/id/1
to this:
http://abc.localhost/user/1
with the following portion of code in my bootstrap but I keep getting an error stating that the 'Reversed route is not specified', any ideas why?
$route = new Zend_Controller_Router_Route_Regex(
'user/(\d+)',
array(
'module' => 'user',
'controller' => 'view',
'action' => 'index'
),
array(
1 => 'id'
)
);
$router->addRoute('user', $route);
Thanks,
Martin
If you want to use the URL helper with Regex routes you need to pass a 4th parameter to Zend_Controller_Router_Route_Regex that it can use to rebuild the route. This 4th parameter should be a string in a sprintf-format which it can inject the params into.
In your case it would be something like:
$route = new Zend_Controller_Router_Route_Regex(
'user/(\d+)',
array(
'module' => 'user',
'controller' => 'view',
'action' => 'index'
),
array(
1 => 'id'
),
'user/%d'
);
$router->addRoute('user', $route);
There is some info on this right at the end of the manual section on Regex routes: http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.regex - but it's easy to miss.