I am newbie to zend framework and i am trying to configure the zend. I was sucessfully installed zendskeleton applicaion on window 7 and XAMPP
After installation I am creating new module Album as per define in user guide. I was make all code and pages according to guide, but after that i was enable to open Album module. i got error 404 not found.
here code
application.config
return array(
'modules' => array(
'Application','Album',
),
'module_paths' => array(
'./module',
'./vendor',
),
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
),
),
);
module.config
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' => 'Album\Controller\AlbumController',
),
),
'router' => array(
'routes' => array(
'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',
),
),
);
Module.php
namespace Album;
// Add these import statements:
use Album\Model\Album;
use Album\Model\AlbumTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
class Module
{
// getAutoloaderConfig() and getConfig() methods here
// Add this method:
public function getServiceConfig()
{
return array(
'factories' => array(
'Album\Model\AlbumTable' => function($sm) {
$tableGateway = $sm->get('AlbumTableGateway');
$table = new AlbumTable($tableGateway);
return $table;
},
'AlbumTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Album());
return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
},
),
);
}
}
httpd-vhosts.conf
<VirtualHost *:81>
ServerName zf2-tutorial.localhost
DocumentRoot "C:/xampp\htdocs/ZendSkeletonApplication/ZendSkeletonApplication-master/public"
SetEnv APPLICATION_ENV "development"
<Directory C:/xampp\htdocs/ZendSkeletonApplication/ZendSkeletonApplication-master/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
host entry at system32
127.0.0.1:8081 zf2-tutorial.localhost
How can i handle it.
Thanks
when you point with your apache document root to C:/xampp\htdocs/ZendSkeletonApplication/ZendSkeletonApplication-master/public
you need to use in your browser this url http://zf2-tutorial.localhost:8081/album
and not like you wrote http://zf2-tutorial.localhost/ZendSkeletonApplication/ZendSkeletonApplication-master/public/album
this url points internal to a different module/location.
//edit
if this not work check your zf2 /public folder if there is a .htaccess file present otherwise use the file from the zend skeleton application here https://github.com/zendframework/ZendSkeletonApplication/blob/master/public/.htaccess
please check also your apache vhost entry if the port is equal to your windows host file port.
make sure apache ModRewrite is loaded!
Basic misconfigurations in your setup causes this error.
I don't use windows for along time but you're using both forward and backslashes in your paths. Firstly, you should find the correct directory separator for windows and stick with it. This seems problematic: C:/xampp\htdocs/foo/bar/public
You're defining a virtualhost which listens any IP address on port 81 (*:81), a system32 hosts entry which points to port 8081 as zf2-tutorial.localhost alias and trying to call zf2-tutorial.localhost/album url using port 80. Getting this kind of error pretty normal.
After completely reading the official Getting Started and Using Apache Web Server documentations, you'll easily figure out the solution.
It seems you missing a vhost entry in your webserver. This could the reason why your application doesn't resolve your request right. Check the "getting startet: A skeleton application" again, to provide a proper configuration.
You need this hostfile entry. Adding ports is not possible in this file.
127.0.0.1 zf2-tutorial.localhost
The website is now available at zf2-tutorial.localhost:81 (and not 8081, you set 81 as the portnumber in the first line of httpd-vhosts.conf)
Related
I'm using Zendframework (2.3), I'm struggling to understand what I'm doing wrong when attempting to create a new action and view on an existing controller. I've read some relevant documentation but still fail to see what I'm missing.
Currently the controller basically defaults to a single action (main), I would like to add an additional one.
EG:
/list-item/main => // Existing Route
/list-item/add => // New Route I would like to add.
This is how my module.config.php looks:
return array(
'router' => array(
'routes' => array(
'list-item' => array(
'type' => 'segment',
'options' => array(
'route' => '/list-item[/:action][/:id][/:id1][/:id2][/:id3][/:id4][/:id5]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
),
'defaults' => array(
'controller' => 'ListItem',
'action' => 'main',
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'ListItem' => 'ListItem\Controller\ListItemController',
),
),
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
If I can read this configuration correctly, the actual action name is optional, but the module will default to the main action. Yet it is flexible to allow for any action to be attempted.
So, I proceeded to add a new public function into the ListItemController, assumed this is how the convention works:
public function addAction() {
return new ViewModel();
}
And with it a new view file into the module's views folder, in fact right next to main.phtml but called add.phtml.
But when I attempt to access the route /list-item/add I only get a permission denied. Funny, because the actual status is 200. But I have no other information. I honestly don't even know if this is a ZF thing, I can only assume.
Also, I'm using php -S 0.0.0.0:8080 -t public/ in case the web server might have something to do.
Thanks in advance, any help will be greatly appreciated.
I'm receiving the following error in a Zend Framework 3 Application:
Fatal error: Uncaught Zend\ModuleManager\Exception\RuntimeException: Module (Serve) could not be initialized.
I'm aware that there is some answers however none seem to point to zf3 and ive already scanned them without answer. I cannot seem to find an answer through research.
Is it possible that my application is not loading modules? I have modified the application config just a tad so it might just not be loading the module itself.
I have a folder structure:
- module
-Serve
-src
-Module.php
-Controller
-IndexController.php
-config
-module.config.php
-view
I have the module added to the modules array inside /config/application.config.php.
Here is my module.config.php
namespace Serve;
return array(
'controllers' => array(
'invokables' => array(
'Serve\Controller\Index' => 'Serve\Controller\IndexController',
),
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'serve' => array(
'type' => 'segment',
'options' => array(
'route' => '/srv[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
),
'defaults' => array(
'controller' => 'Serve\Controller\Index',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
'strategies' => array(
'ViewJsonStrategy',
),
),
);
Here is my Serve\Module.php file:
<?php
namespace Serve;
class Module
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
}
I have a bunch of business logic inside my Application\Module.php however nothing that looks to disrupt loading modules.
I cannot seem to find an answer through research. What could be wrong here?
Did you add the module to the autoloader? https://github.com/zendframework/ZendSkeletonApplication/blob/master/composer.json#L23
In ZF2, we used to autoload pretty much anything through the Module class, now we can just do it in composer, which is easier and allow options such as --optimize (generate classmaps) and --classmap-authoritative (do not load any class outside of the classmap).
Don't forget to composer dumpautoload after editing the composer.json file :)
So I've been following the fairly straightforward zend 2 skeleton example "album". I followed every step to the teeth and yet I cannot escape the 404 Error - requested URL could not be matched by routing whenever I input http://hostname/album for indexing, or http://hostname/album/add for adding, etc.
Naturally I looked into the routing found in the module.config.php file:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' => 'Album\Controller\AlbumController',
),
),
'router' => array(
'routes' => array(
'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',
),
),
);
Everything here looked fine, so I looked into the Module.php where the module.config.php is getting loaded from:
<?php
namespace Album;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements AutoloaderProviderInterface, ConfigProviderInterface
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
}
Again, everything looks fine here. Now I thought maybe the problem is that I didn't include the Album module in the application.config.php file (comments removed):
<?php
return array(
'modules' => array(
'Application',
'Album',
),
'module_listener_options' => array(
'module_paths' => array(
'./module',
'./vendor',
),
'config_glob_paths' => array(
'config/autoload/{{,*.}global,{,*.}local}.php',
),
);
However it is included. I also have the AlbumController.php and the view (.phtml) files exactly where they should be. I double checked the paths multiple times yet the routes still do not work. Any ideas? Any suggestion would be appreciated.
PS - I am using a Ubuntu 14.04 Virtual Box.
EDIT
Here's the directory structure for the application: (I'm just listing the relevant files/folders to make it more readable)
ZendSkel
public
index.php
config
application.config.php
module
Application
Album
Module.php
config
module.config.php
src
Album
Controller
AlbumController.php
Model
Form
view
album
album
index.phtml
add.phtml
edit.phtml
delete.phtml
Also, I am using virtual host with apache2.2.
For anyone, who is still looking for solution of this problem,
Clearing the data/cache folder, allowed routing to work as expected.
Like #Mayank Awasthi said:
Clearing the data/cache folder, allowed routing to work as
expected.
This applies to Zend AND Laminas.
If the issue is not cache related, check your config and routing files!
If you follwoing the tutorial in the official page step by step and it shows 404. make sure 1- stop the serve,
2- enter "composer development-enable".
3- enter "composer serve"
and then it should work. The tutorial does mention "composer development-enable" but it doesnt mention it in the right order, reason why a lot of people keep getting the 404
Good morning,
I'm currently learning Zend 2 but already have experience in MVC. I installed the current zend2 skeleton application, which is reachable under http://learningzend.local/ on my local machine.
Now, I'm trying to understand Zends routing but I'cant reach /application/index/index. It returns 404.
Here is an excerpt from module.config.php. The only thing I changed was the type in the two latter routes, it was originally set to 'Literal' and 'Segment' without the path. The home route seems to work: when I change the action, the homepage disappears.
'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' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Zend\Mvc\Router\Http\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(
),
),
),
),
And few lines below
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController'
),
),
The controller is completely unchanged
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function indexAction()
{
return new ViewModel();
}
}
I tried everything. Resetted the whole skeleton app to default but it did not change anything. Hope some Zend gurus can tell me whats wrong.
For completeness my VHOST entry
<VirtualHost *>
ServerName learningzend.local
ServerAdmin webmaster#localhost
DocumentRoot /var/www/learningzend.local/public/
AccessFileName .htaccess
<Directory /var/www/learningzend.local/public/>
Options Indexes FollowSymLinks MultiViews
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Here comes....the solution!
The Apache2 setting AllowOverride in my VHOST conf does not override the same setting in apache2.conf, even if they point to different directories. So, go to
$ sudo joe /etc/apache2/apache2.conf
Change the Setting at /var/www to
AllowOverride All
$ sudo service apache2 restart
There are three modules in my app/modules/, named Application, Album, Shop. I want to access each of these modules using HOSTANME/application, HOSTNAME/album, and HOSTNAME/shop. For Application modules, the configuration is:
'router' => array(
'routes' => array(
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'AppliWcation\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(
),
),
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController',
.........
),
},...
There are multiple Controllers under Application module. Via this method, I can accessed URL like:
http://www.myhostname.com/application/controllerName/actionName
It is the similar configuration for other two modules.
This works previous T61 thinkpad at beginning of last month. Then I reinstalled Php/apache in my new bought T410. I also git pull latest ZF2 version:
git log
commit 73e0de644789aa5b712c97f55c512c57c2b9ac76
Merge: 7c65991 d722da5
Author: Matthew Weier O'Phinney <matthew#zend.com>
Date: Fri Oct 4 11:01:06 2013 -0500
Merge branch 'hotfix/version-composer'
Fix for issue introduced with #5191
But everything does not work anymore. I checked the error.log:
[Sun Oct 06 10:47:05 2013] [error] [client 127.0.0.1] File does not exist: /path/to/app../ChipShop/public/application
However, it works for default route http://www.myhostname.com.
Is there any wrong in my configuration or some configuration rule has been changed? Thanks
[UPDATE]
There is similar post ZF2 routing configuration, I tried method:
'user' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/user',
'defaults' => array(
'controller' => 'Application\Controller\User',
'action' => 'some-action',
),
),
) ,
But it still not work for me..
SOLUTION
The issue is not in code side. It is in my confiuration.
1, I forgot to enable apache rewrite module. As what said in akond post.
2, In virtualHost confiuration,
AllowOverride None
should be changed to
AllowOverride All
https://github.com/zendframework/zf2 clearly states that the master branch contains 2.2.5dev. Dev releases are potentially unstable and you should not rely on them. Use composer with
"require": {
"zendframework/zendframework": "~2.2.0"
}
and you will be just fine.
Update
The error message says it cannot find the application directory. Of course it does not exist, since it is virtual. This means that problem is not in the ZF routes at all. Check that
.htaccess file exists in the public directory and is of correct content
Appache is configured to parse this .htaccess file
mod_rewrite module is on.
the directory up to your public directory does exist
and the Virtual Host is attached to this directory.