Regex Routing - my rule is not found - regex

I'm quite a beginner with Zend Framework so I would much appreciate your help to understand why my regex routing rule is not found.
Here is the rule:
// Picture: www.mywebsite.com/gallery/12/pic/45/my-beautiful-picture.html
$router->addRoute('picture',
new Zend_Controller_Router_Route_Regex(
'gallery/(\d+)/pic/(\d+)/([A-Za-z0-9.]+)',
array(
'module'=>'frontoffice',
'controller'=>'gallery',
'action'=>'picture'
),
array(
1 => 'gallery_id',
2 => 'picture_id',
3 => 'title'
),
'gallery/%d/pic/%d/%s'
)
);
When I visit the page www.mywebsite.com/gallery/1/pic/9/my-beautiful-picture.html I get this error message:
An error occurred
Page not found
Exception information:
Message: Action "1" does not exist and was not trapped in __call()
Stack trace:
#0 /usr/local/zend/share/ZendFramework/library/Zend/Controller/Action.php(518): Zend_Controller_Action->__call('1Action', Array)
#1 /usr/local/zend/share/ZendFramework/library/Zend/Controller/Dispatcher/Standard.php(295): Zend_Controller_Action->dispatch('1Action')
#2 /usr/local/zend/share/ZendFramework/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#3 /usr/local/zend/share/ZendFramework/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#4 /usr/local/zend/share/ZendFramework/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#5 /var/www/misterjules/public/index.php(27): Zend_Application->run()
#6 {main}
Request Parameters:
array (
'controller' => 'gallery',
'action' => '1',
'pic' => '9',
'module' => 'frontoffice',
)
My other Regex Routing rules (written before in the script) which work fine are as follows:
// *** GALLERY ***
// Galleries (index): www.mywebsite.com/gallery
$router->addRoute('galleries',
new Zend_Controller_Router_Route_Regex(
'gallery(/page=(\d+))?',
array(
'module'=>'frontoffice',
'controller'=>'gallery',
'action'=>'galleries'
),
array(
2 => 'page',
),
'gallery'
)
);
// a gallery index: www.mywebsite.com/gallery/13/my-travel-in-dublin.html
// OR www.mywebsite.com/gallery/13/my-travel-in-dublin.html/page=4
$router->addRoute('gallery',
new Zend_Controller_Router_Route_Regex(
'gallery/(\d+)/([A-Za-z0-9.]+)(/page=(\d+))?',
array(
'module'=>'frontoffice',
'controller'=>'gallery',
'action'=>'gallery'
),
array(
1 => 'gallery_id',
2 => 'title',
4 => 'page'
),
'gallery/%d/%s'
)
);
I don't understand where my mistake is.
Thanks for your help.
Jules

It appears that your regex is missing a dash which it needs in order to match gallery/12/pic/45/my-beautiful-picture.html.
Try this:
$router->addRoute('picture',
new Zend_Controller_Router_Route_Regex(
'gallery/(\d+)/pic/(\d+)/([A-Za-z0-9.-]+)', #etc.

Related

ZF2 Routing regexp

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

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

Cakephp validating if my user checked at least one option

I have a problem validating if my user checked at least one option from a list of checkboxes.
Here is what i tried:
My view looks like this:
echo $this->Form->input('market_segment_targeted', array(
'multiple' => 'checkbox',
'label'=>array('text' => 'Market segment targeted', 'class'=>'w120'),
'options' => array(
'Home users' => 'Home users',
'SOHO' => 'SOHO',
'SMB' => 'SMB',
'Enterprise' => 'Enterprise'
),
));
In my controller i have added this snippet of code:
$validate_on_fly = array(
'market_segment_targeted' => array(
'notEmpty' => array(
'rule' => array('multiple', array('min' => 1)),
'required' => true,
'message' => 'Please select at least one!'
))
)));
$this->Partner->validate = Set::merge(
$this->Partner->validate,
$validate_on_fly
);
Any ideas what am i doing wrong?
Thank you
In CakePHP you can use Model Validation for checkboxes. Here is a quick example.
Your Form can look like:
$this->Form->create('User');
$this->Form->input('User.agree', array('type'=>'checkbox', 'hiddenField'=>false, 'value'=>'0'));
$this->Form->submit('Save'):
$this->Form->end();
Then in your Model under public $validate, use:
'agree'=>array(
'Not empty'=>array(
'rule'=>array('comparison', '!=', 0),
'required'=>true,
'message'=>'You must agree to the ToS'
)
)

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.