I have been following the tutorial from Zend (http://www.youtube.com/watch?feature=player_embedded&v=EerB9bTvqrY) however when I add a new Module in my project I can not navigate to it, are the instructions in this tutorial incorrect?
Basically when I add a new Module in Zend Studio to my Zend Framework project I can not navigate to it, my new module is called "deals". I navigate to localhost/dealproject/deals and I get error 404. When navigating to localhost/dealproject/ it loads the zend skeleton application page correctly.
Thanks for your help.
module.config.php
<?php
return array(
'controllers' => array(
'invokables' => array(
'Deals\Controller\Deals' => 'Deals\Controller\DealsController',
),
),
'router' => array(
'routes' => array(
'deals' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/deals',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'Deals\Controller',
'controller' => 'Deals',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
// This route is a sane default when developing a module;
// as you solidify the routes for your module, however,
// you may want to remove it and replace it with more
// specific routes.
'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(
),
),
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'Deals' => __DIR__ . '/../view',
),
),
);
Make sure you've enabled the module in ~/config/application.config.php
'modules' => array(
'Application',
'Deals',
),
To me the route configuration looks correct and it should show results. What I noticed is that your base URL (localhost/dealproject/) for the ZF2 application is in a sub directory of the domain's root. As you probably know all the requests are mapped to the index.php of your application (~/public/index.php). This is done by some configuration in your .htaccess file (in the same directory).
In the example URL www.example.com/blog if blog doesn't exists as a folder under www.example.com's root you get a 404 Page not found. However, the .htaccess of ZF2 makes apache call the index.php in the domain's root if no directory is found (this assumes you use the ZendSkeletonApplication).
Now, since your index.php is not in the domain root (localhost/) but in localhost/dealproject/ and your .htaccess is (I assume) in localhost/dealproject/ as well, when you call localhost/dealproject/deals apache is looking for a directory dealproject/deals since in localhost there is no .htaccess to prepare the necessary configuration.
I would advise you to make dealproject folder the root of localhost. This will enable you to call your route like this: localhost/deals and .htaccess and index.php will be processed correctly.
Hope this helps :)
Stoyan
Related
Helle ZF2 Guru! Normally we get the zf2 translation file from a directory in Appliction module.config.php like this: 'base_dir' => DIR . '/../language' .
Is it possible to get it from a Uri?
Application module.config.php:
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',//http://example.com/
'pattern' => '%s.mo',
),
),
),
Why this is important?Correct me if I am wrong! In a multilingual application, language files are static files and for a real world application any static files better be on cloud like AWS CloudFront or CDON for better performance of Application and longer caching period.
I don't think this way is possible and I think it's not interresting to have this file outside the App directory because it's a part of your PHP software which will be interpreted and "compiled" by your web server at each HTTP request.
It's interresting for a JSON file downloaded by the navigator with AJAX request for example because the navigator will store the file in cache.
Our solution was to determine what is the current language in application.config.php:
define ('SITE_LANG', ...);
In our case it was simply the first two letters of the path:
www.site.com/en/...
Then in module.config.php:
'translator' => [
'locale' => SITE_LANG,
'translation_file_patterns' => [
[
'type' => 'phparray',
'base_dir' => 'vendor/zendframework/zendframework/resources/languages/',
'pattern' => '%s/Zend_Validate.php',
],
and it did the trick.
I seem to have a problem configuring Yiistrap in the Yii basic template (which I used as a starting point for my web-site).
What I aim to do is adding icons in my NavBar and using an image file as the brand in the NavVar. It seems the normal NavBar of Yii is unable to do this as the "brand' option is not recognized.
So I installed YiiStrap using composer in my vendor directory. But then it is said in http://www.getyiistrap.com/site/started to adjust my main.php file (but here isn't one)!
So basically, how do I configure this correctly so I can add the extra features in the navbar?
Kind regards
Code which I used (composer):
Downloaded Yiistrap in /basic/vendor/2amigos/yiistrap
Config directory has these files:
console.php
db.php
main.php
params.php
web.php
The code of main.php is
<?php
// main configuration
return array(
// path aliases
'aliases' => array(
'bootstrap' => realpath(__DIR__ . '/vendor/2amigos/yiistrap'), // change this if necessary
),
// import paths
'import' => array(
'bootstrap.helpers.TbHtml',
),
// application modules
'modules' => array(
'gii' => array(
'generatorPaths' => array('bootstrap.gii'),
),
),
// application components
'components' => array(
'bootstrap' => array(
'class' => 'bootstrap.components.TbApi',
),
),
);
Maybe I am overcomplicating this, but the documentation isn't very clear to me. I am using Zend Framework 2 to serve some dynamic content, but I also have a few routes that are purely static HTML pages. Those static pages are all children of a parent route. For example:
/foo/bar
/foo/baz
/foo/cat
How can I just simply serve up these static pages if I already have a "FooController" I should add that /foo doesn't have a view itself, but all of the foo children do.
The easiest would be to set up your route like this:
'foopage' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/foo/:action',
'defaults' => array(
'controller' => 'My\Controller\Foo',
'action' => 'index'
),
'constraints' => array(
'action' => '[a-zA-Z]+'
)
),
)
Then inside your FooController simply create a couple of empty actions like indexAction, fooAction, barAction, bazAction and create the respective templates.
An alternative would be to use the Module of Matthew called PhlySimplePage
DISCLAIMER: I'm a complete noob to Zend.
I'm evaluating Zend Framework 2 at work, and trying to configure it to work with ZfTwig for templating. (See here: https://github.com/mtymek/ZfTwig)
I got through Step 3 of the config ok, but I can't figure out Step 4.
I tried placing the following in application.config, but no good.
Where am I supposed to put this?
return array(
'di' => array(
'instance' => array(
// setup other stuff...
// ...
// setup view script resolvers - very similar to configuration
// from ZendSkeletonApplication
'Zend\View\Resolver\AggregateResolver' => array(
'injections' => array(
'Zend\View\Resolver\TemplateMapResolver',
'ZfTwig\TemplatePathStack',
),
),
'Zend\View\Resolver\TemplateMapResolver' => array(
'parameters' => array(
'map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.twig',
),
),
),
'ZfTwig\TemplatePathStack' => array(
'parameters' => array(
'paths' => array(
'application' => __DIR__ . '/../view',
),
),
),
// Tell TwigRenderer how it should locate .twig files
'ZfTwig\TwigRenderer' => array(
'parameters' => array(
'resolver' => 'Zend\View\Resolver\AggregateResolver',
),
),
),
);
Google is no help... I can't find any documentation on Zend's site or anywhere telling me where this is supposed to go.
Thanks for the help!
The di configuration is from the first betas of Zend Framework 2. Zend\Di is a component still available, but internally (as with many other modules) replaced by Zend\ServiceManager.
Basically, both are able to provide dependency injection. Only for Zend\Di it can do this kind-of automatically and for Zend\ServiceManager there are other options to make dependency injection more explicit.
To give an answer to your question: ZfcTwig is now part of ZF-Commons and https://github.com/ZF-Commons/ZfcTwig is the location you have to search for now. Just for your insights, this file is an example of a factory used by the service manager. For more background of service managers in Zend Framework 2, I have written a blog post two months ago which might be interesting.
I've created a module, a basic copy of the the albums example given in the ZF2 documentation, however, with the new module, I am not able to access it at all - I'm always given a 404 error. I'm building this on the ZF2 skeleton.
I've got three modules loaded: Application, Frontend and Security.
Both Frontend and Security are duplicates of each other, however, I have thoroughly checked and there is no reference to old code (as I literally copied the module folder and renamed/rewrote references).
The module is also loaded in application.config.php.
Any ideas on what I'm missing?
Module Config:
return array(
'controllers' => array(
'invokables' => array(
'Security\Controller\Security' => 'Security\Controller\SecurityController',
),
),
'router' => array(
'routes' => array(
'security' => array(
'type' => 'segment',
'options' => array(
'route' => '/security[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Security\Controller\Security',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'security' => __DIR__ . '/../view',
),
),
);
I had the same problem while following the skeleton application tutorial (Getting started: A skeleton application). Whenever I would go to the album url in the browser (ZendSkeletonApplication/public/album in my case), I would get a 404 error page but no details on why I got the 404. It wasn't clear to me how I would be able determine why I was getting the 404 when I had double checked everything and was pretty sure I copied and configured the Album module properly. It turned out that I was missing a slash in my route (module.config.php). For example I had 'route' => 'album[/:action][/:id]' instead of 'route' => '/album[/:action][/:id]'.
I was only able to figure it out by intentionally causing errors by misspelling things like making the 'Album\Controller\Albums' instead of 'Album\Controller\Album'in the invokables value, this would cause a stack trace to display which then showed the ZF2 classes that where called on the request. I would continue to misspell, test, and then correct each part of the module.config.php until I was given a clue to what part of the configuration was causing the error.
I'm pretty sure this was not the best way to debug an application's configuration.
There is few things that need to be make sure is:-
You have to add your module in
application.config.php (which you are saying you done it.)
Security\Controller\Security has to be same in default too (which you already has)
One more thing is Your folder structure....
-
Just to doulbe check you have a /MODULE/src/MODULE/Controller/CONTROLLER_FILE_NAME.php
I hope that helps..
I know it is an old post. However another thing to make sure you have in the modules top directory (same directory as the Module.php file) is the "autoload_classmap.php"
file with "<?php return array();?>" inside of it.
A simple tip to know whether your rule has already added correctly to the routes or not, you may check the routes value in the config file inside any working module, as following:
$config = $this->serviceLocator->get('config');
var_dump($config);