zf2 routing issue with segments - zend-framework2

I try to route with multiple segments like the below
htpro/doc/appointments/15/10/2014
'doc' => array(
'type' => 'segment',
'options' => array(
'route' => '/doc[/:action][/:app_day][/:app_month][/:app_year]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'doc\Controller\Doc',
'action' => 'appointments',
),
),
),
but i am getting the follwoing error, what might be the issue
The requested URL could not be matched by routing.

try this :
'doc' => array(
'type' => 'segment',
'options' => array(
'route' => '/htpro/doc[/:action][/:app_day][/:app_month][/:app_year]', //add /htpro
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'app_day' => '[0-9]*', // add this
'app_month' => '[0-9]*', // add this
'app_year' => '[0-9]*', // add this
),
'defaults' => array(
'controller' => 'doc\Controller\Doc',
'action' => 'appointments',
),
),
),
but you still need to define your costum constraints.

Related

The requested URL could not be matched by routing. ZF2

This is the error that i am getting. I am learning zf2 from a book but they forget to say where i should put this piece of code (i tried few attempts but none of them wokred.)
This is my module.config.php
<?php
return array(
'controllers' => array(
'invokables' => array(
'Users\Controller\Index' =>
'Users\Controller\IndexController',
'Users\Controller\Register' =>
'Users\Controller\RegisterController',
'Users\Controller\Login' =>
'Users\Controller\LoginController',
'Users\Controller\UserManager' =>
'Users\Controller\UserManagerController',
),
),
'router' => array(
'routes' => array(
'users' => array(
'type' => 'Literal',
'options' => array(
'route' => '/users',
'defaults' => array(
'__NAMESPACE__' => 'Users\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(),
),
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'users' => __DIR__ . '/../view',
),
),
);
Where should this be ?
'user-manager' => array(
'type' => 'Segment',
'options' => array(
'route' => '/User-manager[/:action[/:id]]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Users\Controller\UserManager',
'action' => 'index',
),
),
),
The url is
http://zend2.com/users/user-manager/edit/5
Thank you !
It should be in the 'child_routes' array instead of the default route. which should depending if you want to use it should be on the same level as 'users'
Also in the route the address is written with a uppercase U in User-manager this is case sensative so you should write ur address as http://zend2.com/users/User-manager/edit/5

Zend Framework 2 routing action

I've recive an error when run I run "localhost/products/edit"
what's wrong I have done ? Ofcourse I have function edit in
Product controller classand edit.html in view.
'products' => array(
'type' => 'Literal',
'options' => array(
'route' => '/products',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Products',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/products[/:action]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController',
'Application\Controller\Products' => 'Application\Controller\ProductsController'
)
),
);
Try this as your child roots array:
'child_routes' => array(
'view' => array(
'type' => 'segment',
'options' => array(
'route' => '/:id',
'constraints' => array(
'id' => '[0-9]+',
),
'defaults' => array(
'action' => 'view',
),
),
'may_terminate' => true,
'child_routes' => array(
'actions' => array(
'type' => 'segment',
'options' => array(
'route' => '/:action',
'constraints' => array(
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Application\Controller\ProductsController,
'action' => 'view', ),
),
),
),
),
),
Also it needs to be localhost/products/[:id]/edit like localhost/products/1/edit not localhost/products/edit I would imagine anyway but I don't know what your doing to be fair. Just remove the id parameter and constraint and it should work
Notice child root actions do not have the route repeated as it is inherited... your route would create something like /products/products/edit

Zend framework edit a record

