ZF2 routing ignores namespace - php

I am having an issue with zf2 routing. I am using skeleton example https://github.com/zendframework/ZendSkeletonApplication/blob/master/module/Application/config/module.config.php, but when I tying to access http://localhost/admin/ or http://localhost/admin/answers I am getting 404 with message:
A 404 error occurred
Page not found.
The requested controller could not be mapped to an existing controller class.
Controller:
Index(resolves to invalid controller class or alias: Index)
No Exception available
From error message I think router ignores __NAMESPACE__. Maybe someone could help me find solution to my problem?
I have used https://github.com/zendframework/ZFTool to create module and controllers.
My file structure is:
module/Admin
├── config
│   └── module.config.php
├── Module.php
├── src
│   └── Admin
│   └── Controller
│   ├── AnswersController.php
│   └── IndexController.php
└── view
└── admin
├── answers
│   └── index.phtml
└── index
└── index.phtml
My module.config.php:
return array(
'controllers' => array(
'invokables' => array(
'Admin\Controller\Answers' => 'Admin\Controller\AnswersController',
'Admin\Controller\Index' => 'Admin\Controller\IndexController',
),
),
'router' => array(
'routes' => array(
'admin' => array(
'type' => 'literal',
'options' => array(
'route' => '/admin',
'defaults' => array(
'__NAMESPACE__' => 'Admin\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(
'admin' => __DIR__ . '/../view',
),
),
);
If I am changing: '\__NAMESPACE__' => 'Admin\Controller', 'controller' => 'Index', to 'controller' => 'Admin\Controller\Index' I can access Index controller, but not Answers .
Additional info:
I found strange behavior of skeleton application. I have downloaded fresh skeleton application and this configuration works. If I am going to localhost/application/index or localhost/application/answers it works as accepted. But if I am changing Application module name to Admin and changing router configuration (replacing all application to admin and Application to Admin) and config stops working. Can anyone explain this? Maybe Application module works as default in skeleton applications?
I am using Zend Framework 2.2.4.

add above default key
'answers' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/answers',
'defaults' => array(
'controller' => 'Admin\Controller\Answers',
'action' => 'index',
),
),
),
if that not help try comment default key
So maybe try wildcards ?
'application' => 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' => 'Application',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Wildcard',
'options' => array(
),
),
),
),
it's only lead not complete solution

Set in Module.php
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}

I'am not 100% sure of what you are trying to achieve but this may help you in a way. In case you have a variable controller param I would set up my routes like this:
'router' => array(
'routes' => array(
'admin' => array(
'type' => 'segment',
'options' => array(
'route' => '/admin[/:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]+',
'action' => '[a-zA-Z][a-zA-Z0-9_-]+',
),
'defaults' => array(
'controller' => 'Admin\Controller\Admin',
'action' => 'index',
),
),
),
),
),
When redirecting you would need to specify the controller and the action this way though.
return $this->redirect()->toRoute('admin', array('action' => 'newAction', 'controller' => 'someOtherController'));
URL View-Helper equivalent:
$this->url('admin', array('action' => 'newAction', 'controller' => 'someOtherController'));
EDIT:
Zend framework2 merges all config fiels trough your models and provides the Router Module with the array you have configured. In fact you could extend the Router class and alter it to your likeing.
As you can probably see it actually is not "mandatory" to provide the router with the namespace of each module within your config files. The router has your invoked Controllers and can "track" them via the zf2 naming convention.
EDIT2: I forgot to add some constraint's to the route can you try it with the updated route configuration again?

Related

Zend 2 : Unable to render template

I have an error in my Zend Framework 2 project, I try to add a new view named " Blog "
I created the module.config and indexController, and the view but always I get this error and I don't know why :
Zend\View\Renderer\PhpRenderer::render: Unable to render template
"blog/index/index"; resolver could not resolve to a file
the module.config code
<?php
namespace Blog;
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 /blog/:controller/:action
'blog' => array(
'type' => 'Literal',
'options' => array(
'route' => '/blog',
'defaults' => array(
'__NAMESPACE__' => 'Blog\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(
),
),
),
),
),
),
),
'controllers' => array(
'invokables' => array(
//'Blog\Controller\Index' => 'Blog\Controller\IndexController'
'Blog\Controller\Index' => Controller\IndexController::class
),
),
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
?>
the indexController code :
<?php
namespace Blog\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function indexAction()
{
return new ViewModel();
}
}
?>
and finally my structure folders is like this :
I use version 2.4.13
The src folder should only contain PHP classes, your view templates should not be in there. Move the view folder up a level and it should work.
Take a look at the folder structure of the ZF skeleton app as a reference: https://github.com/zendframework/ZendSkeletonApplication/tree/master/module/Application

Zend2 - ZF2 - Routing Issue

