ZF2 - Zend Framework 2, understanding routing - zend-framework2

I am trying to get my head around Module routing in ZF2.
At the moment I am only able to create a single controller for a single action and am struggling to figure this routing out. I have looked at other modules and plugins and I kind of get it, just need a small push to "get it".
In this example I am trying to route to the two actions: indexAction and cmstoolsAction
Essentially a user navigates to:
/affiliates/overview
/affiliates/cmstools
And the error is:
The requested URL could not be matched by routing.
I think where I am struggling is firstly comprehending how MVC works and getting lost in the detail. There is so much information in the manual that it becomes a little overwhelming.
anyway - would greatly appreciate any input!
Image of the Module structure:
My controller looks like this:
<?php
namespace Affiliates\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class AffiliatesController extends AbstractActionController
{
//Overview page
public function IndexAction()
{
}
public function CmstoolsAction()
{
}
}
And my module config looks like this:
<?php
return array(
'view_manager' => array(
'template_path_stack' => array(
'affiliates' => __DIR__ . '/../view',
),
),
'controllers' => array(
'invokables' => array(
'Affiliates\Controller\Affiliates' =>
'Affiliates\Controller\AffiliatesController'
),
),
'router' => array(
'routes' => array(
'affiliates' => array(
'type' => 'Literal',
'options' => array(
'route' => '/overview',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'cmstools' => array(
'type' => 'Literal',
'options' => array(
'route' => '/cmstools',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'cmstools',
),
),
),
),
),
),
),
);

The routing config is the only important part here. At the moment, you have a route for /overview, which has a child route for /cmstool. This would match the following URLs:
/overview
/overview/cmstool
Not quite what you were after.
There are a few different ways you could configure this. The one closest to what you currently have would be a route for /affiliates, with two child routes, one for each of your actions. The configuration for this would be:
'router' => array(
'routes' => array(
'affiliates' => array(
'type' => 'Literal',
'options' => array(
'route' => '/affiliates',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'index',
),
),
'child_routes' => array(
'overview' => array(
'type' => 'Literal',
'options' => array(
'route' => '/overview',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'index',
),
),
),
'cmstools' => array(
'type' => 'Literal',
'options' => array(
'route' => '/cmstools',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'cmstools',
),
),
),
),
),
),
),
This configuration contains three routes: affiliates, overview and cmstools. The latter two are both child routes of affiliates. Note that I removed the line 'may_terminate' => true, from the affiliates route. This determines whether or not the affiliates route would match on its own (i.e. whether the URL /affiliates would work). Since you didn't list this I'm assuming you don't want it to.
Another way you could configure this would be to simply create two literal routes, once for each URL (not using child routes at all):
'router' => array(
'routes' => array(
'overview' => array(
'type' => 'Literal',
'options' => array(
'route' => '/affiliates/overview',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'index',
),
),
),
'cmstools' => array(
'type' => 'Literal',
'options' => array(
'route' => '/affiliates/cmstools',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'cmstools',
),
),
),
),
),

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

Routing Zend Framework 2 Language in url

For my application translation I would like to use a language structure e.g.:
site.com (english)
site.com/de/ (german)
site.com/fr/ (france)
site.com/nl/ (dutch)
etc..
I can do this easily with the router option in Literal as '[a-z]{2}' but I want to exclude languages I do not support, e.g. site.com/it/ if it is not supported I want a 404. I tried to fix this with regex (adding supported languages) but something (I do not know) went wrong.
Thanks in advance!
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'language' => array(
'type' => 'Regex',
'options' => array(
'regex' => '/(?<lang>(de|fr|nl))?',
'defaults' => array(
'lang' => 'en', //default
),
'spec' => '/%lang%',
),
),
),
),
),
),
I think your regex needs to be
'regex' => '/(?<lang>(de|fr|nl)?)'
You could so the same with a Segment route and an appropriate constraint...
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'language' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:lang]',
'defaults' => array(
'lang' => 'en', //default
),
'constraints' => array(
'lang' => '(en|de|fr|nl)?',
),
),
),
),
),
),
),

zf2&zfc2 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 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'], ) ?>

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'],)); ?>

zf2 matching language in url route

how is it possible to match url like www.mydomain.com/en/aboutus to
controller -> index
action -> aboutus
lang -> en
in zf2 module routing config ?
in zf1 we fix that by something like this
$route = new Zend_Controller_Router_Route(
'/contact/:lang',
array(
'module' => 'default',
'controller' => 'contact',
'action' => 'index'
)
);
but the aproeach is something else
we want first determine what the language is in url then look into what controller or action user is requesting
zf2 has hirachy support in routers so you can build your routes like a tree
for your situation you have to create a parent route that match lang in url for example
www.mydomain.com/en or www.mydomain.com/fa or www.mydomain.com/de or ....
then in it children write route for others
for code example :
'langroute' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:lang]',
'defaults' => array(
'lang' => 'en',
),
'constraints' => array(
'lang' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
),
'may_terminate' => true,
'child_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
'aboutus' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/aboutus',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'aboutus',
),
),
),
),
as you can see the langrout match the en de fa or ... lang text
then the childern route check for inner page
in this example the url www.mydomain.com/en/ match the lang en and the route home
please add this code in your modules module.config.php
return array(
'router' => array(
'routes' => array(
// 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 /Module_Name/:controller/:lang/:action
'your_route_name_here' => array(
'type' => 'Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'__NAMESPACE__' => 'Module_Name\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller][/:lang][/:action]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'lang' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Index',
'lang' => 'en',
'action' => 'index',
),
),
),
),
),
),
),
);

Resources