I am trying to create edit records functionality in my listing page. But i am getting following error when clicking on edit link against record.
A 404 error occurred
Page not found.
The requested URL could not be matched by routing.
No Exception available
My module.config.php file code for edit view is :
'edit' => array(
'type' => 'literal',
'options' => array(
'route' => '/album[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Application\Controller\Album',
'action' => 'edit',
),
),
),
And my Album listing page code for calling edit controller and passing id to is :
<a href="<?php echo $this->url('edit',
array('action'=>'edit', 'id' => $album->id));?>">Edit</a>
and editAction code is :
$id = (int) $this->params()->fromRoute('id', 0);
Please let me know what i m missing. I am new to zend framework.
i think you have to use 'type' => 'segment' have a look on the documentation
<?php
'edit' => array(
'type' => 'segment', /* <--- use segment*/
'options' => array(
'route' => '/album[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Application\Controller\Album',
'action' => 'edit',
),
),
),
Advise regarding the type option is correct, however you will also need the may_terminate option to ensure that the router knows that this route can be matched.
'edit' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Application\Controller\Album',
'action' => 'edit',
),
),
'may_terminate' => true, // Add this line
),
See now my route script in module.config.php is this.
'album' => array(
'type' => 'literal',
'options' => array(
'route' => '/album/',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Album',
'action' => 'album',
),
),
'may_terminate' => true,
'child_routes' => array(
'add' => array(
'type' => 'Segment',
'options' => array(
'route' => '/album/[:add]',
'constraints' => array(
'add' => '[a-zA-Z0-9_-]+'
),
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Album',
'action' => 'add',
),
),
),
'edit' => array(
'type' => 'Segment',
'options' => array(
'route' => '/album/[:edit][/:id]',
'constraints' => array(
'edit' => '[a-zA-Z0-9_-]+'
),
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Album',
'action' => 'edit',
),
),
),
),
),
in this case if i am clicking on any link only the album page displaying not other which i am hitting.
Finally i got solution and now my application is working perfectly....
I just replace above code with :
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Application\Controller\Album',
'action' => 'album',
),
),
),

hide module and action name from zf2 routing

<?php
return array(
'controllers' => array(
'invokables' => array(
'Member\Controller\Member' => 'Member\Controller\MemberController',
),
),
'router' => array(
'routes' => array(
'member' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Member\Controller\Member',
'action' => 'index',
),
),
),
'member' => array(
'type' => 'segment',
'options' => array(
'route' => '[/:action][/:pkMemberId][/:status]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'pkMemberId' => '[0-9]+',
'status' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Member\Controller\Member',
'action' => 'index',
),
),
),
'changeMemberPassword' => array(
'type' => 'segment',
'options' => array(
'route' => '/changeMemberPassword',
'defaults' => array(
'controller' => 'Member\Controller\Member',
'action' => 'changePassword',
),
),
),
),
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'aliases' => array(
'translator' => 'MvcTranslator',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
);
?>
i have a route like this http://www.abc.com/member-profile/756 but i want to show it like this http://www.abc.com/john. showing user first name in the url. Thanks
If you want to use firstname in url by hiding module and action from url, you can do this by add routing as below:
'member' => array(
'type' => 'segment',
'options' => array(
'route' => '[/:firstname][/:pkMemberId][/:status]',
'constraints' => array(
'firstname' => '[a-zA-Z][a-zA-Z0-9_-]*',
'pkMemberId' => '[0-9]+',
'status' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Member\Controller\Member',
'action' => 'view',
),
),
),
You need to call this by adding this line:
harish
you need to use a unique/distinct first name for every member to identify the user or provide an id in parameter to identify the member.

Customize the Zend Framework 2 Routers

I have started the application development with zend framework 2.
For instance, I have the Module named as 'Album'.
And i have the following codes in module.config.php.
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Album\Controller\Index',
'action' => 'index',
),
),
),
'album' => array(
'type' => 'Segment',
'options' => array(
'route' => '/album',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'__NAMESPACE__' => 'Album\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
),
),
),
),
),
),
),
);
I can access the following urls.
ZF2/album
ZF2/album/album/index
I like to route the url 'ZF2/album/album/view' into ZF2/view-album.
How can i route? Help me.
Thanks.
I got the access with the following codes.
'view-album' => array(
'type' => 'Segment',
'options' => array(
'route' => '/view-album[/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'__NAMESPACE__' => 'Album\Controller',
'controller' => 'Index',
'action' => 'view-album',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
),
),
),
),
),

Resources