zf2&zfc2 and routing - zend-framework2

I want to create admin panel using ZfcAdmin module. I want to create routing, to manage users. Here is it:
<?php
return array(
'controllers' => array(
'invokables' => array(
'AdminUser\Controller\AdminUser' => 'AdminUser\Controller\AdminUserController',
),
),
'view_manager' => array(
'template_path_stack' => array(
'admin-user' => __DIR__ . '/../view',
),
),
'router' => array(
'routes' => array(
'zfcadmin' => array(
'may_terminate' => true,
'child_routes' => array(
'user' => array(
'type' => 'segment',
'options' => array(
'route' => '/user',
'defaults' => array(
'controller' => 'AdminUser\Controller\AdminUser',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' =>array(
'edit' => array(
'type' => 'segment',
'options' => array(
'route' => '/edit/:user_id',
'defaults' => array(
'controller' => 'AdminUser\Controller\AdminUser',
'action' => 'edit',
),
),
),
),
),
),
),
),
),
);
I based on info from zfcadmin github page: https://github.com/ZF-Commons/ZfcAdmin. I just copy and paste the example, and change to fix my needs. However, I recieve a error message:
"Fatal error: Uncaught exception 'Zend\Mvc\Router\Exception\RuntimeException' with message 'Part route may not terminate'"
What's wrong?
EDIT:
I request: /admin/user and it's ok, but when I want to receive URL like: /admin/user/edit/1 I always get /admin/user I create the link this way:
<?php $this->url('zfcadmin/user/edit', array(
'action' => 'edit',
'user_id' => $user['user_id'],
)) ?>

You creating URL incorrectly. Please create the link like this.
<?php $this->url('zfcadmin/user/edit', array( 'user_id' => $user['user_id'], ) ?>

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 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',
),
),
),

Tree Routing in Zend Framework 2

I have a problem about tree routing in Zend Framework 2, I hope you'll help me.
I'm building module Administrator. When I login, it will run loginAction of AdministratorController. When I login success, it will go to indexAction of AdministratorController. I'm done this.
zf2exam.loc/Administrator ---> login --> login success -> zf2exam.loc/Administrator/index.php
So, when I login success, I want to manage all controllers of module Administrator. How must I do to route to another controller in module Administrator? Example: zf2exam.loc/Administrator/News/Update/1. Thanks for your watching. Thanks so much.
return array(
'controllers' => array(
'invokables' => array(
'Admin\Controller\Admin' => 'Admin\Controller\AdminController',
'Admin\Controller\News' => 'Admin\Controller\NewsController',
),
),
'router' => array(
'routes' => array(
'admin' => array(
'type' => 'Segment',
'options' => array(
'route' => '/admin[/:action][/:id]',
'defaults' => array(
'__NAMESPACE__' => 'Admin\Controller',
'controller' => 'admin',
'action' => 'login',
),
),
'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(
'controller' => 'about',
'action' => 'index',
),
),
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'doctype' => 'HTML5',
'admin' => __DIR__ . '/../view',
),
),
);
I can't test, but i would do something like this :
'router' => array(
'routes' => array(
'admin' => array(
'type' => 'Litteral',
'options' => array(
'route' => '/Administrator',
'defaults' => array(
'__NAMESPACE__' => 'Admin\Controller',
'controller' => 'Admin',
'action' => 'login',
),
),
'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(
'controller' => 'Admin',
'action' => 'index',
),
),
),
),
),
),
),

zend framework 2 (zfcadmin) and routing

