Automatic convention-based routing within a module in Zend Framework 2--possible? - zend-framework

I'm trying to understand all the configuration necessary to get my routing working in Zend Framework 2, and I can't help but wonder if I am making this more complicated than necessary.
I am working on a simple app that will follow a very simple convention:
/:module/:controller/:action
I've already created and wired up my module, "svc" (short for "service)". I then created a second controller, the "ClientsController", and I can't get the routing to pass through my requests to, e.g., /svc/clients/list to ClientsController::listAction().
As I'm wading through hundreds of lines of configuration, in deeply nested arrays, I'm thinking--isn't there some way to just have a default mapping of my URLs to /:module/:controller/:action ?
Thanks for any assistance. I'm going off of the Zend Framework 2 Quick Start, which walked me through creating a new module and then adding a controller to that module. But when I tried to add second controller to that module, I am tripping over the routing.
Update: I didn't catch this the first time through, but apparently this is supposed to be a feature of the Zend Framework Skeleton app. From the quick start guide:
ZendSkeletonApplication ships with a “default route” that will likely
get you to this action. That route basically expects
“/{module}/{controller}/{action}”, which allows you to specify this:
“/zend-user/hello/world”
That's exactly what I want! But I can't get it to work.
It lists an incomplete module.config.php, with a comment at the bottom about putting "other configuration" here. I tried to figure out what that "other configuration" is, and wound up with this:
return array(
'svc' => array(
'type' => 'Literal',
'options' => array(
'route' => '/svc',
'defaults' => array(
'controller' => 'svc\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(
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'svc\Controller\Clients' => 'svc\Controller\ClientsController',
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);
JFYI, here is what my controller looks like.
namespace svc\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class ClientsController extends AbstractActionController {
public function indexAction() {
return new ViewModel();
}
public function anotherAction(){
return new ViewModel();
}
}
My routes are not working. I get "route not found" when I try to pull up any of my routes.

It lists an incomplete module.config.php, with a comment at the bottom about putting "other configuration" here. I tried to figure out what that "other configuration" is, and wound up with this:
If your module.config.php really looks like that then it won't work, routes is an array of routes defined in the router key, your config contains no such spec, try replacing it with this
return array(
// routes
'router' => array(
'routes' => array(
'svc' => array(
'type' => 'Literal',
'options' => array(
'route' => '/svc',
'defaults' => array(
'controller' => 'svc\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(
// add the default namespace for :controllers in this route
'__NAMESPACE__' => 'svc\Controller',
),
),
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'svc\Controller\Clients' => 'svc\Controller\ClientsController',
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);

Related

Zend Framework 2 routing error: resolves to invalid controller class or alias

I'm trying to learn Zend Framework 2 and I have their skeleton application up and running. In order to access it I visit http://localhost:8080/. When visiting that link it displays their generic Zend page. What I want to be able to do is visit http://localhost:8080/application/test and have it bring me to a different page with a different layout.
Here is the module.config.php
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* #link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* #copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* #license http://framework.zend.com/license/new-bsd New BSD License
*/
return array(
'router' => array(
'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
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\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(
),
),
),
),
),
)
),
'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',
),
),
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController'
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
// Placeholder for console routes
'console' => array(
'router' => array(
'routes' => array(
),
),
),
);
In order to make it work for me this is what I tried:
First I created a controller named TestController in Application/src/Application/Controller/.
Next I added 'application/test/index' => DIR . '/../view/application/test/index.phtml' to the template_map array in view_manager.
I also added 'Application\Controller\Test' => 'Application\Controller\TestController' to the controllers array.
When I visit http://localhost:8080/application/test I get the error:
test(resolves to invalid controller class or alias: test)
I'm obviously doing something wrong but tbh the documentation on Zend is not newb friendly at all and it's becoming very frustrating. Could someone point me in the correct direction? There's just so much configuration that I'm sure I'm missing something small. Thanks!
At the moment the route named application (the parent) defines a URL route of /application. The child route default however requires the controller name to be passed in as the first argument, followed by
action.
This means the URL would be
/application/[:controller]/[:action]
So visting
/application/test
You are inadvertently trying to fetch the 'test' controller; hence the error.
Resolves to invalid controller class or alias : test
To resolve this I would strongly recomend against using the a :controller route parameter but instead use a route per controller action.
'application' => [
'type' => 'Literal',
'options' => [
'route' => '/application',
'defaults' => [
'controller' => 'Application\Controller\Index',
'action' => 'index',
],
'may_terminate' => true,
'child_routes' => [
'test' => [
'type' => 'Literal',
'options' => [
'route' => '/test',
'defaults' => [
// controller value is inherited from parent!
'action' => 'test',
],
],
],
],
],
],

