Zend Framework 3 skeleton two module - zend-framework

I'm following the zend framework 3 skeleton tutorial to the book.
I first got the application module working where it shows the standard welcome to zend screen.
When I added the album module everything went fine. When I navigate to /album in my url it displays the album section so all good there. However, when I remove the /album from the end of the url to get back to the application section I get the following 404 page.
**A 404 error occurred**
Page not found.
The requested controller could not be mapped to an existing controller class.
Controller:
ApplicationController (resolves to invalid controller class or alias:
ApplicationController)
No Exception available
I would show some sections of my code but at the minute I'm unsure as to what file could be causing this. If someone could tell me what config file is causing this issue then I can upload it. I'm pretty sure it's routing but coming from ZF1 it's a bit of a head scratcher.
If someone could help or explain to me where to look I would be very thankful.
UPDATE
Below is my module.config.php for my Application module.
<?php
/**
* #link http://github.com/zendframework/ZendAlbumApplication for the canonical source repository
* #copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* #license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Application;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;
return [
'router' => [
'routes' => [
'home' => [
'type' => Literal::class,
'options' => [
'route' => '/',
'defaults' => [
'controller' => \ApplicationController::class,
'action' => 'index',
],
],
],
'application' => [
'type' => Segment::class,
'options' => [
'route' => '/application[/:action]',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
],
],
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
],
],
'view_manager' => [
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => [
'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' => [
__DIR__ . '/../view',
],
],
];
?>
UPDATE 2
When I replace the /album with /application in the url it shows the skeleton zend framework welcome page. I thought this page would show with just http://localhost? Am I missing the point or can I make the application module the default module so you don't have to add /application to the end of the url?

I've done it. It seems like it was a newby mistake/typo.
I had to change the line:
'controller' => \ApplicationController::class,
TO
'controller' => Controller\IndexController::class,
Thank you for pointing me in the right direction and telling me what page of code to upload.

Related

How to set a default action for SocialEngine module

I have created a module with two controllers: Index and Pay. There are actions for both controllers, i.e.
Index -> indexAction
Pay -> indexAction, callbackAction, etc.
I have defined routes in the module's manifest.php, although it seems defining the routes in that file makes no difference as those routes work correctly anyway. The problem is when I browse the root of the module i.e. http://example.com/pgateway, only a specific action from my second controller (PayController->callbackAction) is executed. Why is that and how can I make say IndexController->indexAction the default page when example.com/pgateway is browsed?
My route definitions (manifest.php):
'routes' => [
'pay_general' => [
'route' => 'pgateway/:controller/:action/*',
'defaults' => [
'module' => 'pgateway',
'controller' => 'pay',
'action' => 'index',
],
'reqs' => [
'controller' => '\D+',
'action' => '\D+',
],
],
'pay_callback' => [
'route' => 'pgateway/:controller/:action/*',
'defaults' => [
'module' => 'pgateway',
'controller' => 'pay',
'action' => 'callback',
],
'reqs' => [
'controller' => '\D+',
'action' => '\D+',
],
],
],
route should be unique, in each definition. :action means it would work with values as well as empty. Incase of empty it will use defaults.
In your case latest route is overriding.
Try removing pay_callback, it would work as in pay_general.
Convention is manage one route for a controller, and manage accordingly.

Zend2 Module can't be initialized

Here my Module.php:
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* #link http://github.com/zendframework/ZendSkeletonModule for the canonical source repository
* #copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* #license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Users;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Mvc\ModuleRouteListener;
class Module implements AutoloaderProviderInterface
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
// if we're in a namespace deeper than one level we need to fix the \ in the path
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function onBootstrap($e)
{
// You may not need to do this if you're doing it elsewhere in your
// application
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
}
And here my module.config.php:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Users\Controller\Index' => 'Users\Controller\IndexController',
),
),
'router' => array(
'routes' => array(
'users' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/users',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'Users\Controller',
'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(
'users' => __DIR__ . '/../view',
),
),
);
But when I try to see the index page I get this:
( ! ) Fatal error: Uncaught
Zend\ModuleManager\Exception\RuntimeException: Module (Users) could
not be initialized. in
/var/www/html/communicationapp/vendor/zendframework/zendframework/library/Zend/ModuleManager/ModuleManager.php
on line 195
( ! ) Zend\ModuleManager\Exception\RuntimeException: Module (Users) could not be initialized. in /var/www/html/communicationapp/vendor/zendframework/zendframework/library/Zend/ModuleManager/ModuleManager.php
on line 195
Anyone knows what's wrong with this code?
A couple of things to check:
The filename is Module.php - that exact case.
That Module.php is in module/Users (i.e. not in module/Users/src/)
I used internal server of php where I don't have xdebug enabled it got this:
127.0.0.1:48908 [500]: /public/ - Uncaught Zend\ModuleManager\Listener\Exception\InvalidArgumentException: Config being merged must be an array, implement the Traversable interface, or be an instance of Zend\Config\Config. boolean given. in /var/www/html/communicationapp/vendor/zendframework/zendframework/library/Zend/ModuleManager/Listener/ConfigListener.php:342
Stack trace:
0 /var/www/html/communicationapp/vendor/zendframework/zendframework/library/Zend/ModuleManager/Listener/ConfigListener.php(127): Zend\ModuleManager\Listener\ConfigListener->addConfig('Users', false)
1 [internal function]: Zend\ModuleManager\Listener\ConfigListener->onLoadModule(Object(Zend\ModuleManager\ModuleEvent))
2 /var/www/html/communicationapp/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(444): call_user_func(Array, Object(Zend\ModuleManager\ModuleEvent))
3 /var/www/html/communicationapp/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(205): Zend\EventManager\EventManager->triggerListeners('loadModule in /var/www/html/communicationapp/vendor/zendframework/zendframework/library/Zend/ModuleManager/Listener/ConfigListener.php on line 342

rendering layout on the production server

My productionserver is not able to render my loginlayout. On my development system everything works fine.
Here comes the error on the production website:
Fatal error: Uncaught Zend\View\Exception\RuntimeException: Zend\View\Renderer\PhpRenderer::render: Unable to render template "layout/layoutlogin"
How I did this:
My Application/config/module.config.php gives the templates as follows:
'view_manager' => [
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => [
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'layout/layoutlogin' => __DIR__ . '/../view/layout/layoutlogin.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' => [
__DIR__ . '/../view',
],
],
In my Indexcontroller I set the Layout to:
$this->layout()->setTemplate('layout/layoutlogin');
That might do what I want!? I get no error and no warning in the development mode on the development environment. Any ideas appreciated!
You may try this way
public function loginAction()
{
// Provide the layout here
$this->layout('layout/layoutlogin');
$view = new ViewModel();
// Here you may set the template
$view->setTemplate("/module/controller/template");
return $view;
}
This actually means you are using a layout which is other than the default and then inside this layout you may use template that you want for this.
The error ist solved. Actually it was a server configuration issue. In this case it was that Apache didn't have enough rights for the folder
data/cache

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

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

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