I want to create admin panel using ZfcAdmin module. I want to create routing, to manage users. Here is it:
<?php
return array(
'controllers' => array(
'invokables' => array(
'AdminUser\Controller\AdminUser' => 'AdminUser\Controller\AdminUserController',
),
),
'view_manager' => array(
'template_path_stack' => array(
'admin-user' => __DIR__ . '/../view',
),
),
'router' => array(
'routes' => array(
'zfcadmin' => array(
'may_terminate' => true,
'child_routes' => array(
'user' => array(
'type' => 'segment',
'options' => array(
'route' => '/user',
'defaults' => array(
'controller' => 'AdminUser\Controller\AdminUser',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' =>array(
'edit' => array(
'type' => 'segment',
'options' => array(
'route' => '/edit/:user_id',
'defaults' => array(
'controller' => 'AdminUser\Controller\AdminUser',
'action' => 'edit',
),
),
),
),
),
),
),
),
),
);
I request: /admin/user and it's ok, but when I want to receive URL like: /admin/user/edit/1 I always get /admin/user I create the link this way:
<?php $this->url('zfcadmin/user/edit', array(
'action' => 'edit',
'user_id' => $user['user_id'],
)) ?>
why? what's wrong?
The first argument in the ZF2 Url view helper is a $name. Have you tried using 'edit' like you've specified in your config?
I also think you may need to echo the value.
<?php echo $this->url('edit', array('action' => 'edit', 'user_id' => $user['user_id'],)); ?>

Zend Framework 2 Navigation's exception

I was looking for show a navigation menu in the layout.phtml file:
<?php echo $this->navigation('navigation')->menu(); ?>
So, I added these lines to /module/Application/config/module.config.php in order to declare the menu's structure according to my app routes:
'navigation' => array(
'default' => array(
'place' => array(
'label' => 'Places',
'route' => '/place',
'pages' => array(
'country' => array(
'label' => 'Countries',
'route' => '/place/country',
),
'state' => array(
'label' => 'States',
'route' => '/place/state',
),
'city' => array(
'label' => 'Cities',
'route' => '/place/city',
),
),
),
),
),
and then I wrote a loader for the Zend/Navigation instance:
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
),
),
but I get repeatedly this exception:
Fatal error: Zend\Mvc\Router\Exception\RuntimeException: Route with
name "" not found in
C:\wamp\www\bsol\vendor\zendframework\zendframework\library\Zend\View\Helper\Navigation\AbstractHelper.php
on line 471
I attempted to solve the problem adding this line to the menu declaration:
'pages' => array(
'route' => '/place',
'country' => array(
'label' => 'Countries',
'route' => '/place/country',
),
Nevertheless, In that case I get another different exception:
Fatal error: Uncaught exception
'Zend\Navigation\Exception\InvalidArgumentException' with message
'Invalid argument: $page must be an instance of
Zend\Navigation\Page\AbstractPage or Traversable, or an array' in
C:\wamp\www\bsol\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php
on line 733
UPDATED: Following Jurian Sluiman's suggestion,
This must be my router configuration (module\Place\config\module.config.php), if I've got a module called 'Place', with 3 controllers (city, country and state):
'router' => array(
'routes' => array(
'city' => array(
'type' => 'segment',
'options' => array(
'route' => '/city[/:action][/:oid]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Place\Controller\City',
'action' => 'index',
),
),
),
'country' => array(
'type' => 'segment',
'options' => array(
'route' => '/country[/:action][/:oid]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Place\Controller\Country',
'action' => 'index',
),
),
),
'state' => array(
'type' => 'segment',
'options' => array(
'route' => '/state[/:action][/:oid]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Place\Controller\State',
'action' => 'index',
),
),
),
),
),
And this must be my navigation config (actually I located this one in an autoloadable file in /config/autoload/menu.global.php):
return array(
'navigation' => array(
'default' => array(
'place' => array(
'label' => 'Places',
'route' => 'country',
'pages' => array(
'country' => array(
'label' => 'Countries',
'route' => 'country',
),
'state' => array(
'label' => 'States',
'route' => 'state',
),
'city' => array(
'label' => 'Cities',
'route' => 'city',
),
),
),
),
),
);
...And now It works!.
Usually routes start not with a /. The / is a delimiter for route names, so when the /place route is assembled, you have a parent "" and then a child "place". The first one is empty, giving you this error.
Perhaps you confused route names with the route itself. You have to provide the name of the route, so the route itself (the URL) will be returned. So imagine you have this route config:
'router' => array(
'routes' => array(
'place' => array(
'type' => 'literal',
'options' => array(
'route' => '/place',
'defaults' => array(
// Some defaults here
),
),
),
),
),
In this case, your route name is place and not /place. If you have a different route config then the one here above, put the route config in your question so I can update my answer.

Resources