How to add a Restful controller in a Zend module

I'm actually working on a zend framework 2 application which should render two sorts of View :
a view model
a json model
I would include the restful controller in my module but I don't understand if it's possible or recommended.
If someone has been already confronted to a same case, does he could bring me some explanations because I'm searching an answer since 2 days fruitlessly.
In my module.config.php I have :
return array(
'controllers' => array(
'invokables' => array(
'Role\Controller\Role' => 'Role\Controller\RoleController',
'Role\Controller\RoleRest' => 'Role\Controller\RoleRestController',
),
),
'router' => array(
'routes' => array(
'role' => array(
'type' => 'segment',
'options' => array(
'route' => '/role[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Role\Controller\Role',
'action' => 'index',
),
),
),
'role-rest' => array(
'type' => 'segment',
'options' => array(
'route' => '/role-rest[/:id]',
'constraints' => array(
'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Role\Controller\RoleRest',
'action' => 'getList',
),
),
),
),
),
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy',
),
'template_path_stack' => array(
'role' => __DIR__ . '/../view',
),
),
);
and when i try to get myApplication.localhost/role-rest, the page displays the following message :
"The requested controller was unable to dispatch the request.
Controller:
Role\Controller\RoleRest"
I resolve the problem with a simple change in the id's regex constraint :
'constraints' => array(
'id' => '[a-zA-Z0-9_-]*',
),

Routing with Zend Framework 2 Restful Webservice

I want to implement a RESTful webservice by using Zend Framework 2, more precisely 2.1.5. I got a 404 if I visit http://ehcserver.localhost/rest, the corresponding message is 'rest(resolves to invalid controller class or alias: rest)'. What went wrong?
You can see my source code in my github-repository:
https://github.com/Jochen1980/EhcServer/blob/master/module/Application/config/module.config.php
The route is defined like this:
return array(
'router' => array(
'routes' => array(
'rest' => array(
'type' => 'ZendMvcRouterHttpSegment',
'options' => array(
'route' => '/:controller[.:formatter][/:id]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'formatter' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]*'
),
),
),
'home' => array(
...
Your route doesn't define a namespace to which the controller belongs, you need to add a __NAMESPACE__ to route defaults
'rest' => array(
'type' => 'ZendMvcRouterHttpSegment',
'options' => array(
'route' => '/:controller[.:formatter][/:id]',
'defaults' => array(
// tell the router which namespace :controller belongs to
'__NAMESPACE__' => 'Application\Controller',
),
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'formatter' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]*'
),
),
),
Are you sure the type is valid?
type' => 'ZendMvcRouterHttpSegment',
to this
type' => 'Segment',

How to redirect with GET query in Zend 2?

I can redirect to matchable routes like $this->redirect()->toRoute('controller', array('action' => 'something', 'foo' => 'bar')), but how can I append a get query? I'm using it for a filter for a table. The filter can either be submitted on the page (form get method), or linked from another page (need to add get query).
try this :
'course' => array(
'type' => 'Segment',
'options' => array(
'route' => '/course[/:action[/:id]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '\d+'
),
'defaults' => array(
'__NAMESPACE__' => 'Home\Controller',
'controller' => 'course',
'action' => 'index',
'id' => 0
),
),
'may_terminate' => true,
'child_routes' => array(
'query' => array(
'type' => 'Query',
'options' => array(
'route' => '?[:filterByDepartment]'
),
),
),
),
make sure the route is met your need in the view :
echo $this->url('course/query', array('controller'=>'course', 'action'=>'index', 'id'=>0, 'filterByDepartment'=>'depA'));
if this is right, let's try the following in the controller action :
$this->redirect()->toRoute('course/query', array('controller'=>'course', 'action'=>'index', 'id'=>0, 'filterByDepartment'=>'depA'));

same controller name in different modules in zf2