i am trying to make my router working so that:
/Auth redirects to Auth controller of Auth MOdule
/Auth/Login redirects to Login controller of Auth Module
While the first works just right the /Auth/Login results in routing issue.
My router configuration file looks like below:
'router' => array(
'routes' => array(
'Auth' => array(
'type' => 'literal',
'options' => array(
'route' => '/Auth',
'defaults' => array(
'controller' => 'Auth\Controller\Auth',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'Auth/Login' => array(
'type' => 'literal',
'options' => array(
'route' => '/Login',
'defaults' => array(
'controller' => 'Auth\Controller\Login',
'action' => 'index')
),
),
),
),
),
),
edit this section
'child_routes' => array(
'Auth_Login' => array(
// ... your existing codes
Just remove / from Auth/Login and use hyphen - or _ instead.
The answer lies in #TimFountain his comment. Because you named the child route Auth/Login you will have to request Auth/Auth/Login to get a match.
As soon as you rename the child route to Login you will get the route match as expected on Auth/Login.

Unable to render template ... resolver could not resolve to a file

Starting a new project with the skeleton module and I'm running into some issues. I'm sure this is a basic config error on my part, but I can't figure it out.
Here is my module.config.php
return array(
'controllers' => array(
'invokables' => array(
'ZFTickets\Controller\Index' => 'ZFTickets\Controller\IndexController',
),
),
'router' => array(
'routes' => array(
'zftickets' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
//'route' => '/zftickets',
'route' => '/',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'ZFTickets\Controller',
'controller' => 'Index',
//'controller' => 'ZFTickets\Controller\Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
// This route is a sane default when developing a module;
// as you solidify the routes for your module, however,
// you may want to remove it and replace it with more
// specific routes.
'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(
'zftickets' => __DIR__ . '/../view',
),
),
and here is the directory structure:
The error I get is: Zend\View\Renderer\PhpRenderer::render: Unable to render template "zf-tickets/index/index"; resolver could not resolve to a file
The resolver is looking for zf-tickets/index/index, but you're created the folders as zftickets/zftickets/index. Change these and it should work fine.
You should also change the view manager part of your config to:
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
The array key is irrelevant there, so what you currently have might appear confusing.
ZF2's view renderer will try to render .phtml files accordingly matched controller/action names since you don't provide a custom template. Try to rename second zftickets folder to your controller name (in this case: index) like this:
|-- ...
|-- test
|-- view
| `-- zftickets
| `-- index
| `-- index.phtml
`-- autload_classmap.php

Multiple controller in one module in zend framework

hi i am new in zend framework2.2.0. i want to create one module with multiple controller i have download "Album" module from github and its working fine
Now i want to add the more controller in it
the below i have shown my folder structure for file in module
module/
Album/
config/
module.config.php
src/
Album/
Controller/
AlbumController.php
UserController.php
Form/
AlbumForm.php
UserForm.php
Model/
AlbumTable.php
Album.php
UserTable.php
User.php
view/
album/
album/
index.phtml
user/
index.phtml
i have also changed all the name space in file
namespace Album\Controller;
class UserController extends \Zend\Mvc\Controller\AbstractActionController
and some indexAction method witch returns a new \Zend\View\Model\ViewModel();
then you can create your viewfile in
Album/view/Album/user/index.phtml
i did above changes.
is there any chage needed in "Album/Module.php" file ?
can you tell me through which link i can see users list ?
i and tired with this error help me to get out of it
You may need to add url rules for user in module.config.php in Album/config folder. Then you can access the url like
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'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' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
'user' => array(
'type' => 'segment',
'options' => array(
'route' => '/user[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\User',
'action' => 'index',
),
),
),
),
),
Also
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' => 'Album\Controller\AlbumController',
'Album\Controller\User' => 'Album\Controller\UserController',
),
),

Zend Framework 2 Routing subdomains to module

After searching a long time with no success.
before I give up, I would like to ask:
Is there a way to route a subdomain to a module in Zend Framework 2? like:
Subdomain => Module
api.site.com => api
dev.site.com => dev
admin.site.com => admin
site.com => public
...
I tried doing it like this but I can't get access to controllers other than the default (Index).
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'site.com',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
)
)
),
),
Thank you for taking the time to help me.
Zend Framework 2 doesn't have a notion of routing to modules; all routing mappings are between a URI pattern (for HTTP routes) and a specific controller class. That said, Zend\Mvc provides an event listener (Zend\Mvc\ModuleRouteListener) which allows you to define a URI pattern that maps to multiple controllers based on a given pattern, and so emulates "module routing". To define such a route, you would place this as your routing configuration:
'router' => array(
'routes' => array(
// This defines the hostname route which forms the base
// of each "child" route
'home' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'site.com',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
// This Segment route captures the requested controller
// and action from the URI and, through ModuleRouteListener,
// selects the correct controller class to use
'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' => 'Index',
'action' => 'index',
),
),
),
),
),
),
),
(Click here to see an example of this # ZendSkeletonApplication)
This is only half of the equation, though. You must also register every controller class in your module using a specific naming format. This is also done through the same configuration file:
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController'
),
),
The array key is the alias ModuleRouteListener will use to find the right controller, and it must be in the following format:
<Namespace>\<Controller>\<Action>
The value assigned to this array key is the fully-qualified name of the controller class.
(Click here to see an example of this # ZendSkeletonApplication)
NOTE: IF you aren't using ZendSkeletonApplication, or have removed it's default Application module, you will need to register the ModuleRouteListener in one of your own modules. Click here to see an example of how ZendSkeletonApplication registers this listener
If i understand slide #39 of DASPRIDS Rounter Presentation correctly, it's as simple as - on a per module basis - to define your subdomain hosts, i.e.:
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'api.site.com',
'defaults' => array(
'__NAMESPACE__' => 'Api\Controller',
'controller' => 'Index',
'action' => 'index',
),
)
)
),
),
Etc, you'd do this for every Module on its own.

Categories