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',
),
Related
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',
),
),
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',
),
),
...
...
);
Site links:
http://test.scampaigns.com/Frontend/index
https://test.scampaigns.com/Frontend/index
Problem:
Number 1 is working, number 2 is giving 404 error.
The problem is with HTTP the site is working fine. but with HTTPS only the default controller is working but other controllers are not working with the https:
Below is my module.config.php
<?php
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' => '/', //edited by koushik
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '[:controller[/:action]][/:id][/:pId][/:devId]', //edited by koushik
'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',
'Application\Controller\Developer' => 'Application\Controller\DeveloperController', // edited by Poulami
'Application\Controller\Template' => 'Application\Controller\TemplateController', // edited by Poulami
'Application\Controller\Admin' => 'Application\Controller\AdminController',
'Application\Controller\Frontend' => 'Application\Controller\FrontendController',
'Application\Controller\Ajaxcall' => 'Application\Controller\AjaxcallController'
),
),
'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',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),// Added by Baishakhi
// Placeholder for console routes
'console' => array(
'router' => array(
'routes' => array(
),
),
),
);
Is there any one who can help me and give me solution?
zend doesn't matter what url scheme is used when trying to find a RouteMatch with your router configuration.
i think your problem is located in your apache configuration that your https vhost don't allow .htaccess files in your public folder.
when all urls fail and the index.php is working - this indicates a rewrite problem. check your apache configuration and that your public folder can override htaccess files with the option
AllowOverride All
If you want to use https in your route you need to use the Zend\Mvc\Router\Http\Scheme router. All you need is to add an option in your route configuration: 'scheme' => 'https'.
You can follow this simple example :
'router' => array(
'routes' => array(
'name_route' => array(
'type' => 'Scheme', //<-----add the type Schema here.
'options' => array(
'route' => '/url',
'scheme' => 'https', //<-----specify this here.
'defaults' => array(
//set the default options here.
),
),
),
// ....
),
),
You can also see this post.
I'm having an odd problem. If I have 1 module my routes are correctly matched and the pages returned. Add another module to the application config and BANG - it stops working and ends up in the Module.php of my second module (which has boostrapping and triggers loads of events).
application.config.php
<?php
return array(
'modules' => array(
'CMS',
'AOCS',
),
'module_listener_options' => array(
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
),
'module_paths' => array(
'./module',
'./vendor',
),
),
);
CMS module config
<?php
return array(
'router' => array(
'routes' => array(
'Admin' => array(
'type' => 'Literal',
'options' => array(
'route' => '/admin',
'defaults' => array(
'__NAMESPACE__' => 'CMS\Controller',
'controller' => 'Index',
'action' => 'Login',
),
),
'may_terminate' => true,
),
'Logout' => array(
'type' => 'Literal',
'options' => array(
'route' => '/logout',
'defaults' => array(
'__NAMESPACE__' => 'CMS\Controller',
'controller' => 'Index',
'action' => 'Logout',
),
),
),
'CMS/Welcome' => array(
'type' => 'Literal',
'options' => array(
'route' => '/aocs/welcome',
'defaults' => array(
'__NAMESPACE__' => 'AOCS\Controller',
'controller' => 'Welcome',
'action' => 'Welcome',
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'CMS\Controller\Index' => 'CMS\Controller\IndexController',
),
),
'translator' => array(
'locale' => 'en_GB',
),
'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__ . '/../templates/admin.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
AOCS module config
<?php
return array(
'router' => array(
'routes' => array(
'Welcome' => array(
'type' => 'Literal',
'options' => array(
'route' => '/welcome',
'defaults' => array(
'__NAMESPACE__' => 'AOCS\Controller',
'controller' => 'Welcome',
'action' => 'Welcome',
),
),
),
'Mains' => array(
'type' => 'segment',
'options' => array(
'route' => '/aocs[/:controller][/:action][/:id]',
'defaults' => array(
'__NAMESPACE__' => 'AOCS\Controller',
'controller' => 'Welcome',
'action' => 'Welcome',
),
),
'may_terminate' => true,
'child_routes' => array(
'query' => array(
'type' => 'Query',
),
),
),
),
),
'navigation' => array(
'menu' => array(
'page-1' => array(
'label' => 'Logout',
'route' => 'Logout',
'resource' => 'aocs_index_logout'
),
'page-2' => array(
'label' => 'Login',
'route' => 'Admin' ,
'resource' => 'cms_index_login'
),
),
),
'service_manager' => array(
'factories' => array(
'menu' => 'AOCS\Navigation\MenuNavigationFactory'
),
),
'controllers' => array(
'invokables' => array(
'AOCS\Controller\Welcome' => 'AOCS\Controller\WelcomeController'
),
),
'translator' => array(
'locale' => 'en_GB',
),
'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__ . '/../templates/admin.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
/*'strategies' => array(
'ViewJsonStrategy',
),*/
),
);
Module.php from CMS
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* #link http://github.com/zendframework/ZendSkeletonApplication 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 CMS;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\View\Renderer\JsonRenderer;
use Zend\Di\Di;
use Zend\Config\Reader\Ini;
use Zend\Navigation\Navigation;
// AH core code
use Core\Classes\setDB;
use Core\Classes\setCache;
use Core\Models\SecurityModel;
use Core\Models\AclModel;
class Module
{
public function onBootstrap(MvcEvent $e)
{
$sm = $e->getApplication()->getServiceManager();
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
// Set config - required throughout
$reader = new Ini;
$sm->setService('configIni',$reader->fromFile('./config/application.ini'));
// Setup a Database connection
$sm->setService('setDB',new setDB($sm,'database'));
// Setup Caching
$sm->setService('setCache',new setCache($sm));
/*$eventManager->attach('dispatch', function ($sm) use ($controllers) {
print'<pre>[CMS]';print_r($sm->getRouteMatch());print'</pre>';
exit;
}, 100); // execute before executing action logic*/
// Assign system names to view models so we can set across templates
$view = $e->getViewModel();
$config = $sm->get('configIni');
$view->setVariable('systemname',$config['system']['name']);
$view->setVariable('systemshort',$config['system']['shortname']);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}
Am I missing something obvious? Its really annoying!
Thanks
Antony
Fixed it!
The BANG was that I have a dispatch in the 2x Modules.php which for some reason were triggering automatically. As the 2nd checked if the user is logged in and they weren't it redirected causing problems.
The issue wasn't to do with my config it appears (unless I'm missing the obvious) that this is a bug in ZF2 where the dispatch events are not tied to the module they're called from but all get run automatically.
So if my page is in CMS module the AOCS modules > bootstrap is still run. Why??
Thanks for the point about type = Query - will update.
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..:)