zend framework 2 nested module structure in one main module - php

I am trying to create a app using zend framework 2. Have used structure as follow. Getting error class not found. Trying to created nested sub module login inside a main module called album.
I have following structure:
Album
- src
- Album
- Controller
- AlbumController.php
- Form
- AlbumForm.php
- Model
- Login
- Controller
- LoginController.php
- Form
- LoginForm.php
- Model
Getting following error:
Zend\Mvc\Controller\ControllerManager::createFromInvokable: failed retrieving "logincontrollerlogin(alias: Login\Controller\Login)" via invokable class "Login\Controller\LoginController"; class does not exist
Routing is as follow:
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' => 'Album\Controller\AlbumController',
'Login\Controller\Login' => 'Login\Controller\LoginController',
),
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'login' => array(
'type' => 'segment',
'options' => array(
'route' => '/login',
'defaults' => array(
'controller' => 'Login\Controller\Login',
'action' => 'index',
),
),
),
'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',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
'login' => __DIR__ . '/../view',
),
), );
Can you please help me to resolve the above error:
Autoload in Module.php:
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}

Your autoloader probably can't find your controller because it's under a different namespace. Try specifying the Login namespace and see if it helps.
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
'Login' => __DIR__ . '/src/Login',
),
),

Related

Page not found creating a new action controller in Zend Framework 2

I've got a big problem because I can see index.html give it by IndexController, but if I create another action, for example, getdataAction() I have the next error:
I think that I have not to configure anything to do that, but these are my files:
module_config.php
namespace Application;
return array(
'doctrine' => array(
'driver' => array(
'application_entities' => array(
'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/Application/Entity')
),
'orm_default' => array(
'drivers' => array(
'Application\Entity' => 'application_entities'
)
)
)
),
'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',
),
'factories' => array(
'translator' => 'Zend\Mvc\Service\TranslatorServiceFactory',
),
),
'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' => Controller\IndexController::class
//'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(
),
),
),
);
IndexController.php
<?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Entity\UserEntity;
class IndexController extends AbstractActionController
{
protected $_objectManager;
public function indexAction(){
try{
$users = $this->getObjectManager()->getRepository('\Application\Entity\UserEntity')->findAll();
foreach ($users as $user){
echo "[id]: " . $user->getId() . " Nombre: " . $user->getFullName() . "<br />";
}
}catch(Exception $ex){
echo "error: " . $ex->getMessage();
}
return new ViewModel(array("users" => $users));
}
public function getdataAction(){
return new ViewModel();
}
protected function getObjectManager()
{
if (!$this->_objectManager) {
$this->_objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
}
return $this->_objectManager;
}
}
The directory structure is the next:
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/Application',
'defaults' => array(
__NAMESPACE__ => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
This code is wrong you have to quote this NAMESPACE key
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/Application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
It will be fix this issue
Index(resolves to invalid controller class or alias: Index)
In the other hand you use this :
'invokables' => array(
'Application\Controller\Index' => Controller\IndexController::class
You almost have this right, but you have to put the complete Full Qualified Class Name (FQCN)
'invokables' => array(
'Application\Controller\Index' => Application\Controller\IndexController::class
EDIT :
You define your main route as /Application and
'defaults' => array(
__NAMESPACE__ => 'Application\Controller',
'controller' => 'Index',
ucfirst matters here try it.
http://localhost/Application/Index/getdata
and
http://localhost/application/index/getdata
You don't have any route to your action.
The skeleton application defines a catch all route as standard, which you call via:
/application/:controller/:action
So you should be able to use:
/application/index/getdata
To get a better route you'll need to define one in module_config.

Zend Framework class not loading in Ubuntu

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. :)

How do I access a function from a controller in zend 2?

Finally, I succeeded installing zf2 , now I'm trying to accessing a function but i get the 404 page .
This is my code :
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController{
public function indexAction(){
return new ViewModel();
}
public function addAction(){
echo 1;
}
public function editAction(){
}
public function deleteAction(){
}
}
when i access mylink/add it redirects me to 404. Why ?
This is the route:
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(
),
),
),
);
Can someone explain me where i have to modify the route to access the function addAction??? or to access a new controller ? for example : myproject/mynewcontroller or myproject/add ? thx
You need to configure a route to call that function, something along the lines of:
'router' => array(
'routes' => array(
'index' => array(
'type' => 'Literal',
'options' => array(
'route' => '/add',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'add',
),
),
I'm not entirely sure if this code is valid, since I haven't used routing in zend in a while, but it should be something in that direction. Refer to the zend framework docs for more ino: http://framework.zend.com/manual/current/en/modules/zend.mvc.routing.html
It might be possible you forget to add controller invokables in your module.config.php file try this:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController',
),
),
...
...
);

New module configuration in zend framework 2

I'm trying to create restful api module with zend framework 2.
I'm unable to create proper module.config.php
My folder structure for new module is:
[...]/module/Api
[...]/module/Api/config
[...]/module/Api/src/Api/Controller
My controller is named: ShortenerController.php located in
[...]/module/Api/src/Api/Controller/ShortenerController.php
In it I have namespace setted as:
namespace Api\Controller;
In [...]/module/Api/config I have module.config.php file which has this code:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Api\Controller\Shortener' => 'Api\Controller\ShortenerController',
),
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'Api' => array(
'type' => 'segment',
'options' => array(
'route' => '/api/s/[:url]',
'defaults' => array(
'controller' => 'API\Controller\Shortener',
),
),
),
),
),
'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',
// 'index/index' => __DIR__ . '/../view/index/index.phtml',
// 'error/404' => __DIR__ . '/../view/error/404.phtml',
// 'error/index' => __DIR__ . '/../view/error/index.phtml',
// ),
// 'template_path_stack' => array(
// 'application' => __DIR__ . '/../view',
// ),
'strategies' => array(
'ViewJsonStrategy',
),
),
);
When I'm trying to invoke curl with post data to this link:
http://server_address/api/s/
I'm getting error such as this:
PHP Fatal error: Class 'Api\\Controller\\ShortenerController' not found in /home/ertai/zf/library/Zend/ServiceManager/AbstractPluginManager.php on line 170
What I'm doing here wrong? I can't get a grasp on what I should write into module.config.php file in order to have proper route.
It seems that you forgot to declare your module in application.config.php like this :
return array(
'modules' => array(
'Application',
'Api',
),
And by the way, take care in your default route configuration
'defaults' => array(
'controller' => 'API\Controller\Shortener',
),
This is case-sensitive, so it should be :
'defaults' => array(
'controller' => 'Api\Controller\Shortener',
),
And I advise you to set a default action too like this :
'defaults' => array(
'controller' => 'Api\Controller\Shortener',
'action' => 'index',
),

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