Unable to route anything except '/' in Zend framework 2 - php

I am using the Zend Framework 2 with Zend Studio 10 and Zend Server.
The skeleton application run fine, but anything except '/' cannot get routed too, it just returns a "The requested URL was not found on this server"
Here's the module.config, which is almost identical to the skeleton one except I added a controller "Foo" with an action "lol" on the route "/lol" but it does not work.
<?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
*/
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' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/lol',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Foo',
'action' => 'lol',
),
),
'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(
'factories' => array(
'translator' => 'Zend\I18n\Translator\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' => 'Application\Controller\IndexController',
'Application\Controller\Foo' => 'Application\Controller\FooController',
),
),
'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',
),
),
);
Same thing, if I would change the route "/" to "/asdf" (and nothing else) then try to run localhost/Test/asdf rather than localhost/Test/ I would get "URL not found" again.
EDIT: I checked the error log in apache2 and this is what it said:
[Mon Jan 07 14:45:58 2013] [error] [client 127.0.0.1] File does not exist: C:/Program Files (x86)/Zend/ZendServer/data/apps/http/__default__/0/Test/1.0.0/public/lol
[Mon Jan 07 14:46:03 2013] [error] [client 127.0.0.1] File does not exist: C:/Program Files (x86)/Zend/ZendServer/data/apps/http/__default__/0/Test/1.0.0/public/lol
What could be causing it?

I solved it, by configuring the Zend apache2 server and set "AllowOverride None" to "AllowOverride All". I do not know specifically why it helped though, would be glad if someone pointed that out.

Related

How to configure Zend 2 on a hosting server?

I created a virtual host: www.attila-naghi.ro in the xampp. I put my project there and it works perfectly. Now I want to put it online, on a hosting server. I observed that my css and js are not loaded and if I access the controllers, I get the 404. This is the URL. Can anyone help me? thx
This is my module.config.php file:
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* #link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* #copyright Copyright (c) 2005-2014 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',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '[:controller[/:action]][/:param1]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
),
'defaults' => array(
'action' => 'index',
'__NAMESPACE__' => 'Application\Controller',
// 'param1' => 'tralala'
)
)
)
)
),
),
),
'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\Create' => 'Application\Controller\CreateController',
'Application\Controller\Blog' => 'Application\Controller\BlogController',
'Application\Controller\Portofolio' => 'Application\Controller\PortofolioController',
'Application\Controller\User' => 'Application\Controller\UserController',
),
),
'view_manager' => array(
'base_path' => 'http://www.attila-naghi.com/',
'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(
),
),
),
);
If you take a look at the Skeleton Application you can see the /public directory. The contents of those are supposed to go into whatever your server's public directory is. For example, for servers with cPanel it's the public_html. Other hosting servers may have www, or something else.
I've recently uploaded a zend skeleton app doing exactly as above and it worked perfectly. If you wonder what to do with the original (Skeleton's) directory /public - I left it along with my public_html but you can remove it as it is empty.

Zend framework 2 https link not working

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.

ZF2 PaginationControl Disaply Wrong URL

When I Click - my index page -
localhost/gov_app/public/approval_allowances/institute/index
It displays the Data Set with Pagination.If i click "<<" or ">>" then it goes to localhost/gov_app/public/approval_allowances/?page=2
Unfortuantely ZF2 Pagination Control Gives Wrong Links.Here is my Codes
index.phtml
<?php echo $this->paginationControl($this->paginator, 'Elastic', 'approval_allowances/institute/partial/paginator.phtml') ?>
<?php
// add at the end of the file after the table
echo $this->paginationControl(
// the paginator object
$paginator,
// the scrolling style
'sliding',
// the partial to use to render the control
array('approval_allowances/institute/partial/paginator.phtml','Approval_allowances'),
// the route to link to when a user clicks a control link
array('route' => 'institute')
);
?>
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-2014 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' => 'Approval_allowances\Controller\Index',
'action' => 'index',
),
),
),
'institute' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/approval_allowances/institute',
'defaults' => array(
'controller' => 'Approval_allowances\Controller\Institute',
'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
'allowance_approval' => array(
'type' => 'Literal',
'options' => array(
'route' => '/approval_allowances',
'defaults' => array(
'__NAMESPACE__' => 'Approval_allowances\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(
'Approval_allowances\Controller\Index' => 'Approval_allowances\Controller\IndexController',
'Approval_allowances\Controller\Institute' => 'Approval_allowances\Controller\InstituteController'
),
),
'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',
'approval_allowances/index/index' => __DIR__ . '/../view/approval_allowances/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(
),
),
),
);
Change your index.phtml file to include the index action along with the route. Like this:
<?php echo $this->paginationControl($this->paginator, 'Elastic', 'approval_allowances/institute/partial/paginator.phtml') ?>
<?php
// add at the end of the file after the table
echo $this->paginationControl(
// the paginator object
$paginator,
// the scrolling style
'sliding',
// the partial to use to render the control
array('approval_allowances/institute/partial/paginator.phtml','Approval_allowances'),
// the route to link to when a user clicks a control link
array('route' => 'institute'
'options' => array(
'action' => 'index'
)
)
);

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

ZF2 new module breaks routing

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.

Categories