ZF2 "Unable to render template" Zend looks in wrong directory - php

I've got a strange situation...
After the creation of the ZF2 SkeletonApplication I created an extra Module called Authentication with an AuthController and a LoginAction also in the view directory "authentication/auth" i placed a login.phtml.
When i run the app i get an error
Zend\View\Renderer\PhpRenderer::render: Unable to render template "authentication/auth/login"; resolver could not resolve to a file
The strange thing is that when I place the complete folder "authentication/auth/login.phtml" in the Standard Application Module View Folder it finds it.
So Zend is looking in the wrong directory.
This is my module.config.php (Authentication Module).
return array(
'router' => array(
'routes' => array(
'authentication' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/authentication/login',
'defaults' => array(
'controller' => 'Authentication\Controller\Auth',
'action' => 'login',
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'Authentication\Controller\Auth' => 'Authentication\Controller\AuthController',
),
),
'viewmanager' => array(
'template_path_stack' => array(
'authentication' => __DIR__ . '/../view',
),
)
);
This is the AuthController
namespace Authentication\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class AuthController extends AbstractActionController
{
public function loginAction()
{
return new ViewModel();
}
}
I hope someone can point me in the right direction.

The complete path cannot be resolved by zf2. The viewManager is using your template pathStack to find the relative view. In your example, the viewManager is looking for this file :
DIR . '/../view/authentication/auth/login.phtml
In other way, you can add to your viewManager a templateMap like this :
'view_manager' => array(
'template_map' => array(
'authentication/auth/login' => __DIR__ . '/../view/where/you/want.phtml',
)
);

change your config:
'template_path_stack' => array(
'authentication' => __DIR__ . '/../view',
),
I am guessing you are a level too far back..
If you config is here:
Authentication/config/module.config.php
then you only want to go back a single level, then into your view directory. Your code would take you back a level higher, into the modules directory.

Related

Zend 2 : Unable to render template

I have an error in my Zend Framework 2 project, I try to add a new view named " Blog "
I created the module.config and indexController, and the view but always I get this error and I don't know why :
Zend\View\Renderer\PhpRenderer::render: Unable to render template
"blog/index/index"; resolver could not resolve to a file
the module.config code
<?php
namespace Blog;
return array(
'router' => array(
'routes' => array(
// 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 /blog/:controller/:action
'blog' => array(
'type' => 'Literal',
'options' => array(
'route' => '/blog',
'defaults' => array(
'__NAMESPACE__' => 'Blog\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(
//'Blog\Controller\Index' => 'Blog\Controller\IndexController'
'Blog\Controller\Index' => Controller\IndexController::class
),
),
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
?>
the indexController code :
<?php
namespace Blog\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function indexAction()
{
return new ViewModel();
}
}
?>
and finally my structure folders is like this :
I use version 2.4.13
The src folder should only contain PHP classes, your view templates should not be in there. Move the view folder up a level and it should work.
Take a look at the folder structure of the ZF skeleton app as a reference: https://github.com/zendframework/ZendSkeletonApplication/tree/master/module/Application

Zendframework - Adding an action to existing controller results in permission denied

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.

Module cannot be initialized zf3 already searched internet

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 :)

Zend\View\Renderer\PhpRenderer::render: Unable to render template, resolver could not resolve to a file

I'm new to this framework and having a trouble rendering the view. here are my codes. I'm following a tutorial from this site http://zf2.readthedocs.io/en/latest/in-depth-guide/first-module.html. I'm really stuck here all i get is the same error over and over. thanks
module.config.php
return array(
// This lines opens the configuration for the RouteManager
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
'controllers' => array(
'invokables' => array(
'Blog\Controller\List' => 'Blog\Controller\ListController'
)
),
'router' => array(
// Open configuration for all possible routes
'routes' => array(
// Define a new route called "post"
'post' => array(
// Define the routes type to be "Zend\Mvc\Router\Http\Literal", which is basically just a string
'type' => 'literal',
// Configure the route itself
'options' => array(
// Listen to "/blog" as uri
'route' => '/blog',
// Define default controller and action to be called when this route is matched
'defaults' => array(
'controller' => 'Blog\Controller\List',
'action' => 'index',
)
)
)
)
),
);
Edit:
Additional Information: Zend\View\Exception\RuntimeException
Message: Zend\View\Renderer\PhpRenderer::render: Unable to render template "blog/list/index"; resolver could not resolve to a file
module
Blog
config
module.config.php
src
Blog
Controller
ListController.php
view
blog
list
index.phtml
Module.php
Regarding my controller, it only contains this
namespace Blog\Controller;
use Zend\Mvc\Controller\AbstractActionController;
Class ListController extends AbstractActionController
{
}
The path to your view directory is wrong. You're setting it to __DIR__ . '/../src/view', pointing it to $module/src/view.
In the directory tree you provided, the views are not located in the $module/src directory though but in $module/view/.
Updating the path should do the trick:
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
(Notice I removed the src fragment).
You can use the below code into module.config.php of the module configuration file
'view_manager' => array(
'template_map' => array(
'layout/blog' => __DIR__ . '/../view/layout/layout.phtml',
'blog/list/index' => __DIR__ . '/../view/blog/list/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),

ZendSkeleton 404 error - The requested URL could not be matched by routing

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

Categories