Zend Framework class not loading in Ubuntu - php

I have ported my Zend Framework application to a different server. It's a Zend Framework application version 2.3.*
Now when going to this url http://calendar.app/calendar I get the following error:
Fatal error: Class 'Calendar\Controller\CalendarController' not found in /home/vagrant/Code/calendar/vendor/zendframework/zendframework/library/Zend/ServiceManager/AbstractPluginManager.php on line 170
My CalendarControler lives in my calendar module which is loaded like
return array(
'controllers' => array(
'invokables' => array(
'Calendar' => 'Calendar\Controller\CalendarController',
),
),
'router' => array(
'routes' => array(
'calendar' => array(
'type' => 'Literal',
'options' => array(
'route' => '/calendar',
'defaults' => array(
'controller' => 'calendar',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
I tried
composer clear-cache
composer dump-autoload
But this didn't help.
How could I fix this.

Your Calendar declaration inside the array is wrong. You need to use the full name in the array in order for Zend to find the class.
return array(
'controllers' => array(
'invokables' => array(
'Calendar\ControllerCalendar' => 'Calendar\Controller\CalendarController',
),
'alias' => array(
'Calendar' => 'Calendar\ControllerCalendar',
),
),
'router' => array(
'routes' => array(
'calendar' => array(
'type' => 'Literal',
'options' => array(
'route' => '/calendar',
'defaults' => array(
'controller' => 'calendar',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
Aside fro this error. Consider using controller factories if your class has hard dependancies or you are using the service manager like $this->getServiceLocator() inside your controller. :)

Related

Zend framework 2- more than one RESTful api modules

We have more than two RESTful modules (Restful & Testservices). Only one REST module is working at a time.
If I rearranged the Modules order in application.config.php , the last module of REST api is working .
For example if I keep 'Testservices' module after " Restful" module , "Testservices" is working.
Exmple - Testservices : http://localhost/dev/public/testservices/Userslist is working fine .
If I am calling this http://localhost/dev/public/restful/stateslist getting the following error :
A 404 error occurred
Page not found.
The requested controller was unable to dispatch the request.
Controller:
Application\Controller\Index
If I keep if I keep ' Restful ' module after " Testservices " module , " Restful " is working. Getting errors just reverse of above.
Here is the application.config.php
return array(
// This should be an array of module namespaces used in the application.
'modules' => array(
'DoctrineModule',
'DoctrineORMModule',
'Application',
'Ads',
'Consumer',
'Restful',
'Cron',
'Admin',
'Payment',
'Frontoffice',
'Onboarding',
'Testservices',
),
// These are various options for the listeners attached to the ModuleManager
'module_listener_options' => array(
// This should be an array of paths in which modules reside.
// If a string key is provided, the listener will consider that a module
// namespace, the value of that key the specific path to that module's
// Module class.
'module_paths' => array(
'./module',
'./vendor',
),
// An array of paths from which to glob configuration files after
// modules are loaded. These effectively overide configuration
// provided by modules themselves. Paths may use GLOB_BRACE notation.
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
),
// Whether or not to enable a configuration cache.
// If enabled, the merged configuration will be cached and used in
// subsequent requests.
//'config_cache_enabled' => $booleanValue,
// The key used to create the configuration cache file name.
//'config_cache_key' => $stringKey,
// Whether or not to enable a module class map cache.
// If enabled, creates a module class map cache which will be used
// by in future requests, to reduce the autoloading process.
//'module_map_cache_enabled' => $booleanValue,
// The key used to create the class map cache file name.
//'module_map_cache_key' => $stringKey,
// The path in which to cache merged configuration.
//'cache_dir' => $stringPath,
// Whether or not to enable modules dependency checking.
// Enabled by default, prevents usage of modules that depend on other modules
// that weren't loaded.
// 'check_dependencies' => true,
),
// Used to create an own service manager. May contain one or more child arrays.
//'service_listener_options' => array(
// array(
// 'service_manager' => $stringServiceManagerName,
// 'config_key' => $stringConfigKey,
// 'interface' => $stringOptionalInterface,
// 'method' => $stringRequiredMethodName,
// ),
// )
// Initial configuration with which to seed the ServiceManager.
// Should be compatible with Zend\ServiceManager\Config.
// 'service_manager' => array(),
);
Here is the module.config.php for Restful module
namespace Restful;
return array(
'controllers' => array(
'invokables' => array(
'Restful\Controller\Stateslist' => 'Restful\Controller\StateslistController',
'Restful\Controller\Citieslist' => 'Restful\Controller\CitieslistController',
),
),
'router' => array(
'routes' => array(
'api' => array(
'type' => 'Literal',
'options' => array(
'route' => '/Restful',
'defaults' => array(
'__NAMESPACE__' => 'Restful\Controller',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*'
),
'defaults' => array(
),
),
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'api' => __DIR__ . '/../view',
),
'strategies' => array(
'ViewJsonStrategy'
)
),
);
Here is the module.config.php for Testservices module
namespace Testservices;
return array(
'controllers' => array(
'invokables' => array(
'Testservices\Controller\Userslist' => 'Testservices\Controller\Userslist',
'Testservices\Controller\Roleslist' => 'Testservices\Controller\RoleslistController',
),
),
'router' => array(
'routes' => array(
'api' => array(
'type' => 'Literal',
'options' => array(
'route' => '/Testservices',
'defaults' => array(
'__NAMESPACE__' => 'Testservices\Controller',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*'
),
'defaults' => array(
),
),
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'api' => __DIR__ . '/../view',
),
'strategies' => array(
'ViewJsonStrategy'
)
),
);
Thanks for your help in advance.
Both your routes use the key api in module.config.php I assume when this all gets merged together the last one loaded wins. Change one of them like:
'router' => array(
'routes' => array(
'api_changed' => array(
'type' => 'Literal',
'options' => array(
'route' => '/Testservices',
'defaults' => array(
'__NAMESPACE__' => 'Testservices\Controller',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*'
),
'defaults' => array(
),
),
),
),
),
),
),
Where api has been updated to api_changed

ZF2 - The requested URL could not be matched by routing

i receive this error when i go to
localhost/app/public/spanel/test/index
This application have a sPanel module. Into it have a TestController with indexAction()
This is the sPanel\config\module.config.php
<?php
return array(
'controllers' => array(
'invokables' => array(
'sPanel\Controller\Test' => 'sPanel\Controller\TestController',
),
),
'router' => array(
'routes' => array(
'spanel' => array(
'type' => 'segment',
'options' => array(
'route' => '/test[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'sPanel\Controller\Test',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'spanel' => __DIR__ . '/../view',
),
),
);
First of all i strongly suggest that you start working with virtual hosts to get rid of that annoying localhost/bla/public
Second of all things, you try to access this route: yourdomain/spanel/test/index, but there is no indication of you assigning the part /spanel anywhere in your route, all you assign is /test[...]
So in short, you want to access yourdomain/test/index or you want to modify your route to inclide the /spanel part

Multiple routes under same tree in ZF2

I am having problem while implementing Multiple routes as defined Below in my snippet
EDIT: i am getting this exception too
Additional information:
Zend\Mvc\Exception\InvalidControllerException
with Message
Controller of type Account\Controller\VoucherController is invalid; must implement Zend\Stdlib\DispatchableInterface
<?php
namespace Account;
return array(
'controllers' => array(
'invokables' => array(
'Account\Controller\Account' => 'Account\Controller\AccountController',
'Account\Controller\Voucher' => 'Account\Controller\VoucherController',
),
// --------- Doctrine Settings For the Module
'doctrine' => array(
'driver' => array(
'account_entities' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/Account/Entity')
),
'orm_default' => array(
'drivers' => array(
'Account\Entity' => 'account_entities'
)
)
)
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'account' => array(
'type' => 'segment',
'options' => array(
'route' => '/account[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Account\Controller\Account',
'action' => 'index',
),
),
),
'voucher' => array(
'type' => 'segment',
'options' => array(
'route' => '/account/voucher[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Account\Controller\Voucher',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'account' => __DIR__ . '/../view',
),
),
),
);
Now the issue is i am getting a 404, when i try to access MyHost/account/Voucher
P.S: I already have A Controller under Account/Controller/Voucher and a view under Account/View/Voucher named as index.phtml now i dont know what am i missing here.
As Adnrew and Timdev comments above that there is something not right in your controller, you can check few basic things in your controller, that you have following code correct. specially the typos.
namespace Account\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class VoucherController extends AbstractActionController {
// you acctions
}

ZF2 run the application without using the router

I am learning ZF2.
Is it possible to run the application without Router as in Zf1?
Do we need to define router for each controller?
Example:
In ZF1: "admin/index/index" shows as "module/controller/action"
IN ZF2: "admin/index/index" shows as "router[/:controller][/:action]"
please help me to clear my doubts.
Please tyr this
return array(
// routes
'router' => array(
'routes' => array(
'album' => array(
'type' => 'Literal',
'options' => array(
'route' => '/album',
'defaults' => array(
'controller' => 'album\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__' => 'album\Controller',
),
),
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'album\Controller\Test' => 'Album\Controller\TestController',
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);
You need to add your controller name in invokables manually or invoke via Abstract Factories
'controllers' => array(
'invokables' => array(
'album\Controller\Test' => 'Album\Controller\TestController',
),
),
Automatic Controller Invokables via Abstract Factories
Reference

How to make a custom model for the default Module in Zend Framework 2

Anyone know how to change the default Module in Zend Framework 2? I am using the Skeleton Application as the home page but I want to make another module that I have the default home page.
I tried two things
I removed the "Skeleton Application" from the application.config.php file this is what it looked like
return array(
'modules' => array(
'Application',
'Helloworld'
),
'module_listener_options' => array(
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
),
'module_paths' => array(
'./module',
'./vendor',
),
),
);
this is what it looks like now
return array(
'modules' => array(
'Helloworld'
),
'module_listener_options' => array(
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
),
'module_paths' => array(
'./module',
'./vendor',
),
),
);
if you can't tell I removed the 'Application' from the module
then I changed my module.config.php file for the Helloworld module
here's what it used to look like
return array(
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
'router' => array(
'routes' => array(
'sayhello' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/sayhello',
'defaults' => array(
'controller' => 'Helloworld\Controller\Index',
'action' => 'index',
)
)
)
)
),
'controllers' => array(
'factories' => array(
'Helloworld\Controller\Index' => 'Helloworld\Controller\IndexControllerFactory'
)
),
'service_manager' => array(
'invokables' => array(
'greetingService' => 'Helloworld\Service\GreetingService'
)
)
);
this is what it looks like now
return array(
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
'router' => array(
'routes' => array(
'sayhello' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Helloworld\Controller\Index',
'action' => 'index',
)
)
)
)
),
'controllers' => array(
'factories' => array(
'Helloworld\Controller\Index' => 'Helloworld\Controller\IndexControllerFactory'
)
),
'service_manager' => array(
'invokables' => array(
'greetingService' => 'Helloworld\Service\GreetingService'
)
)
);
the change was made to the 'router' array in the options=>route I changed the value to just '/'
but it throws a 5000 error
can anyone elaborate on what I am doing wrong?
OK so I saw the problem with my application. The routing is actually correct the problem was that I was missing a layout.phtml file I needed to specify all this in the module configuration. I need to add the following to my module.config.php
'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',
'valaree/index/index' => __DIR__ . '/../view/valaree/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
)
you can set your project home page by setting url in module.config.php of application module
(myproject/module/application/config/module.config.php) in home route. For Example, if you want to use Index Action of Index Controller of Default Module as your home page you need to change the home route in above mentioned file. i.e
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Default\Controller\Index',
'action' => 'index',
),
),
),
Please make sure you define your new module in (myproject/config/application.config.php) before using above code. Please let me know if you required any further help. Thanks..:)

Categories