Zend framework 2 https link not working - php

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.

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.

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.

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

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

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