I just disvovered today that zf2 doesn't realy like when 2 controllers have the same name even if they are not in the same module.
However, I need to be able to call
localhost/users/types
and
localhost/messages/types
For the moment, my two controllers have the same names.
I also discovered that whatever the name of the module is, I always get the result of messages/types, even with localhost/nonexistingmodule/types oO
Here is what my module.config.php looks like :
return array(
'controllers' => array(
'invokales' => array(
'messages' => 'Messages\Controller\MessagesController,
'messages' => 'Messages\Controller\TypesController,
),
),
'di' => array(
'instance' => array(
'alias' = array(),
),
),
'router' => array(
'routes => array(
'restful' => array(
'type' => 'Zend\Mvc\Router\Http\Segment'
'options' => array(
'route' => '/Messages/:controller[.:formatter][/:id],
'constraints' => array(
'module' => '[a-zA-Z][a-zA-Z0-9_-]*',
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'formater' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]*',
),
'defaults' => array(
'module' => 'Messages',
),
),
),
),
),
'view_manager' => array( ... ),
);
I have tried to set 'module' => 'Messages' in constraints (we never know :p) but I had a 404 error.
The module.config.php of the Tasks module is the same but for tasks.
I originaly had aliases but I removed them after I read somewhere this was not very recomanded.
One more thing, this is a REST API, all my controllers extends AbstractRestfulController (in case this is important)
Anyone has an idea of how to make my 2 url to work?
Thanks :)
Aliases for invokables or services and anything else should be unique. If they are not unique they may be overwritten by another module in the order the modules themselfes are loaded. That means: when setting up invokables or any sorts of aliases, make sure that the aliases are unique and meaningful. Personally i name my Controllers like this:
'invokables' => array(
'mymodule-controller-controllername' => 'Mymodule\Controller\ControllernameController'
)
Same with Services or any other sorts of aliases
'services' => array(
'mymodule-service-servicename' => 'Mymodule\Service\Classname'
)
The Documentation follows a Namespace-styled syntax like...
'invokables' => array(
'Mymodule\Controller\Controllername' => 'Mymodule\Controller\ControllernameController'
)
...which personally i find totally confusing, as it resembles a Namespace way too much and doesn't really auto-imply that it is just an alias / key
Now your comments-question i do not understand. You want to match one route to two different controllers? That would be impossible and senseless.
Answer Update with routing config
As for routing configuration you have several possible ways. Personally i put lots of effort into building up literal routes, since they are the fastest, but also require a lot of manual attention. Alternatively there is segment routes which inhibit a little more magic to them. I will cover the literal approach for you:
Module Messages
'controllers' => array(
'invokables' => array(
'messages-controller-index' => 'Messages\Controller\IndexController',
)
),
'router' => array(
'routes' => array(
'messages' => array(
'type' => 'literal',
'options' => array(
'route' => '/messages',
'defaults' => array(
'controller' => 'messages-controller-index',
'action' => 'index'
)
),
'may_terminate' => true,
'child_routes' => array(
'types' => array(
'type' => 'literal',
'options' => array(
'route' => '/types',
'defaults' => array(
'action' => 'types'
)
)
)
)
)
)
)
Module Tasks
'controllers' => array(
'invokables' => array(
'tasks-controller-index' => 'Tasks\Controller\IndexController',
)
),
'router' => array(
'routes' => array(
'tasks' => array(
'type' => 'literal',
'options' => array(
'route' => '/tasks',
'defaults' => array(
'controller' => 'tasks-controller-index',
'action' => 'index'
)
),
'may_terminate' => true,
'child_routes' => array(
'types' => array(
'type' => 'literal',
'options' => array(
'route' => '/types',
'defaults' => array(
'action' => 'types'
)
)
)
)
)
)
)
So what's happening there is:
if route is /messages if routes to controller-alias messages-controller-index with indexAction()
if route is /messages/types it stays at controller-alias messages-controller-index but goes to typesAction()
if route is /tasks if routes to controller-alias tasks-controller-index with indexAction()
if route is /tasks/types it stays at controller-alias tasks-controller-index but goes to typesAction()
You could obviously change controller aliases and meanings behind that. If you'd want to add an ID for a route like /messages/types/1 you'd build a child-route to the types-route that is of type segment and check for an [:id] parameter whose constraints should be numeric :) Check the official ZF2 Manual for more information, i'm lazy now :P
tl;dr: Make sure you have a __NAMESPACE__ default in your route definition, and then make sure all your service names are prefixed with it. Usually, the value will match your module namespace.
For the longer explanation:
I'm going to be blunt: it's a really bad practice to have a dynamic segment in your route that maps to the controller (:controller in your example). The reason is that if somebody discovers this, they can do controller injection, and request a controller that should not be routed by this particular route.
Additionally, the practice can easily lead to collisions, particularly if you omit a __NAMESPACE__ default in your route configuration -- which, based on the question, you've already discovered.